mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add default stubs to TestContext
Pass default stubs into mocks registry on example startup.
This commit is contained in:
parent
a2b72eaa36
commit
14876a8e9a
8 changed files with 29 additions and 13 deletions
|
@ -27,8 +27,7 @@ module Spectator
|
||||||
# The *example* argument will be the example to run.
|
# The *example* argument will be the example to run.
|
||||||
# The result returned from `Example#run` will be returned.
|
# The result returned from `Example#run` will be returned.
|
||||||
def self.run(example : Example) : Result
|
def self.run(example : Example) : Result
|
||||||
@@current = harness = new(example)
|
@@current = new(example)
|
||||||
harness.mocks.prepare(example.group.context)
|
|
||||||
example.run
|
example.run
|
||||||
ensure
|
ensure
|
||||||
@@current = nil
|
@@current = nil
|
||||||
|
@ -37,7 +36,7 @@ module Spectator
|
||||||
# Retrieves the current running example.
|
# Retrieves the current running example.
|
||||||
getter example : Example
|
getter example : Example
|
||||||
|
|
||||||
getter mocks = Mocks::Registry.new
|
getter mocks : Mocks::Registry
|
||||||
|
|
||||||
# Retrieves the group for the current running example.
|
# Retrieves the group for the current running example.
|
||||||
def group
|
def group
|
||||||
|
@ -60,6 +59,7 @@ module Spectator
|
||||||
# The example the harness is for should be passed in.
|
# The example the harness is for should be passed in.
|
||||||
private def initialize(@example)
|
private def initialize(@example)
|
||||||
@reporter = Expectations::ExpectationReporter.new
|
@reporter = Expectations::ExpectationReporter.new
|
||||||
|
@mocks = Mocks::Registry.new(@example.group.context.stubs)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,8 +10,12 @@ module Spectator::Mocks
|
||||||
@all_instances = {} of String => Entry
|
@all_instances = {} of String => Entry
|
||||||
@entries = {} of Key => Entry
|
@entries = {} of Key => Entry
|
||||||
|
|
||||||
def prepare(context : TestContext) : Nil
|
def initialize(default_stubs)
|
||||||
# TODO
|
@all_instances = default_stubs.map do |k, v|
|
||||||
|
entry = Entry.new
|
||||||
|
entry.stubs.concat(v)
|
||||||
|
{k, entry}
|
||||||
|
end.to_h
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset : Nil
|
def reset : Nil
|
||||||
|
|
|
@ -96,9 +96,8 @@ module Spectator
|
||||||
@@stack.current.add_post_condition(block)
|
@@stack.current.add_post_condition(block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_double(id : Symbol, double_type : Double.class) : Nil
|
def add_default_stub(*args) : Nil
|
||||||
double_factory = DoubleFactory.new(double_type)
|
@@stack.current.add_default_stub(*args)
|
||||||
current_group.add_double(id, double_factory)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Builds the entire spec and returns it as a test suite.
|
# Builds the entire spec and returns it as a test suite.
|
||||||
|
|
|
@ -14,6 +14,7 @@ module Spectator::SpecBuilder
|
||||||
@around_each_hooks = Deque(::SpectatorTest, Proc(Nil) ->).new
|
@around_each_hooks = Deque(::SpectatorTest, Proc(Nil) ->).new
|
||||||
@pre_conditions = Deque(TestMetaMethod).new
|
@pre_conditions = Deque(TestMetaMethod).new
|
||||||
@post_conditions = Deque(TestMetaMethod).new
|
@post_conditions = Deque(TestMetaMethod).new
|
||||||
|
@default_stubs = {} of String => Deque(Mocks::MethodStub)
|
||||||
|
|
||||||
def add_child(child : Child)
|
def add_child(child : Child)
|
||||||
@children << child
|
@children << child
|
||||||
|
@ -47,6 +48,12 @@ module Spectator::SpecBuilder
|
||||||
@post_conditions << hook
|
@post_conditions << hook
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_default_stub(type : T.class, stub : Mocks::MethodStub) forall T
|
||||||
|
key = type.name
|
||||||
|
@default_stubs[key] = Dequeue.new unless @default_stubs.has_key?(key)
|
||||||
|
@default_stubs[key] << stub
|
||||||
|
end
|
||||||
|
|
||||||
private def build_hooks
|
private def build_hooks
|
||||||
ExampleHooks.new(
|
ExampleHooks.new(
|
||||||
@before_all_hooks.to_a,
|
@before_all_hooks.to_a,
|
||||||
|
|
|
@ -7,7 +7,7 @@ module Spectator::SpecBuilder
|
||||||
end
|
end
|
||||||
|
|
||||||
def build(parent_group)
|
def build(parent_group)
|
||||||
context = TestContext.new(parent_group.context, build_hooks, build_conditions, parent_group.context.values)
|
context = TestContext.new(parent_group.context, build_hooks, build_conditions, parent_group.context.values, @default_stubs)
|
||||||
NestedExampleGroup.new(@description, @source, parent_group, context).tap do |group|
|
NestedExampleGroup.new(@description, @source, parent_group, context).tap do |group|
|
||||||
group.children = children.map do |child|
|
group.children = children.map do |child|
|
||||||
child.build(group).as(ExampleComponent)
|
child.build(group).as(ExampleComponent)
|
||||||
|
|
|
@ -4,7 +4,7 @@ require "./example_group_builder"
|
||||||
module Spectator::SpecBuilder
|
module Spectator::SpecBuilder
|
||||||
class RootExampleGroupBuilder < ExampleGroupBuilder
|
class RootExampleGroupBuilder < ExampleGroupBuilder
|
||||||
def build
|
def build
|
||||||
context = TestContext.new(nil, build_hooks, build_conditions, TestValues.empty)
|
context = TestContext.new(nil, build_hooks, build_conditions, TestValues.empty, {} of String => Deque(Mocks::MethodStub))
|
||||||
RootExampleGroup.new(context).tap do |group|
|
RootExampleGroup.new(context).tap do |group|
|
||||||
group.children = children.map do |child|
|
group.children = children.map do |child|
|
||||||
child.build(group).as(ExampleComponent)
|
child.build(group).as(ExampleComponent)
|
||||||
|
|
|
@ -9,7 +9,7 @@ module Spectator::SpecBuilder
|
||||||
def build(parent_group)
|
def build(parent_group)
|
||||||
values = parent_group.context.values
|
values = parent_group.context.values
|
||||||
collection = @collection_builder.call(values)
|
collection = @collection_builder.call(values)
|
||||||
context = TestContext.new(parent_group.context, build_hooks, build_conditions, values)
|
context = TestContext.new(parent_group.context, build_hooks, build_conditions, values, @default_stubs)
|
||||||
NestedExampleGroup.new(@description, @source, parent_group, context).tap do |group|
|
NestedExampleGroup.new(@description, @source, parent_group, context).tap do |group|
|
||||||
group.children = collection.map do |element|
|
group.children = collection.map do |element|
|
||||||
build_sub_group(group, element).as(ExampleComponent)
|
build_sub_group(group, element).as(ExampleComponent)
|
||||||
|
@ -19,7 +19,7 @@ module Spectator::SpecBuilder
|
||||||
|
|
||||||
private def build_sub_group(parent_group, element)
|
private def build_sub_group(parent_group, element)
|
||||||
values = parent_group.context.values.add(@id, @description.to_s, element)
|
values = parent_group.context.values.add(@id, @description.to_s, element)
|
||||||
context = TestContext.new(parent_group.context, ExampleHooks.empty, ExampleConditions.empty, values)
|
context = TestContext.new(parent_group.context, ExampleHooks.empty, ExampleConditions.empty, values, {} of String => Deque(Mocks::MethodStub))
|
||||||
NestedExampleGroup.new("#{@label} = #{element.inspect}", @source, parent_group, context).tap do |group|
|
NestedExampleGroup.new("#{@label} = #{element.inspect}", @source, parent_group, context).tap do |group|
|
||||||
group.children = children.map do |child|
|
group.children = children.map do |child|
|
||||||
child.build(group).as(ExampleComponent)
|
child.build(group).as(ExampleComponent)
|
||||||
|
|
|
@ -5,7 +5,13 @@ module Spectator
|
||||||
class TestContext
|
class TestContext
|
||||||
getter values
|
getter values
|
||||||
|
|
||||||
def initialize(@parent : TestContext?, @hooks : ExampleHooks, @conditions : ExampleConditions, @values : TestValues)
|
getter stubs : Hash(String, Deque(Mocks::MethodStub))
|
||||||
|
|
||||||
|
def initialize(@parent : TestContext?,
|
||||||
|
@hooks : ExampleHooks,
|
||||||
|
@conditions : ExampleConditions,
|
||||||
|
@values : TestValues,
|
||||||
|
@stubs : Hash(String, Deque(Mocks::MethodStub)))
|
||||||
@before_all_hooks_run = false
|
@before_all_hooks_run = false
|
||||||
@after_all_hooks_run = false
|
@after_all_hooks_run = false
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue