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
protected def build : ExampleGroup
root_group.build(nil, {} of Symbol => ValueWrapper)
root_group.build(nil, {} of Symbol => Internals::ValueWrapper)
end
end
end

View File

@ -4,7 +4,7 @@ module Spectator
def initialize(@example_type : Example.class)
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)
end
end

View File

@ -37,7 +37,7 @@ module Spectator
@around_each_hooks << block
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|
children = @children.map do |child|
child.build(group, locals).as(ExampleGroup::Child)

View File

@ -4,11 +4,11 @@ module Spectator
module DSL
class GivenExampleGroupBuilder < ExampleGroupBuilder
def initialize(what : String, @collection : Array(ValueWrapper), @symbol : Symbol)
def initialize(what : String, @collection : Array(Internals::ValueWrapper), @symbol : Symbol)
super(what)
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|
children = [] of ExampleGroup::Child
@collection.each do |value|

View File

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

View File

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