2019-02-12 23:10:14 +00:00
|
|
|
require "./spec_helper"
|
|
|
|
|
|
|
|
# Creates a `Config` for Spectator that is suited for testing it.
|
2019-03-22 06:05:53 +00:00
|
|
|
def spectator_test_config(formatter : Spectator::Formatting::Formatter? = nil, fail_fast = false)
|
2019-03-22 05:40:00 +00:00
|
|
|
builder = Spectator::ConfigBuilder.new
|
|
|
|
builder.formatter = formatter || Spectator::Formatting::SilentFormatter.new
|
2019-03-22 06:05:53 +00:00
|
|
|
builder.fail_fast = fail_fast
|
2019-03-22 05:40:00 +00:00
|
|
|
builder.build
|
2019-02-12 23:10:14 +00:00
|
|
|
end
|
|
|
|
|
2019-03-25 17:35:39 +00:00
|
|
|
def new_test_suite(group : Spectator::ExampleGroup? = nil)
|
|
|
|
filter = Spectator::NullExampleFilter.new
|
|
|
|
Spectator::TestSuite.new(group || PassingExample.create.group, filter)
|
2019-02-12 23:10:14 +00:00
|
|
|
end
|
|
|
|
|
2019-04-23 02:27:54 +00:00
|
|
|
def suite_with_nested_failures(hooks)
|
|
|
|
conditions = Spectator::ExampleConditions.empty
|
|
|
|
values = Spectator::Internals::SampleValues.empty
|
|
|
|
root = Spectator::RootExampleGroup.new(hooks, conditions)
|
|
|
|
root.children = Array(Spectator::ExampleComponent).new(5) do |index|
|
|
|
|
Spectator::NestedExampleGroup.new(index.to_s, root, hooks, conditions).tap do |group|
|
|
|
|
group.children = Array(Spectator::ExampleComponent).new(5) { FailingExample.new(group, values) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
new_test_suite(root)
|
|
|
|
end
|
|
|
|
|
2019-02-12 23:10:14 +00:00
|
|
|
describe Spectator::Runner do
|
|
|
|
describe "#run" do
|
|
|
|
it "runs all examples in the suite" do
|
|
|
|
called = [] of Int32
|
|
|
|
group = SpyExample.create_group(5) do |index|
|
|
|
|
called << index
|
|
|
|
nil
|
|
|
|
end
|
2019-03-25 17:35:39 +00:00
|
|
|
suite = new_test_suite(group)
|
2019-02-12 23:10:14 +00:00
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config)
|
|
|
|
runner.run
|
|
|
|
called.should eq([0, 1, 2, 3, 4])
|
|
|
|
end
|
|
|
|
|
2019-03-22 06:05:53 +00:00
|
|
|
context "with fail-fast enabled" do
|
|
|
|
it "stops on the first failure" do
|
|
|
|
called = [] of Int32
|
|
|
|
group = SpyExample.create_group(10) do |index|
|
|
|
|
called << index
|
|
|
|
raise "Failure" if index > 5
|
|
|
|
end
|
2019-03-25 17:35:39 +00:00
|
|
|
suite = new_test_suite(group)
|
2019-03-22 06:05:53 +00:00
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(fail_fast: true))
|
|
|
|
runner.run
|
|
|
|
called.should eq([0, 1, 2, 3, 4, 5, 6])
|
|
|
|
end
|
|
|
|
|
2019-04-23 01:12:45 +00:00
|
|
|
it "runs after_each hooks" do
|
|
|
|
called = false
|
|
|
|
hooks = new_hooks(after_each: ->{ called = true; nil })
|
|
|
|
group = FailingExample.create_group(hooks: hooks)
|
|
|
|
suite = new_test_suite(group)
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(fail_fast: true))
|
|
|
|
runner.run
|
|
|
|
called.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs after_all hooks" do
|
|
|
|
called = false
|
|
|
|
hooks = new_hooks(after_all: ->{ called = true; nil })
|
|
|
|
group = FailingExample.create_group(hooks: hooks)
|
|
|
|
suite = new_test_suite(group)
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(fail_fast: true))
|
|
|
|
runner.run
|
|
|
|
called.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs the remaining around_each hook code" do
|
|
|
|
called = false
|
|
|
|
hooks = new_hooks(around_each: ->(proc : ->) {
|
|
|
|
proc.call
|
|
|
|
called = true
|
|
|
|
nil
|
|
|
|
})
|
|
|
|
group = FailingExample.create_group(hooks: hooks)
|
|
|
|
suite = new_test_suite(group)
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(fail_fast: true))
|
|
|
|
runner.run
|
|
|
|
called.should be_true
|
|
|
|
end
|
|
|
|
|
2019-04-23 02:27:54 +00:00
|
|
|
context "with nested groups" do
|
|
|
|
it "runs after_each hooks" do
|
|
|
|
call_count = 0
|
|
|
|
hooks = new_hooks(after_each: ->{ call_count += 1; nil })
|
|
|
|
suite = suite_with_nested_failures(hooks)
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(fail_fast: true))
|
|
|
|
runner.run
|
|
|
|
call_count.should eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs after_all hooks" do
|
|
|
|
call_count = 0
|
|
|
|
hooks = new_hooks(after_all: ->{ call_count += 1; nil })
|
|
|
|
suite = suite_with_nested_failures(hooks)
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(fail_fast: true))
|
|
|
|
runner.run
|
|
|
|
call_count.should eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs the remaining around_each hook code" do
|
|
|
|
call_count = 0
|
|
|
|
hooks = new_hooks(around_each: ->(proc : ->) {
|
|
|
|
proc.call
|
|
|
|
call_count += 1
|
|
|
|
nil
|
|
|
|
})
|
|
|
|
suite = suite_with_nested_failures(hooks)
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(fail_fast: true))
|
|
|
|
runner.run
|
|
|
|
call_count.should eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-22 06:05:53 +00:00
|
|
|
context "the report" do
|
|
|
|
it "has the remaining tests" do
|
|
|
|
spy = SpyFormatter.new
|
|
|
|
group = SpyExample.create_group(10) do |index|
|
|
|
|
raise "Failure" if index > 5
|
|
|
|
end
|
2019-03-25 17:35:39 +00:00
|
|
|
suite = new_test_suite(group)
|
2019-03-22 06:05:53 +00:00
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(spy, true))
|
|
|
|
runner.run
|
2019-03-25 18:13:11 +00:00
|
|
|
args = spy.end_suite_calls.first
|
|
|
|
args[:report].remaining_count.should eq(3)
|
2019-03-22 06:05:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-12 23:10:14 +00:00
|
|
|
context "the formatter" do
|
|
|
|
it "#start_suite is called once" do
|
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(new_test_suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
|
|
|
spy.start_suite_call_count.should eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "#start_suite is called at the beginning" do
|
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(new_test_suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
|
|
|
spy.all_calls.first.should eq(:start_suite)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes the test suite to #start_suite" do
|
|
|
|
test_suite = new_test_suite
|
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(test_suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
|
|
|
spy.start_suite_calls.first.should eq(test_suite)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "#end_suite is called once" do
|
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(new_test_suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
|
|
|
spy.end_suite_call_count.should eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "#end_suite is called at the end" do
|
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(new_test_suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
|
|
|
spy.all_calls.last.should eq(:end_suite)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "#start_example is called" do
|
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(new_test_suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
|
|
|
spy.start_example_call_count.should be > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "#start_example is called for each example" do
|
2019-04-06 23:20:12 +00:00
|
|
|
group = SpyExample.create_group(5) { nil }
|
2019-03-25 17:35:39 +00:00
|
|
|
suite = new_test_suite(group)
|
2019-02-12 23:10:14 +00:00
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
|
|
|
spy.start_example_call_count.should eq(5)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes the correct example to #start_example" do
|
2019-04-06 23:20:12 +00:00
|
|
|
group = SpyExample.create_group(5) { nil }
|
2019-03-25 17:35:39 +00:00
|
|
|
suite = new_test_suite(group)
|
2019-02-12 23:10:14 +00:00
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
|
|
|
spy.start_example_calls.should eq(group.children)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls #end_example" do
|
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(new_test_suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
|
|
|
spy.end_example_call_count.should be > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls #end_example for each example" do
|
2019-04-06 23:20:12 +00:00
|
|
|
group = SpyExample.create_group(5) { nil }
|
2019-03-25 17:35:39 +00:00
|
|
|
suite = new_test_suite(group)
|
2019-02-12 23:10:14 +00:00
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
|
|
|
spy.end_example_call_count.should eq(5)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes the correct result to #end_example" do
|
|
|
|
group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty)
|
|
|
|
group.children = Array.new(5) do |index|
|
|
|
|
if index.odd?
|
|
|
|
PassingExample.new(group, Spectator::Internals::SampleValues.empty)
|
|
|
|
else
|
|
|
|
FailingExample.new(group, Spectator::Internals::SampleValues.empty)
|
|
|
|
end.as(Spectator::ExampleComponent)
|
|
|
|
end
|
2019-03-25 17:35:39 +00:00
|
|
|
suite = new_test_suite(group)
|
2019-02-12 23:10:14 +00:00
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
|
|
|
spy.end_example_calls.map(&.example).should eq(group.children)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "the report" do
|
|
|
|
it "contains the expected results" do
|
|
|
|
group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty)
|
|
|
|
group.children = Array.new(5) do |index|
|
|
|
|
if index.odd?
|
|
|
|
PassingExample.new(group, Spectator::Internals::SampleValues.empty)
|
|
|
|
else
|
|
|
|
FailingExample.new(group, Spectator::Internals::SampleValues.empty)
|
|
|
|
end.as(Spectator::ExampleComponent)
|
|
|
|
end
|
2019-03-25 17:35:39 +00:00
|
|
|
suite = new_test_suite(group)
|
2019-02-12 23:10:14 +00:00
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(spy))
|
|
|
|
runner.run
|
2019-03-25 18:13:11 +00:00
|
|
|
args = spy.end_suite_calls.first
|
|
|
|
args[:report].each_with_index do |result, index|
|
2019-02-12 23:10:14 +00:00
|
|
|
if index.odd?
|
|
|
|
result.should be_a(Spectator::SuccessfulResult)
|
|
|
|
else
|
|
|
|
result.should be_a(Spectator::FailedResult)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "contains the expected time span" do
|
2019-04-06 23:20:12 +00:00
|
|
|
group = SpyExample.create_group(5) { nil }
|
2019-03-25 17:35:39 +00:00
|
|
|
suite = new_test_suite(group)
|
2019-02-12 23:10:14 +00:00
|
|
|
spy = SpyFormatter.new
|
|
|
|
runner = Spectator::Runner.new(suite, spectator_test_config(spy))
|
|
|
|
max_time = Time.measure { runner.run }
|
2019-02-12 23:36:06 +00:00
|
|
|
min_time = spy.end_example_calls.each.map(&.as(Spectator::FinishedResult)).sum(&.elapsed)
|
2019-03-25 18:13:11 +00:00
|
|
|
args = spy.end_suite_calls.first
|
|
|
|
report = args[:report]
|
2019-02-12 23:10:14 +00:00
|
|
|
report.runtime.should be <= max_time
|
|
|
|
report.runtime.should be >= min_time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|