Move ValueWrapper and TypedValueWrapper to Internals

Trying to hide from top-level some inner-workings.
This commit is contained in:
Michael Miller 2018-09-23 16:23:26 -06:00
parent 887a04a92a
commit cddfe5591d
11 changed files with 46 additions and 36 deletions

View file

@ -58,7 +58,7 @@ module Spectator
end end
protected def build : ExampleGroup protected def build : ExampleGroup
root_group.build(nil, {} of Symbol => ValueWrapper) root_group.build(nil, {} of Symbol => Internals::ValueWrapper)
end end
end end
end end

View file

@ -4,7 +4,7 @@ module Spectator
def initialize(@example_type : Example.class) def initialize(@example_type : Example.class)
end end
def build(group : ExampleGroup, locals : Hash(Symbol, ValueWrapper)) : Example def build(group : ExampleGroup, locals : Hash(Symbol, Internals::ValueWrapper)) : Example
@example_type.new(group, locals) @example_type.new(group, locals)
end end
end end

View file

@ -37,7 +37,7 @@ module Spectator
@around_each_hooks << block @around_each_hooks << block
end end
def build(parent : ExampleGroup?, locals : Hash(Symbol, ValueWrapper)) : ExampleGroup def build(parent : ExampleGroup?, locals : Hash(Symbol, Internals::ValueWrapper)) : ExampleGroup
ExampleGroup.new(@what, parent, build_hooks).tap do |group| ExampleGroup.new(@what, parent, build_hooks).tap do |group|
children = @children.map do |child| children = @children.map do |child|
child.build(group, locals).as(ExampleGroup::Child) child.build(group, locals).as(ExampleGroup::Child)

View file

@ -4,11 +4,11 @@ module Spectator
module DSL module DSL
class GivenExampleGroupBuilder < ExampleGroupBuilder class GivenExampleGroupBuilder < ExampleGroupBuilder
def initialize(what : String, @collection : Array(ValueWrapper), @symbol : Symbol) def initialize(what : String, @collection : Array(Internals::ValueWrapper), @symbol : Symbol)
super(what) super(what)
end end
def build(parent : ExampleGroup?, locals : Hash(Symbol, ValueWrapper)) : ExampleGroup def build(parent : ExampleGroup?, locals : Hash(Symbol, Internals::ValueWrapper)) : ExampleGroup
ExampleGroup.new(@what, parent, build_hooks).tap do |group| ExampleGroup.new(@what, parent, build_hooks).tap do |group|
children = [] of ExampleGroup::Child children = [] of ExampleGroup::Child
@collection.each do |value| @collection.each do |value|

View file

@ -4,7 +4,7 @@ module Spectator
module DSL module DSL
module StructureDSL module StructureDSL
def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper)) def initialize(locals : Hash(Symbol, ::Spectator::Internals::ValueWrapper))
end end
macro describe(what, &block) macro describe(what, &block)
@ -37,13 +37,13 @@ module Spectator
{{collection}} {{collection}}
end end
@%wrapper : ::Spectator::ValueWrapper @%wrapper : ::Spectator::Internals::ValueWrapper
def {{block.args.empty? ? "value".id : block.args.first}} def {{block.args.empty? ? "value".id : block.args.first}}
@%wrapper.as(::Spectator::TypedValueWrapper(typeof(%collection.first))).value @%wrapper.as(::Spectator::Internals::TypedValueWrapper(typeof(%collection.first))).value
end end
def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper)) def initialize(locals : Hash(Symbol, ::Spectator::Internals::ValueWrapper))
super super
@%wrapper = locals[:%group] @%wrapper = locals[:%group]
end end
@ -70,14 +70,14 @@ module Spectator
macro let(name, &block) macro let(name, &block)
let!(%value) {{block}} let!(%value) {{block}}
@%wrapper : ::Spectator::ValueWrapper? @%wrapper : ::Spectator::Internals::ValueWrapper?
def {{name.id}} def {{name.id}}
if (wrapper = @%wrapper) if (wrapper = @%wrapper)
wrapper.unsafe_as(::Spectator::TypedValueWrapper(typeof(%value))).value wrapper.unsafe_as(::Spectator::Internals::TypedValueWrapper(typeof(%value))).value
else else
%value.tap do |value| %value.tap do |value|
@%wrapper = ::Spectator::TypedValueWrapper(typeof(%value)).new(value) @%wrapper = ::Spectator::Internals::TypedValueWrapper(typeof(%value)).new(value)
end end
end end
end end
@ -159,9 +159,9 @@ module Spectator
include {{@type.id}} include {{@type.id}}
def {{to_a_method_name.id}} def {{to_a_method_name.id}}
Array(::Spectator::ValueWrapper).new.tap do |array| Array(::Spectator::Internals::ValueWrapper).new.tap do |array|
{{collection_method_name.id}}.each do |item| {{collection_method_name.id}}.each do |item|
array << ::Spectator::TypedValueWrapper(typeof(item)).new(item) array << ::Spectator::Internals::TypedValueWrapper(typeof(item)).new(item)
end end
end end
end end
@ -173,7 +173,7 @@ module Spectator
include ::Spectator::DSL::ExampleDSL include ::Spectator::DSL::ExampleDSL
include {{@type.id}} include {{@type.id}}
def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper)) def initialize(locals : Hash(Symbol, ::Spectator::Internals::ValueWrapper))
super super
end end
@ -185,7 +185,7 @@ module Spectator
private macro _spectator_example(example_class_name, wrapper_class_name, base_class, description, &block) private macro _spectator_example(example_class_name, wrapper_class_name, base_class, description, &block)
class {{example_class_name.id}} < {{base_class.id}} class {{example_class_name.id}} < {{base_class.id}}
def initialize(group : ::Spectator::ExampleGroup, locals : Hash(Symbol, ::Spectator::ValueWrapper)) def initialize(group : ::Spectator::ExampleGroup, locals : Hash(Symbol, ::Spectator::Internals::ValueWrapper))
super super
@instance = {{wrapper_class_name.id}}.new(locals) @instance = {{wrapper_class_name.id}}.new(locals)
end end

View file

@ -6,7 +6,7 @@ module Spectator
abstract def run : Result abstract def run : Result
abstract def description : String abstract def description : String
def initialize(@group, @locals = {} of Symbol => ValueWrapper) def initialize(@group, @locals = {} of Symbol => Internals::ValueWrapper)
end end
private getter locals private getter locals

View file

@ -0,0 +1,6 @@
require "./internals/*"
module Spectator
module Internals
end
end

View file

@ -0,0 +1,12 @@
require "./value_wrapper"
module Spectator
module Internals
class TypedValueWrapper(T) < ValueWrapper
getter value : T
def initialize(@value : T)
end
end
end
end

View file

@ -0,0 +1,11 @@
module Spectator
module Internals
# Base class for proxying test values to examples.
# This abstraction is required for inferring types.
# The `DSL#let` macro makes heavy use of this.
abstract class ValueWrapper
# Retrieves the underlying value.
abstract def value
end
end
end

View file

@ -1,10 +0,0 @@
require "./value_wrapper"
module Spectator
class TypedValueWrapper(T) < ValueWrapper
getter value : T
def initialize(@value : T)
end
end
end

View file

@ -1,9 +0,0 @@
module Spectator
# Base class for proxying test values to examples.
# This abstraction is required for inferring types.
# The `DSL#let` macro makes heavy use of this.
abstract class ValueWrapper
# Retrieves the underlying value.
abstract def value
end
end