Obscure given value setter

This commit is contained in:
Michael Miller 2018-09-11 16:19:31 -06:00
parent 65e8ee3788
commit 6f6ba9c812

View file

@ -74,7 +74,7 @@ module Spectator
{% safe_name = description.id.stringify.chars.map { |c| SPECIAL_CHAR_MAPPING[c] || c }.join("").gsub(/\W+/, "_") %} {% safe_name = description.id.stringify.chars.map { |c| SPECIAL_CHAR_MAPPING[c] || c }.join("").gsub(/\W+/, "_") %}
{% class_name = (safe_name.camelcase + "Example").id %} {% class_name = (safe_name.camelcase + "Example").id %}
{% given_vars = GIVEN_VARIABLES %} {% given_vars = GIVEN_VARIABLES %}
{% var_names = given_vars.map { |v| v[0] } %} {% var_names = given_vars.map { |v| v[:name] } %}
class {{class_name.id}} < ::Spectator::Example class {{class_name.id}} < ::Spectator::Example
include Locals include Locals
@ -85,8 +85,8 @@ module Spectator
{% else %} {% else %}
def initialize(context, {{ var_names.join(", ").id }}) def initialize(context, {{ var_names.join(", ").id }})
super(context) super(context)
{% for var_name in var_names %} {% for given_var in given_vars %}
self.{{var_name}} = {{var_name}} {{given_var[:setter]}}({{given_var[:name]}})
{% end %} {% end %}
end end
{% end %} {% end %}
@ -108,8 +108,8 @@ module Spectator
CURRENT_CONTEXT.examples << {{class_name.id}}.new(CURRENT_CONTEXT) CURRENT_CONTEXT.examples << {{class_name.id}}.new(CURRENT_CONTEXT)
{% else %} {% else %}
{% for given_var in given_vars %} {% for given_var in given_vars %}
{% var_name = given_var[0] %} {% var_name = given_var[:name] %}
{% collection = given_var[1] %} {% collection = given_var[:collection] %}
{{collection}}.each do |{{var_name}}| {{collection}}.each do |{{var_name}}|
{% end %} {% end %}
CURRENT_CONTEXT.examples << {{class_name.id}}.new(CURRENT_CONTEXT, {{var_names.join(", ").id}}) CURRENT_CONTEXT.examples << {{class_name.id}}.new(CURRENT_CONTEXT, {{var_names.join(", ").id}})
@ -156,7 +156,7 @@ module Spectator
macro given(collection, &block) macro given(collection, &block)
context({{collection}}, "Given") do context({{collection}}, "Given") do
{% var_name = block.args.empty? ? "value".id : block.args.first %} {% var_name = block.args.empty? ? "value".id : block.args.first %}
{% if GIVEN_VARIABLES.find { |v| v[0].id == var_name.id } %} {% if GIVEN_VARIABLES.find { |v| v[:name] == var_name.id } %}
{% raise "Duplicate given variable name \"#{var_name.id}\"" %} {% raise "Duplicate given variable name \"#{var_name.id}\"" %}
{% end %} {% end %}
@ -175,12 +175,13 @@ module Spectator
@%wrapper.as(TypedValueWrapper(typeof(%collection_first))).value @%wrapper.as(TypedValueWrapper(typeof(%collection_first))).value
end end
private def {{var_name.id}}=(value) {% setter = "_set_#{var_name.id}".id %}
private def {{setter}}(value)
@%wrapper = TypedValueWrapper(typeof(%collection_first)).new(value) @%wrapper = TypedValueWrapper(typeof(%collection_first)).new(value)
end end
end end
{% GIVEN_VARIABLES << {var_name, collection} %} {% GIVEN_VARIABLES << {name: var_name, collection: collection, setter: setter} %}
{{block.body}} {{block.body}}
end end