Store user-friendly name of given variable

This commit is contained in:
Michael Miller 2018-11-02 18:11:46 -06:00
parent c8609ab609
commit 7c21f1e3eb
2 changed files with 9 additions and 5 deletions

View file

@ -10,20 +10,22 @@ module Spectator::DSL
# Creates a new group builder. # Creates a new group builder.
# The value for `what` should be the text the user specified for the collection. # The value for `what` should be the text the user specified for the collection.
# The `collection` is the actual array of items to create examples for. # The `collection` is the actual array of items to create examples for.
# The `name` is the variable name that the user accesses the current collection item with.
# #
# In this code: # In this code:
# ``` # ```
# given random_integers do # given random_integers do |integer|
# # ... # # ...
# end # end
# ``` # ```
# The `what` value would be "random_integers" # The `what` value would be "random_integers"
# and the collection would contain the items returned by calling `random_integers`. # and the collection would contain the items returned by calling `random_integers`.
# The `name` would be "integer".
# #
# The `symbol` is passed along to the sample values # The `symbol` is passed along to the sample values
# so that the example code can retrieve the current item from the collection. # so that the example code can retrieve the current item from the collection.
# The symbol should be unique. # The symbol should be unique.
def initialize(what : String, @collection : Array(T), @symbol : Symbol) def initialize(what : String, @collection : Array(T), @name : String, @symbol : Symbol)
super(what) super(what)
end end
@ -52,8 +54,7 @@ module Spectator::DSL
# so it shouldn't be added prior to calling this method. # so it shouldn't be added prior to calling this method.
private def build_sub_group(parent : ExampleGroup, sample_values : Internals::SampleValues, value : T) : NestedExampleGroup private def build_sub_group(parent : ExampleGroup, sample_values : Internals::SampleValues, value : T) : NestedExampleGroup
# Add the value to sample values for this sub-group. # Add the value to sample values for this sub-group.
# TODO: Use real name instead of symbol as string. sub_values = sample_values.add(@symbol, @name, value)
sub_values = sample_values.add(@symbol, @symbol.to_s, value)
NestedExampleGroup.new(value.to_s, parent, ExampleHooks.empty).tap do |group| NestedExampleGroup.new(value.to_s, parent, ExampleHooks.empty).tap do |group|
# Set the sub-group's children to built versions of the children from this instance. # Set the sub-group's children to built versions of the children from this instance.
group.children = @children.map do |child| group.children = @children.map do |child|

View file

@ -28,6 +28,8 @@ module Spectator::DSL
end end
macro given(collection, &block) macro given(collection, &block)
{% name = block.args.empty? ? "value".id : block.args.first %}
module Group%group module Group%group
include {{@type.id}} include {{@type.id}}
@ -37,7 +39,7 @@ module Spectator::DSL
@%wrapper : ::Spectator::Internals::ValueWrapper @%wrapper : ::Spectator::Internals::ValueWrapper
def {{block.args.empty? ? "value".id : block.args.first}} def {{name}}
@%wrapper.as(::Spectator::Internals::TypedValueWrapper(typeof(%collection.first))).value @%wrapper.as(::Spectator::Internals::TypedValueWrapper(typeof(%collection.first))).value
end end
@ -51,6 +53,7 @@ module Spectator::DSL
::Spectator::DSL::Builder.start_given_group( ::Spectator::DSL::Builder.start_given_group(
{{collection.stringify}}, {{collection.stringify}},
Collection%collection.new.%to_a, Collection%collection.new.%to_a,
{{name.stringify}},
:%group :%group
) )