diff --git a/spec/composite_example_filter_spec.cr b/spec/composite_example_filter_spec.cr deleted file mode 100644 index 0319712..0000000 --- a/spec/composite_example_filter_spec.cr +++ /dev/null @@ -1,33 +0,0 @@ -require "./spec_helper" - -describe Spectator::CompositeExampleFilter do - describe "#includes?" do - context "with a matching filter" do - it "is true" do - example = PassingExample.create - filters = [Spectator::NullExampleFilter.new.as(Spectator::ExampleFilter)] - filter = Spectator::CompositeExampleFilter.new(filters) - filter.includes?(example).should be_true - end - end - - context "with a non-matching filter" do - it "is false" do - example = PassingExample.create - source = Spectator::Source.new(__FILE__, __LINE__) - filters = [Spectator::SourceExampleFilter.new(source).as(Spectator::ExampleFilter)] - filter = Spectator::CompositeExampleFilter.new(filters) - filter.includes?(example).should be_false - end - end - - context "with no filters" do - it "is false" do - example = PassingExample.create - filters = [] of Spectator::ExampleFilter - filter = Spectator::CompositeExampleFilter.new(filters) - filter.includes?(example).should be_false - end - end - end -end diff --git a/spec/dsl/example_factory_spec.cr b/spec/dsl/example_factory_spec.cr deleted file mode 100644 index b3041d2..0000000 --- a/spec/dsl/example_factory_spec.cr +++ /dev/null @@ -1,27 +0,0 @@ -require "../spec_helper" - -describe Spectator::DSL::ExampleFactory do - describe "#build" do - it "creates an example of the correct type" do - factory = Spectator::DSL::ExampleFactory.new(SpyExample) - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - example = factory.build(group, Spectator::Internals::SampleValues.empty) - example.should be_a(SpyExample) - end - - it "passes along the group" do - factory = Spectator::DSL::ExampleFactory.new(SpyExample) - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - example = factory.build(group, Spectator::Internals::SampleValues.empty) - example.group.should eq(group) - end - - it "passes along the sample values" do - factory = Spectator::DSL::ExampleFactory.new(SpyExample) - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - values = Spectator::Internals::SampleValues.empty.add(:foo, "foo", 12345) - example = factory.build(group, values) - example.as(SpyExample).sample_values.should eq(values) - end - end -end diff --git a/spec/dsl/nested_example_group_builder_spec.cr b/spec/dsl/nested_example_group_builder_spec.cr deleted file mode 100644 index cf41fb6..0000000 --- a/spec/dsl/nested_example_group_builder_spec.cr +++ /dev/null @@ -1,234 +0,0 @@ -require "../spec_helper" - -describe Spectator::DSL::NestedExampleGroupBuilder do - describe "#add_child" do - it "creates the correct number of children" do - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - 3.times do - factory = Spectator::DSL::ExampleFactory.new(PassingExample) - group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("bar") - builder.add_child(factory) - builder.add_child(group_builder) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.size.should eq(6) - end - - context "with an ExampleFactory" do - it "creates the example" do - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - factory = Spectator::DSL::ExampleFactory.new(PassingExample) - builder.add_child(factory) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.first.should be_a(PassingExample) - end - end - - context "with an ExampleGroupBuilder" do - it "creates the group" do - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("bar") - builder.add_child(group_builder) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.first.should be_a(Spectator::NestedExampleGroup) - end - end - end - - describe "#add_before_all_hook" do - it "adds a hook" do - hook_called = false - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - builder.add_before_all_hook(->{ - hook_called = true - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_before_hooks - hook_called.should eq(true) - end - - it "supports multiple hooks" do - call_count = 0 - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - 5.times do |i| - builder.add_before_all_hook(->{ - call_count += i + 1 - }) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_before_hooks - call_count.should eq(15) - end - end - - describe "#add_before_each_hook" do - it "adds a hook" do - hook_called = false - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - builder.add_before_each_hook(->{ - hook_called = true - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_before_hooks - hook_called.should eq(true) - end - - it "supports multiple hooks" do - call_count = 0 - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - 5.times do |i| - builder.add_before_each_hook(->{ - call_count += i + 1 - }) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_before_hooks - call_count.should eq(15) - end - end - - describe "#add_after_all_hook" do - it "adds a hook" do - hook_called = false - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - builder.add_after_all_hook(->{ - hook_called = true - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_after_hooks - hook_called.should eq(true) - end - - it "supports multiple hooks" do - call_count = 0 - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - 5.times do |i| - builder.add_after_all_hook(->{ - call_count += i + 1 - }) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_after_hooks - call_count.should eq(15) - end - end - - describe "#add_after_each_hook" do - it "adds a hook" do - hook_called = false - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - builder.add_after_each_hook(->{ - hook_called = true - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_after_hooks - hook_called.should eq(true) - end - - it "supports multiple hooks" do - call_count = 0 - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - 5.times do |i| - builder.add_after_each_hook(->{ - call_count += i + 1 - }) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_after_hooks - call_count.should eq(15) - end - end - - describe "#add_around_each_hook" do - it "adds a hook" do - hook_called = false - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - builder.add_around_each_hook(->(_proc : ->) { - hook_called = true - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - proc = group.wrap_around_each_hooks { } - proc.call - hook_called.should eq(true) - end - - it "supports multiple hooks" do - call_count = 0 - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - 5.times do |i| - builder.add_around_each_hook(->(proc : ->) { - call_count += i + 1 - proc.call - }) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - proc = group.wrap_around_each_hooks { } - proc.call - call_count.should eq(15) - end - end - - describe "#build" do - it "passes along the what value" do - what = "TEST" - builder = Spectator::DSL::NestedExampleGroupBuilder.new(what) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.what.should eq(what) - end - - it "passes along the parent" do - factory = Spectator::DSL::ExampleFactory.new(SpyExample) - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - builder.add_child(factory) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.parent.should be(root) - end - - it "passes along the sample values" do - factory = Spectator::DSL::ExampleFactory.new(SpyExample) - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - builder.add_child(factory) - values = Spectator::Internals::SampleValues.empty.add(:foo, "foo", 12345) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, values) - group.children.first.as(SpyExample).sample_values.should eq(values) - end - - it "specifies the parent of the children correctly" do - builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - 3.times do - factory = Spectator::DSL::ExampleFactory.new(PassingExample) - group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("bar") - builder.add_child(factory) - builder.add_child(group_builder) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.all? do |child| - case (child) - when Spectator::Example - child.group == group - when Spectator::NestedExampleGroup - child.parent == group - else - false - end - end.should be_true - end - end -end diff --git a/spec/dsl/root_example_group_builder_spec.cr b/spec/dsl/root_example_group_builder_spec.cr deleted file mode 100644 index b4aeb9d..0000000 --- a/spec/dsl/root_example_group_builder_spec.cr +++ /dev/null @@ -1,202 +0,0 @@ -require "../spec_helper" - -describe Spectator::DSL::RootExampleGroupBuilder do - describe "#add_child" do - it "creates the correct number of children" do - builder = Spectator::DSL::RootExampleGroupBuilder.new - 3.times do - factory = Spectator::DSL::ExampleFactory.new(PassingExample) - group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - builder.add_child(factory) - builder.add_child(group_builder) - end - group = builder.build(Spectator::Internals::SampleValues.empty) - group.children.size.should eq(6) - end - - context "with an ExampleFactory" do - it "creates the example" do - builder = Spectator::DSL::RootExampleGroupBuilder.new - factory = Spectator::DSL::ExampleFactory.new(PassingExample) - builder.add_child(factory) - group = builder.build(Spectator::Internals::SampleValues.empty) - group.children.first.should be_a(PassingExample) - end - end - - context "with an ExampleGroupBuilder" do - it "creates the group" do - builder = Spectator::DSL::RootExampleGroupBuilder.new - group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - builder.add_child(group_builder) - group = builder.build(Spectator::Internals::SampleValues.empty) - group.children.first.should be_a(Spectator::NestedExampleGroup) - end - end - end - - describe "#add_before_all_hook" do - it "adds a hook" do - hook_called = false - builder = Spectator::DSL::RootExampleGroupBuilder.new - builder.add_before_all_hook(->{ - hook_called = true - }) - group = builder.build(Spectator::Internals::SampleValues.empty) - group.run_before_hooks - hook_called.should eq(true) - end - - it "supports multiple hooks" do - call_count = 0 - builder = Spectator::DSL::RootExampleGroupBuilder.new - 5.times do |i| - builder.add_before_all_hook(->{ - call_count += i + 1 - }) - end - group = builder.build(Spectator::Internals::SampleValues.empty) - group.run_before_hooks - call_count.should eq(15) - end - end - - describe "#add_before_each_hook" do - it "adds a hook" do - hook_called = false - builder = Spectator::DSL::RootExampleGroupBuilder.new - builder.add_before_each_hook(->{ - hook_called = true - }) - group = builder.build(Spectator::Internals::SampleValues.empty) - group.run_before_hooks - hook_called.should eq(true) - end - - it "supports multiple hooks" do - call_count = 0 - builder = Spectator::DSL::RootExampleGroupBuilder.new - 5.times do |i| - builder.add_before_each_hook(->{ - call_count += i + 1 - }) - end - group = builder.build(Spectator::Internals::SampleValues.empty) - group.run_before_hooks - call_count.should eq(15) - end - end - - describe "#add_after_all_hook" do - it "adds a hook" do - hook_called = false - builder = Spectator::DSL::RootExampleGroupBuilder.new - builder.add_after_all_hook(->{ - hook_called = true - }) - group = builder.build(Spectator::Internals::SampleValues.empty) - group.run_after_hooks - hook_called.should eq(true) - end - - it "supports multiple hooks" do - call_count = 0 - builder = Spectator::DSL::RootExampleGroupBuilder.new - 5.times do |i| - builder.add_after_all_hook(->{ - call_count += i + 1 - }) - end - group = builder.build(Spectator::Internals::SampleValues.empty) - group.run_after_hooks - call_count.should eq(15) - end - end - - describe "#add_after_each_hook" do - it "adds a hook" do - hook_called = false - builder = Spectator::DSL::RootExampleGroupBuilder.new - builder.add_after_each_hook(->{ - hook_called = true - }) - group = builder.build(Spectator::Internals::SampleValues.empty) - group.run_after_hooks - hook_called.should eq(true) - end - - it "supports multiple hooks" do - call_count = 0 - builder = Spectator::DSL::RootExampleGroupBuilder.new - 5.times do |i| - builder.add_after_each_hook(->{ - call_count += i + 1 - }) - end - group = builder.build(Spectator::Internals::SampleValues.empty) - group.run_after_hooks - call_count.should eq(15) - end - end - - describe "#add_around_each_hook" do - it "adds a hook" do - hook_called = false - builder = Spectator::DSL::RootExampleGroupBuilder.new - builder.add_around_each_hook(->(_proc : ->) { - hook_called = true - }) - group = builder.build(Spectator::Internals::SampleValues.empty) - proc = group.wrap_around_each_hooks { } - proc.call - hook_called.should eq(true) - end - - it "supports multiple hooks" do - call_count = 0 - builder = Spectator::DSL::RootExampleGroupBuilder.new - 5.times do |i| - builder.add_around_each_hook(->(proc : ->) { - call_count += i + 1 - proc.call - }) - end - group = builder.build(Spectator::Internals::SampleValues.empty) - proc = group.wrap_around_each_hooks { } - proc.call - call_count.should eq(15) - end - end - - describe "#build" do - it "passes along the sample values" do - factory = Spectator::DSL::ExampleFactory.new(SpyExample) - builder = Spectator::DSL::RootExampleGroupBuilder.new - builder.add_child(factory) - values = Spectator::Internals::SampleValues.empty.add(:foo, "foo", 12345) - group = builder.build(values) - group.children.first.as(SpyExample).sample_values.should eq(values) - end - - it "specifies the parent of the children correctly" do - builder = Spectator::DSL::RootExampleGroupBuilder.new - 3.times do - factory = Spectator::DSL::ExampleFactory.new(PassingExample) - group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("foo") - builder.add_child(factory) - builder.add_child(group_builder) - end - group = builder.build(Spectator::Internals::SampleValues.empty) - group.children.all? do |child| - case (child) - when Spectator::Example - child.group == group - when Spectator::NestedExampleGroup - child.parent == group - else - false - end - end.should be_true - end - end -end diff --git a/spec/dsl/sample_example_group_builder_spec.cr b/spec/dsl/sample_example_group_builder_spec.cr deleted file mode 100644 index a5a9d88..0000000 --- a/spec/dsl/sample_example_group_builder_spec.cr +++ /dev/null @@ -1,350 +0,0 @@ -require "../spec_helper" - -SAMPLE_VALUES_COLLECTION = %i[foo bar baz] - -struct SampleValueCollection - def initialize(sample_values : ::Spectator::Internals::SampleValues) - end - - def create - SAMPLE_VALUES_COLLECTION - end -end - -describe Spectator::DSL::SampleExampleGroupBuilder do - describe "#add_child" do - it "creates the correct number of children" do - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - count = 4 - count.times do - factory = Spectator::DSL::ExampleFactory.new(PassingExample) - group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("bar") - builder.add_child(factory) - builder.add_child(group_builder) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - all_children = group.map { |child| child.as(Spectator::ExampleGroup).to_a }.flatten - all_children.size.should eq(2 * count * SAMPLE_VALUES_COLLECTION.size) - end - - context "with an ExampleFactory" do - it "creates an example for each item in the collection" do - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - factory = Spectator::DSL::ExampleFactory.new(PassingExample) - builder.add_child(factory) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - all_children = group.map { |child| child.as(Spectator::ExampleGroup).to_a }.flatten - all_children.all? { |child| child.is_a?(PassingExample) }.should be_true - end - end - - context "with an ExampleGroupBuilder" do - it "creates a group for each item in the collection" do - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("bar") - builder.add_child(group_builder) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - all_children = group.map { |child| child.as(Spectator::ExampleGroup).to_a }.flatten - all_children.all? { |child| child.is_a?(Spectator::NestedExampleGroup) }.should be_true - end - end - end - - describe "#add_before_all_hook" do - it "adds a hook" do - hook_called = false - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - builder.add_before_all_hook(->{ - hook_called = true - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_before_hooks - hook_called.should eq(true) - end - - it "attachs the hook to just the top-level group" do - call_count = 0 - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - builder.add_before_all_hook(->{ - call_count += 1 - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_before_hooks) - call_count.should eq(1) - end - - it "supports multiple hooks" do - call_count = 0 - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - 5.times do |i| - builder.add_before_all_hook(->{ - call_count += i + 1 - }) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_before_hooks - call_count.should eq(15) - end - end - - describe "#add_before_each_hook" do - it "adds a hook" do - hook_called = false - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - builder.add_before_each_hook(->{ - hook_called = true - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_before_hooks - hook_called.should eq(true) - end - - it "attachs the hook to just the top-level group" do - call_count = 0 - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - builder.add_before_each_hook(->{ - call_count += 1 - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_before_hooks) - call_count.should eq(SAMPLE_VALUES_COLLECTION.size) - end - - it "supports multiple hooks" do - call_count = 0 - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - 5.times do |i| - builder.add_before_each_hook(->{ - call_count += i + 1 - }) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_before_hooks - call_count.should eq(15) - end - end - - describe "#add_after_all_hook" do - it "adds a hook" do - hook_called = false - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - builder.add_after_all_hook(->{ - hook_called = true - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_after_hooks - hook_called.should eq(true) - end - - it "attachs the hook to just the top-level group" do - call_count = 0 - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - builder.add_after_all_hook(->{ - call_count += 1 - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_after_hooks) - call_count.should eq(1) - end - - it "supports multiple hooks" do - call_count = 0 - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - 5.times do |i| - builder.add_after_all_hook(->{ - call_count += i + 1 - }) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_after_hooks - call_count.should eq(15) - end - end - - describe "#add_after_each_hook" do - it "adds a hook" do - hook_called = false - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - builder.add_after_each_hook(->{ - hook_called = true - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_after_hooks - hook_called.should eq(true) - end - - it "attachs the hook to just the top-level group" do - call_count = 0 - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - builder.add_after_each_hook(->{ - call_count += 1 - }) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.map(&.as(Spectator::ExampleGroup)).each(&.run_after_hooks) - call_count.should eq(SAMPLE_VALUES_COLLECTION.size) - end - - it "supports multiple hooks" do - call_count = 0 - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - 5.times do |i| - builder.add_after_each_hook(->{ - call_count += i + 1 - }) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.run_after_hooks - call_count.should eq(15) - end - end - - describe "#build" do - it "passes along the what value" do - what = "TEST" - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new(what, SampleValueCollection, create_proc, "value", :foo) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.what.should eq(what) - end - - it "passes along the parent" do - factory = Spectator::DSL::ExampleFactory.new(SpyExample) - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - builder.add_child(factory) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.parent.should be(root) - end - - it "passes along the sample values" do - factory = Spectator::DSL::ExampleFactory.new(SpyExample) - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - builder.add_child(factory) - symbol = :test - values = Spectator::Internals::SampleValues.empty.add(symbol, "foo", 12345) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, values) - all_children = group.map { |child| child.as(Spectator::ExampleGroup).to_a }.flatten - all_children.map(&.as(SpyExample)).all? { |child| child.sample_values.get_wrapper(symbol) }.should be_true - end - - it "passes along the value name" do - symbol = :foo - name = "value" - factory = Spectator::DSL::ExampleFactory.new(SpyExample) - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, name, symbol) - builder.add_child(factory) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - all_children = group.map { |child| child.as(Spectator::ExampleGroup).to_a }.flatten - all_children.each do |child| - entries = child.as(SpyExample).sample_values.map(&.name) - entries.should contain(name) - end - end - - it "creates the correct number of sub-groups" do - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - factory = Spectator::DSL::ExampleFactory.new(PassingExample) - builder.add_child(factory) - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.size.should eq(SAMPLE_VALUES_COLLECTION.size) - end - - it "passes the correct value to each sub-group" do - factory = Spectator::DSL::ExampleFactory.new(SpyExample) - symbol = :test - count = 3 - expected = Array.new(SAMPLE_VALUES_COLLECTION.size * count) { |i| SAMPLE_VALUES_COLLECTION[i // count] } - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", symbol) - count.times { builder.add_child(factory) } - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - all_children = group.map { |child| child.as(Spectator::ExampleGroup).to_a }.flatten - all_children.map { |child| child.as(SpyExample).sample_values.get_value(symbol, typeof(SAMPLE_VALUES_COLLECTION.first)) }.should eq(expected) - end - - it "specifies the parent of the children correctly" do - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - 3.times do - factory = Spectator::DSL::ExampleFactory.new(PassingExample) - group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("baz") - builder.add_child(factory) - builder.add_child(group_builder) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.all? do |child| - case (child) - when Spectator::Example - child.group == group - when Spectator::NestedExampleGroup - child.parent == group - else - false - end - end.should be_true - end - - it "specifies the container for the parent of the sub-groups" do - create_proc = ->(s : SampleValueCollection) { s.create } - builder = Spectator::DSL::SampleExampleGroupBuilder.new("foobar", SampleValueCollection, create_proc, "value", :foo) - 3.times do - factory = Spectator::DSL::ExampleFactory.new(PassingExample) - group_builder = Spectator::DSL::NestedExampleGroupBuilder.new("baz") - builder.add_child(factory) - builder.add_child(group_builder) - end - root = Spectator::DSL::RootExampleGroupBuilder.new.build(Spectator::Internals::SampleValues.empty) - group = builder.build(root, Spectator::Internals::SampleValues.empty) - group.children.all? do |child| - case (child) - when Spectator::Example - child.group == group - when Spectator::NestedExampleGroup - child.parent == group - else - false - end - end.should be_true - end - end -end diff --git a/spec/errored_result_spec.cr b/spec/errored_result_spec.cr deleted file mode 100644 index 4b5f524..0000000 --- a/spec/errored_result_spec.cr +++ /dev/null @@ -1,87 +0,0 @@ -require "./spec_helper" - -def new_errored_result( - example : Spectator::Example? = nil, - elapsed : Time::Span? = nil, - expectations : Spectator::Expectations::ExampleExpectations? = nil, - error : Exception? = nil -) - Spectator::ErroredResult.new( - example || FailingExample.create, - elapsed || Time::Span.zero, - expectations || Spectator::Expectations::ExampleExpectations.new(generate_expectations(0, 1)[:expectations]), - error || Exception.new("foobar") - ) -end - -describe Spectator::ErroredResult do - describe "#call" do - context "without a block" do - it "invokes #error on an instance" do - spy = ResultCallSpy.new - new_errored_result.call(spy) - spy.error?.should be_true - end - - it "returns the value of #failure" do - result = new_errored_result - returned = result.call(ResultCallSpy.new) - returned.should eq(:error) - end - end - - context "with a block" do - it "invokes #error on an instance" do - spy = ResultCallSpy.new - new_errored_result.call(spy) { nil } - spy.error?.should be_true - end - - it "yields itself" do - result = new_errored_result - value = nil.as(Spectator::Result?) - result.call(ResultCallSpy.new) { |r| value = r } - value.should eq(result) - end - - it "returns the value of #failure" do - result = new_errored_result - value = 42 - returned = result.call(ResultCallSpy.new) { value } - returned.should eq(value) - end - end - end - - describe "#example" do - it "is the expected value" do - example = FailingExample.create - result = new_errored_result(example: example) - result.example.should eq(example) - end - end - - describe "#elapsed" do - it "is the expected value" do - elapsed = Time::Span.new(10, 10, 10) - result = new_errored_result(elapsed: elapsed) - result.elapsed.should eq(elapsed) - end - end - - describe "#expectations" do - it "is the expected value" do - expectations = Spectator::Expectations::ExampleExpectations.new(generate_expectations(5, 1)[:expectations]) - result = new_errored_result(expectations: expectations) - result.expectations.should eq(expectations) - end - end - - describe "#error" do - it "is the expected value" do - error = IO::Error.new("oops") - result = new_errored_result(error: error) - result.error.should eq(error) - end - end -end diff --git a/spec/example_conditions_spec.cr b/spec/example_conditions_spec.cr deleted file mode 100644 index 3398f02..0000000 --- a/spec/example_conditions_spec.cr +++ /dev/null @@ -1,36 +0,0 @@ -require "./spec_helper" - -describe Spectator::ExampleConditions do - {% for condition in %i[pre post] %} - describe "#run_{{condition.id}}_conditions" do - it "calls a proc" do - called = false - conditions = new_conditions({{condition.id}}: ->{ called = true; nil }) - conditions.run_{{condition.id}}_conditions - called.should be_true - end - - it "calls multiple procs" do - call_count = 0 - conditions = new_conditions({{condition.id}}: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - conditions.run_{{condition.id}}_conditions - call_count.should eq(6) - end - - it "calls procs in the correct order" do - calls = [] of Symbol - conditions = new_conditions({{condition.id}}: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - conditions.run_{{condition.id}}_conditions - calls.should eq(\%i[a b c]) - end - end - {% end %} -end diff --git a/spec/example_hooks_spec.cr b/spec/example_hooks_spec.cr deleted file mode 100644 index 9fc8798..0000000 --- a/spec/example_hooks_spec.cr +++ /dev/null @@ -1,79 +0,0 @@ -require "./spec_helper" - -describe Spectator::ExampleHooks do - {% for hook in %i[before_all before_each after_all after_each] %} - describe "#run_{{hook.id}}" do - it "calls a proc" do - called = false - hooks = new_hooks({{hook.id}}: ->{ called = true; nil }) - hooks.run_{{hook.id}} - called.should be_true - end - - it "calls multiple procs" do - call_count = 0 - hooks = new_hooks({{hook.id}}: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - hooks.run_{{hook.id}} - call_count.should eq(6) - end - - it "calls procs in the correct order" do - calls = [] of Symbol - hooks = new_hooks({{hook.id}}: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - hooks.run_{{hook.id}} - calls.should eq(\%i[a b c]) - end - end - {% end %} - - describe "#wrap_around_each" do - it "wraps the block" do - called = false - wrapper = new_hooks.wrap_around_each do - called = true - end - wrapper.call - called.should be_true - end - - it "wraps a proc" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { called = true; proc.call }) - wrapper = hooks.wrap_around_each { } - wrapper.call - called.should be_true - end - - it "wraps multiple procs" do - call_count = 0 - hooks = new_hooks(around_each: [ - ->(proc : ->) { call_count += 1; proc.call }, - ->(proc : ->) { call_count += 2; proc.call }, - ->(proc : ->) { call_count += 3; proc.call }, - ]) - wrapper = hooks.wrap_around_each { } - wrapper.call - call_count.should eq(6) - end - - it "wraps procs in the correct order" do - calls = [] of Symbol - hooks = new_hooks(around_each: [ - ->(proc : ->) { calls << :a; proc.call }, - ->(proc : ->) { calls << :b; proc.call }, - ->(proc : ->) { calls << :c; proc.call }, - ]) - wrapper = hooks.wrap_around_each { } - wrapper.call - calls.should eq(%i[a b c]) - end - end -end diff --git a/spec/example_iterator_spec.cr b/spec/example_iterator_spec.cr deleted file mode 100644 index e7829f4..0000000 --- a/spec/example_iterator_spec.cr +++ /dev/null @@ -1,205 +0,0 @@ -require "./spec_helper" - -describe Spectator::ExampleIterator do - describe "#next" do - context "with one example" do - it "returns the example" do - example = PassingExample.create - iterator = Spectator::ExampleIterator.new(example.group) - iterator.next.should eq(example) - end - - it "returns 'stop' after the example" do - example = PassingExample.create - iterator = Spectator::ExampleIterator.new(example.group) - iterator.next # Should return example. - iterator.next.should be_a(Iterator::Stop) - end - end - - context "when empty" do - it "returns 'stop'" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = [] of Spectator::ExampleComponent - iterator = Spectator::ExampleIterator.new(group) - iterator.next.should be_a(Iterator::Stop) - end - end - - context "with one level of examples" do - it "iterates through all examples" do - examples = [] of Spectator::Example - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - iterator = Spectator::ExampleIterator.new(group) - 5.times { examples << iterator.next.as(Spectator::Example) } - examples.should eq(group.children) - end - - it "returns 'stop' at the end" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - iterator = Spectator::ExampleIterator.new(group) - 5.times { iterator.next } - iterator.next.should be_a(Iterator::Stop) - end - end - - context "with empty sub-groups" do - context "one level deep" do - it "returns 'stop'" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = [] of Spectator::ExampleComponent - end - end - iterator = Spectator::ExampleIterator.new(group) - iterator.next.should be_a(Iterator::Stop) - end - end - - context "multiple levels deep" do - it "returns 'stop'" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = Array(Spectator::ExampleComponent).new(5) do |j| - Spectator::NestedExampleGroup.new(j.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_sub_group| - sub_sub_group.children = [] of Spectator::ExampleComponent - end - end - end - end - iterator = Spectator::ExampleIterator.new(group) - iterator.next.should be_a(Iterator::Stop) - end - end - end - - context "with multiple levels of examples" do - it "iterates through all examples" do - actual_examples = [] of Spectator::Example - expected_examples = [] of Spectator::Example - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - expected_examples << example - end - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(sub_group, Spectator::Internals::SampleValues.empty).tap do |example| - expected_examples << example - end - end - end - end - end - iterator = Spectator::ExampleIterator.new(group) - 13.times { actual_examples << iterator.next.as(Spectator::Example) } - actual_examples.should eq(expected_examples) - end - - it "returns 'stop' at the end" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(sub_group, Spectator::Internals::SampleValues.empty) - end - end - end - end - iterator = Spectator::ExampleIterator.new(group) - 13.times { iterator.next } - iterator.next.should be_a(Iterator::Stop) - end - end - - context "with deep nesting" do - # Sorry for this atrocity, - # but it was fun to write. - it "iterates through all examples" do - actual_examples = [] of Spectator::Example - expected_examples = [] of Spectator::Example - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group1| - sub_group1.children = Array(Spectator::ExampleComponent).new(5) do |j| - Spectator::NestedExampleGroup.new(j.to_s, sub_group1, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group2| - sub_group2.children = Array(Spectator::ExampleComponent).new(5) do |k| - Spectator::NestedExampleGroup.new(k.to_s, sub_group2, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group3| - sub_group3.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(sub_group3, Spectator::Internals::SampleValues.empty).tap do |example| - expected_examples << example - end - end - end - end - end - end - end - end - iterator = Spectator::ExampleIterator.new(group) - (5 ** 4).times { actual_examples << iterator.next.as(Spectator::Example) } - actual_examples.should eq(expected_examples) - end - - it "returns 'stop' at the end" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group1| - sub_group1.children = Array(Spectator::ExampleComponent).new(5) do |j| - Spectator::NestedExampleGroup.new(j.to_s, sub_group1, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group2| - sub_group2.children = Array(Spectator::ExampleComponent).new(5) do |k| - Spectator::NestedExampleGroup.new(k.to_s, sub_group2, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group3| - sub_group3.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(sub_group3, Spectator::Internals::SampleValues.empty) - end - end - end - end - end - end - end - iterator = Spectator::ExampleIterator.new(group) - (5 ** 4).times { iterator.next } - iterator.next.should be_a(Iterator::Stop) - end - end - - it "returns 'stop' after the end has been reached" do - example = PassingExample.create - iterator = Spectator::ExampleIterator.new(example.group) - iterator.next # Should return example. - iterator.next # Should return "stop". - iterator.next.should be_a(Iterator::Stop) # Should still return "stop". - end - end - - describe "#rewind" do - it "restarts the iterator" do - example = PassingExample.create - iterator = Spectator::ExampleIterator.new(example.group) - iterator.next - iterator.rewind - iterator.next.should eq(example) - end - - it "can be called before #next" do - example = PassingExample.create - iterator = Spectator::ExampleIterator.new(example.group) - iterator.rewind - iterator.next.should eq(example) - end - end -end diff --git a/spec/expectation_failed_spec.cr b/spec/expectation_failed_spec.cr deleted file mode 100644 index 54c5d1d..0000000 --- a/spec/expectation_failed_spec.cr +++ /dev/null @@ -1,19 +0,0 @@ -require "./spec_helper" - -describe Spectator::ExpectationFailed do - describe "#expectation" do - it "contains the expected value" do - expectation = new_unsatisfied_expectation - error = Spectator::ExpectationFailed.new(expectation) - error.expectation.should eq(expectation) - end - end - - describe "#message" do - it "is the same as the expectation's #actual_message" do - expectation = new_unsatisfied_expectation - error = Spectator::ExpectationFailed.new(expectation) - error.message.should eq(expectation.failure_message) - end - end -end diff --git a/spec/expectations/example_expectations_spec.cr b/spec/expectations/example_expectations_spec.cr deleted file mode 100644 index 0b55ab9..0000000 --- a/spec/expectations/example_expectations_spec.cr +++ /dev/null @@ -1,207 +0,0 @@ -require "../spec_helper" - -describe Spectator::Expectations::ExampleExpectations do - describe "#each" do - it "yields all expectations" do - tuple = generate_expectations(5, 5) - expectations = [] of Spectator::Expectations::Expectation - tuple[:reporter].expectations.each { |expectation| expectations << expectation } - # Expectations might not be in the same order. - # Just check if if the arrays contain the same items. - expectations.size.should eq(tuple[:expectations].size) - (expectations - tuple[:expectations]).empty?.should be_true - end - end - - describe "#satisfied" do - it "returns only satisfied expectations" do - tuple = generate_expectations(5, 5) - expectations = tuple[:reporter].expectations - expectations.satisfied.all?(&.satisfied?).should be_true - end - - it "returns the correct expectations" do - tuple = generate_expectations(5, 5) - expectations = tuple[:reporter].expectations - satisfied = expectations.satisfied.to_a - satisfied.size.should eq(5) - (satisfied - tuple[:satisfied]).empty?.should be_true - end - - context "with all satisfied expectations" do - it "returns all expectations" do - tuple = generate_expectations(5, 0) - expectations = tuple[:reporter].expectations - expectations.satisfied.size.should eq(tuple[:satisfied].size) - end - end - - context "with all unsatisfied expectations" do - it "returns an empty collection" do - tuple = generate_expectations(0, 5) - expectations = tuple[:reporter].expectations - expectations.satisfied.size.should eq(0) - end - end - end - - describe "#each_satisfied" do - it "yields only satisfied expectations" do - tuple = generate_expectations(5, 5) - expectations = [] of Spectator::Expectations::Expectation - tuple[:reporter].expectations.each_satisfied { |expectation| expectations << expectation } - # Expectations might not be in the same order. - # Just check if if the arrays contain the same items. - expectations.size.should eq(tuple[:satisfied].size) - (expectations - tuple[:satisfied]).empty?.should be_true - end - - context "with all satisfied expectations" do - it "yields all expectations" do - tuple = generate_expectations(0, 5) - expectations = [] of Spectator::Expectations::Expectation - tuple[:reporter].expectations.each_satisfied { |expectation| expectations << expectation } - expectations.size.should eq(tuple[:satisfied].size) - end - end - - context "with all unsatisfied expectations" do - it "yields nothing" do - tuple = generate_expectations(0, 5) - expectations = [] of Spectator::Expectations::Expectation - tuple[:reporter].expectations.each_satisfied { |expectation| expectations << expectation } - expectations.empty?.should be_true - end - end - end - - describe "#unsatisfied" do - it "returns only unsatisfied expectations" do - tuple = generate_expectations(5, 5) - expectations = tuple[:reporter].expectations - expectations.unsatisfied.all?(&.satisfied?).should be_false - end - - it "returns the correct expectations" do - tuple = generate_expectations(5, 5) - expectations = tuple[:reporter].expectations - unsatisfied = expectations.unsatisfied.to_a - unsatisfied.size.should eq(5) - (unsatisfied - tuple[:unsatisfied]).empty?.should be_true - end - - context "with all satisfied expectations" do - it "returns an empty collection" do - tuple = generate_expectations(5, 0) - expectations = tuple[:reporter].expectations - expectations.unsatisfied.size.should eq(0) - end - end - - context "with all unsatisfied expectations" do - it "returns all expectations" do - tuple = generate_expectations(0, 5) - expectations = tuple[:reporter].expectations - expectations.unsatisfied.size.should eq(tuple[:unsatisfied].size) - end - end - end - - describe "#each_unsatisfied" do - it "yields only unsatisfied expectations" do - tuple = generate_expectations(5, 5) - expectations = [] of Spectator::Expectations::Expectation - tuple[:reporter].expectations.each_unsatisfied { |expectation| expectations << expectation } - # Expectations might not be in the same order. - # Just check if if the arrays contain the same items. - expectations.size.should eq(tuple[:unsatisfied].size) - (expectations - tuple[:unsatisfied]).empty?.should be_true - end - - context "with all satisfied expectations" do - it "yields nothing" do - tuple = generate_expectations(5, 0) - expectations = [] of Spectator::Expectations::Expectation - tuple[:reporter].expectations.each_unsatisfied { |expectation| expectations << expectation } - expectations.empty?.should be_true - end - end - - context "with all unsatisfied expectations" do - it "yields all expectations" do - tuple = generate_expectations(0, 5) - expectations = [] of Spectator::Expectations::Expectation - tuple[:reporter].expectations.each_unsatisfied { |expectation| expectations << expectation } - expectations.size.should eq(tuple[:unsatisfied].size) - end - end - end - - describe "#successful?" do - context "with all satisfied expectations" do - it "is true" do - tuple = generate_expectations(5, 0) - expectations = tuple[:reporter].expectations - expectations.successful?.should be_true - end - end - - context "with one unsatisfied expectation" do - it "is false" do - tuple = generate_expectations(5, 1) - expectations = tuple[:reporter].expectations - expectations.successful?.should be_false - end - end - - context "with one satisfied expectation" do - it "is false" do - tuple = generate_expectations(1, 5) - expectations = tuple[:reporter].expectations - expectations.successful?.should be_false - end - end - - context "with all unsatisfied expectations" do - it "is false" do - tuple = generate_expectations(0, 5) - expectations = tuple[:reporter].expectations - expectations.successful?.should be_false - end - end - end - - describe "#failed?" do - context "with all satisfied expectations" do - it "is false" do - tuple = generate_expectations(5, 0) - expectations = tuple[:reporter].expectations - expectations.failed?.should be_false - end - end - - context "with one unsatisfied expectation" do - it "is true" do - tuple = generate_expectations(5, 1) - expectations = tuple[:reporter].expectations - expectations.failed?.should be_true - end - end - - context "with one satisfied expectation" do - it "is true" do - tuple = generate_expectations(1, 5) - expectations = tuple[:reporter].expectations - expectations.failed?.should be_true - end - end - - context "with all unsatisfied expectations" do - it "is true" do - tuple = generate_expectations(0, 5) - expectations = tuple[:reporter].expectations - expectations.failed?.should be_true - end - end - end -end diff --git a/spec/expectations/expectation_reporter_spec.cr b/spec/expectations/expectation_reporter_spec.cr deleted file mode 100644 index 0f32281..0000000 --- a/spec/expectations/expectation_reporter_spec.cr +++ /dev/null @@ -1,80 +0,0 @@ -require "../spec_helper" - -describe Spectator::Expectations::ExpectationReporter do - describe "#report" do - context "with raise flag set" do - context "given a satisfied expectation" do - it "stores the result" do - expectation = new_satisfied_expectation - reporter = Spectator::Expectations::ExpectationReporter.new(true) - reporter.report(expectation) - reporter.expectations.should contain(expectation) - end - end - - context "given a unsatisfied expectation" do - it "raises and error" do - expectation = new_unsatisfied_expectation - reporter = Spectator::Expectations::ExpectationReporter.new(true) - expect_raises(Spectator::ExpectationFailed) { reporter.report(expectation) } - end - - it "stores the expectation" do - expectation = new_unsatisfied_expectation - reporter = Spectator::Expectations::ExpectationReporter.new(true) - begin - reporter.report(expectation) - rescue - # Ignore error, not testing that in this example. - end - reporter.expectations.should contain(expectation) - end - end - end - - context "with raise flag not set" do - context "given a satisfied expectation" do - it "stores the expectation" do - expectation = new_satisfied_expectation - reporter = Spectator::Expectations::ExpectationReporter.new(false) - reporter.report(expectation) - reporter.expectations.should contain(expectation) - end - end - - context "given a unsatisfied expectation" do - it "stores the expectation" do - expectation = new_unsatisfied_expectation - reporter = Spectator::Expectations::ExpectationReporter.new(false) - reporter.report(expectation) - reporter.expectations.should contain(expectation) - end - end - end - end - - describe "#expectations" do - context "with no expectations" do - it "is empty" do - reporter = Spectator::Expectations::ExpectationReporter.new - reporter.expectations.size.should eq(0) - end - end - - context "with multiple expectations" do - it "contains all expectations" do - expectation1 = new_satisfied_expectation - expectation2 = new_unsatisfied_expectation - reporter = Spectator::Expectations::ExpectationReporter.new(false) - begin - reporter.report(expectation1) - reporter.report(expectation2) - rescue - # Ignore errors for this test. - end - reporter.expectations.should contain(expectation1) - reporter.expectations.should contain(expectation2) - end - end - end -end diff --git a/spec/expectations/expectation_spec.cr b/spec/expectations/expectation_spec.cr deleted file mode 100644 index 06540fc..0000000 --- a/spec/expectations/expectation_spec.cr +++ /dev/null @@ -1,51 +0,0 @@ -require "../spec_helper" - -describe Spectator::Expectations::Expectation do - describe "#satisifed?" do - context "with a successful match" do - it "is true" do - value = 42 - matcher = new_matcher(value) - partial = new_partial(value) - match_data = matcher.match(partial.actual) - match_data.matched?.should be_true # Sanity check. - expectation = Spectator::Expectations::Expectation.new(match_data, partial.source) - expectation.satisfied?.should be_true - end - - context "when negated" do - it "is false" do - value = 42 - matcher = new_matcher(value) - partial = new_partial(value) - match_data = matcher.negated_match(partial.actual) - match_data.matched?.should be_false # Sanity check. - expectation = Spectator::Expectations::Expectation.new(match_data, partial.source) - expectation.satisfied?.should be_false - end - end - end - - context "with an unsuccessful match" do - it "is false" do - matcher = new_matcher(42) - partial = new_partial(777) - match_data = matcher.match(partial.actual) - match_data.matched?.should be_false # Sanity check. - expectation = Spectator::Expectations::Expectation.new(match_data, partial.source) - expectation.satisfied?.should be_false - end - - context "when negated" do - it "is true" do - matcher = new_matcher(42) - partial = new_partial(777) - match_data = matcher.negated_match(partial.actual) - match_data.matched?.should be_true # Sanity check. - expectation = Spectator::Expectations::Expectation.new(match_data, partial.source) - expectation.satisfied?.should be_true - end - end - end - end -end diff --git a/spec/failed_result_spec.cr b/spec/failed_result_spec.cr deleted file mode 100644 index 34b557d..0000000 --- a/spec/failed_result_spec.cr +++ /dev/null @@ -1,87 +0,0 @@ -require "./spec_helper" - -def new_failed_result( - example : Spectator::Example? = nil, - elapsed : Time::Span? = nil, - expectations : Spectator::Expectations::ExampleExpectations? = nil, - error : Exception? = nil -) - Spectator::FailedResult.new( - example || FailingExample.create, - elapsed || Time::Span.zero, - expectations || Spectator::Expectations::ExampleExpectations.new(generate_expectations(0, 1)[:expectations]), - error || Exception.new("foobar") - ) -end - -describe Spectator::FailedResult do - describe "#call" do - context "without a block" do - it "invokes #failure on an instance" do - spy = ResultCallSpy.new - new_failed_result.call(spy) - spy.failure?.should be_true - end - - it "returns the value of #failure" do - result = new_failed_result - returned = result.call(ResultCallSpy.new) - returned.should eq(:failure) - end - end - - context "with a block" do - it "invokes #failure on an instance" do - spy = ResultCallSpy.new - new_failed_result.call(spy) { nil } - spy.failure?.should be_true - end - - it "yields itself" do - result = new_failed_result - value = nil.as(Spectator::Result?) - result.call(ResultCallSpy.new) { |r| value = r } - value.should eq(result) - end - - it "returns the value of #failure" do - result = new_failed_result - value = 42 - returned = result.call(ResultCallSpy.new) { value } - returned.should eq(value) - end - end - end - - describe "#example" do - it "is the expected value" do - example = FailingExample.create - result = new_failed_result(example: example) - result.example.should eq(example) - end - end - - describe "#elapsed" do - it "is the expected value" do - elapsed = Time::Span.new(10, 10, 10) - result = new_failed_result(elapsed: elapsed) - result.elapsed.should eq(elapsed) - end - end - - describe "#expectations" do - it "is the expected value" do - expectations = Spectator::Expectations::ExampleExpectations.new(generate_expectations(5, 1)[:expectations]) - result = new_failed_result(expectations: expectations) - result.expectations.should eq(expectations) - end - end - - describe "#error" do - it "is the expected value" do - error = IO::Error.new("oops") - result = new_failed_result(error: error) - result.error.should eq(error) - end - end -end diff --git a/spec/fomatting/color_spec.cr b/spec/fomatting/color_spec.cr deleted file mode 100644 index 406b5db..0000000 --- a/spec/fomatting/color_spec.cr +++ /dev/null @@ -1,93 +0,0 @@ -require "../spec_helper" - -describe Spectator::Formatting::Color do - describe "#success" do - it "includes the input text" do - text = "foobar" - output = Spectator::Formatting::Color.success(text) - output.to_s.should contain(text) - end - - it "prefixes with green markers" do - output = Spectator::Formatting::Color.success("foobar") - output.to_s.should start_with("\e[32m") - end - - it "appends color reset markers" do - output = Spectator::Formatting::Color.success("foobar") - output.to_s.should end_with("\e[0m") - end - end - - describe "#failure" do - it "includes the input text" do - text = "foobar" - output = Spectator::Formatting::Color.failure(text) - output.to_s.should contain(text) - end - - it "prefixes with green markers" do - output = Spectator::Formatting::Color.failure("foobar") - output.to_s.should start_with("\e[31m") - end - - it "appends color reset markers" do - output = Spectator::Formatting::Color.failure("foobar") - output.to_s.should end_with("\e[0m") - end - end - - describe "#error" do - it "includes the input text" do - text = "foobar" - output = Spectator::Formatting::Color.error(text) - output.to_s.should contain(text) - end - - it "prefixes with green markers" do - output = Spectator::Formatting::Color.error("foobar") - output.to_s.should start_with("\e[35m") - end - - it "appends color reset markers" do - output = Spectator::Formatting::Color.error("foobar") - output.to_s.should end_with("\e[0m") - end - end - - describe "#pending" do - it "includes the input text" do - text = "foobar" - output = Spectator::Formatting::Color.pending(text) - output.to_s.should contain(text) - end - - it "prefixes with green markers" do - output = Spectator::Formatting::Color.pending("foobar") - output.to_s.should start_with("\e[33m") - end - - it "appends color reset markers" do - output = Spectator::Formatting::Color.pending("foobar") - output.to_s.should end_with("\e[0m") - end - end - - describe "#comment" do - it "includes the input text" do - text = "foobar" - output = Spectator::Formatting::Color.comment(text) - output.to_s.should contain(text) - end - - it "prefixes with green markers" do - output = Spectator::Formatting::Color.comment("foobar") - output.to_s.should start_with("\e[36m") - end - - it "appends color reset markers" do - output = Spectator::Formatting::Color.comment("foobar") - output.to_s.should end_with("\e[0m") - end - end -end diff --git a/spec/helpers/errored_example.cr b/spec/helpers/errored_example.cr deleted file mode 100644 index aa82617..0000000 --- a/spec/helpers/errored_example.cr +++ /dev/null @@ -1,37 +0,0 @@ -# Example that always raises an exception. -class ErroredExample < Spectator::RunnableExample - # Dummy description. - def what : Symbol | String - "ERROR" - end - - # Dummy source. - def source : ::Spectator::Source - ::Spectator::Source.new(__FILE__, __LINE__) - end - - # Dummy symbolic flag. - def symbolic? : Bool - false - end - - # Dummy instance. - def instance - nil - end - - # Run the example that always produces an error. - private def run_instance - raise "Oops" - end - - # Creates an errored example. - def self.create - hooks = Spectator::ExampleHooks.empty - group = Spectator::RootExampleGroup.new(hooks) - values = Spectator::Internals::SampleValues.empty - new(group, values).tap do |example| - group.children = [example.as(Spectator::ExampleComponent)] - end - end -end diff --git a/spec/helpers/example_hooks_helper.cr b/spec/helpers/example_hooks_helper.cr deleted file mode 100644 index afdf537..0000000 --- a/spec/helpers/example_hooks_helper.cr +++ /dev/null @@ -1,59 +0,0 @@ -# Creates a new `Spectator::ExampleHooks` instance. -# All arguments are optional, -# only specify the sets of hooks that are needed. -# The hooks that aren't specified will be left empty. -def new_hooks( - before_all = [] of ->, - before_each = [] of ->, - after_all = [] of ->, - after_each = [] of ->, - around_each = [] of Proc(Nil) -> -) - Spectator::ExampleHooks.new(before_all, before_each, - after_all, after_each, around_each) -end - -# Creates a new `Spectator::ExampleHooks` instance. -# All arguments are optional, -# only specify a hook for the types that are needed. -# The hooks that aren't specified will be left empty. -def new_hooks( - before_all : Proc(Nil)? = nil, - before_each : Proc(Nil)? = nil, - after_all : Proc(Nil)? = nil, - after_each : Proc(Nil)? = nil, - around_each : Proc(Proc(Nil), Nil)? = nil -) - new_hooks( - before_all ? [before_all] : [] of ->, - before_each ? [before_each] : [] of ->, - after_all ? [after_all] : [] of ->, - after_each ? [after_each] : [] of ->, - around_each ? [around_each] : [] of Proc(Nil) -> - ) -end - -# Creates a new `Spectator::ExampleConditions` instance. -# All arguments are optional, -# only specify the sets of conditions that are needed. -# The conditions that aren't specified will be left empty. -def new_conditions( - pre = [] of ->, - post = [] of -> -) - Spectator::ExampleConditions.new(pre, post) -end - -# Creates a new `Spectator::ExampleConditions` instance. -# All arguments are optional, -# only specify a condition for the types that are needed. -# The conditions that aren't specified will be left empty. -def new_conditions( - pre : Proc(Nil)? = nil, - post : Proc(Nil)? = nil -) - new_conditions( - pre ? [pre] : [] of ->, - post ? [post] : [] of -> - ) -end diff --git a/spec/helpers/expectations_helper.cr b/spec/helpers/expectations_helper.cr deleted file mode 100644 index 0096f84..0000000 --- a/spec/helpers/expectations_helper.cr +++ /dev/null @@ -1,71 +0,0 @@ -# Utility methods for creating expectations, partials, and matchers. - -def new_partial(actual : T, label : String) forall T - test_value = Spectator::TestValue.new(actual, label) - source = Spectator::Source.new(__FILE__, __LINE__) - Spectator::Expectations::ExpectationPartial.new(test_value, source) -end - -def new_partial(actual : T = 123) forall T - test_value = Spectator::TestValue.new(actual) - source = Spectator::Source.new(__FILE__, __LINE__) - Spectator::Expectations::ExpectationPartial.new(test_value, source) -end - -def new_block_partial(label = "BLOCK", &block) - test_block = Spectator::TestBlock.new(block, label) - source = Spectator::Source.new(__FILE__, __LINE__) - Spectator::Expectations::ExpectationPartial.new(test_block, source) -end - -def new_matcher(expected : T, label : String) forall T - test_value = Spectator::TestValue.new(expected, label) - Spectator::Matchers::EqualityMatcher.new(test_value) -end - -def new_matcher(expected : T = 123) forall T - test_value = Spectator::TestValue.new(expected) - Spectator::Matchers::EqualityMatcher.new(test_value) -end - -def new_expectation(expected : ExpectedType = 123, actual : ActualType = 123) forall ExpectedType, ActualType - partial = new_partial(actual, "foo") - matcher = new_matcher(expected, "bar") - match_data = matcher.match(partial.actual) - Spectator::Expectations::Expectation.new(match_data, partial.source) -end - -def new_satisfied_expectation(value : T = 123) forall T - new_expectation(value, value).tap do |expectation| - expectation.satisfied?.should be_true # Sanity check. - end -end - -def new_unsatisfied_expectation(expected : ExpectedType = 123, actual : ActualType = 456) forall ExpectedType, ActualType - new_expectation(expected, actual).tap do |expectation| - expectation.satisfied?.should be_false # Sanity check. - end -end - -def create_expectations(success_count = 1, failure_count = 0) - satisfied = Array.new(success_count) { new_satisfied_expectation } - unsatisfied = Array.new(failure_count) { new_unsatisfied_expectation } - (satisfied + unsatisfied).shuffle -end - -def generate_expectations(success_count = 1, failure_count = 0) - satisfied = Array.new(success_count) { new_satisfied_expectation } - unsatisfied = Array.new(failure_count) { new_unsatisfied_expectation } - expectations = (satisfied + unsatisfied).shuffle - reporter = Spectator::Expectations::ExpectationReporter.new(false) - expectations.each do |expectation| - reporter.report(expectation) - end - {satisfied: satisfied, unsatisfied: unsatisfied, expectations: expectations, reporter: reporter} -end - -def report_expectations(success_count = 1, failure_count = 0) - harness = Spectator::Internals::Harness.current - success_count.times { harness.report_expectation(new_satisfied_expectation) } - failure_count.times { harness.report_expectation(new_unsatisfied_expectation) } -end diff --git a/spec/helpers/failing_example.cr b/spec/helpers/failing_example.cr deleted file mode 100644 index a0e972d..0000000 --- a/spec/helpers/failing_example.cr +++ /dev/null @@ -1,44 +0,0 @@ -# Example that always fails. -class FailingExample < Spectator::RunnableExample - # Dummy description. - def what : Symbol | String - "FAIL" - end - - # Dummy source. - def source : ::Spectator::Source - ::Spectator::Source.new(__FILE__, __LINE__) - end - - # Dummy symbolic flag. - def symbolic? : Bool - false - end - - # Dummy instance. - def instance - nil - end - - # Run the example that always fails. - private def run_instance - report_expectations(0, 1) - end - - # Creates a failing example. - def self.create(hooks = Spectator::ExampleHooks.empty, conditions = Spectator::ExampleConditions.empty) - group = Spectator::RootExampleGroup.new(hooks, conditions) - values = Spectator::Internals::SampleValues.empty - new(group, values).tap do |example| - group.children = [example.as(Spectator::ExampleComponent)] - end - end - - # Creates a group of failing examples. - def self.create_group(count = 5, hooks = Spectator::ExampleHooks.empty, conditions = Spectator::ExampleConditions.empty) - values = Spectator::Internals::SampleValues.empty - Spectator::RootExampleGroup.new(hooks, conditions).tap do |group| - group.children = Array.new(count) { new(group, values).as(Spectator::ExampleComponent) } - end - end -end diff --git a/spec/helpers/matchers_helper.cr b/spec/helpers/matchers_helper.cr deleted file mode 100644 index 2f123df..0000000 --- a/spec/helpers/matchers_helper.cr +++ /dev/null @@ -1,20 +0,0 @@ -# Retrieves a value from match data given its key/label. -def match_data_value_with_key(match_data_values, key) - labeled_value = match_data_values.find { |v| v.label == key } - raise "#{key} is missing" unless labeled_value - labeled_value.value -end - -# Retrieves the string representation and base value -# from a `Spectator::Matchers::PrefixedMatchDataValue` (or similar) -# in the values returned by `Spectator::Matchers::MatchData#values`. -def match_data_value_sans_prefix(match_data_values, key) - prefix = match_data_value_with_key(match_data_values, key) - {to_s: prefix.to_s, value: prefix.value} -end - -# Check whether match data has a value with a specified key/label. -def match_data_has_key?(match_data_values, key) - found = match_data_values.find { |v| v.label == key } - !!found -end diff --git a/spec/helpers/passing_example.cr b/spec/helpers/passing_example.cr deleted file mode 100644 index 6d03aae..0000000 --- a/spec/helpers/passing_example.cr +++ /dev/null @@ -1,42 +0,0 @@ -# Example that always succeeds. -class PassingExample < Spectator::RunnableExample - # Creates the example. - def initialize(group, values, @symbolic = false) - super(group, values) - end - - # Dummy description. - def what : Symbol | String - "PASS" - end - - # Dummy source. - def source : ::Spectator::Source - ::Spectator::Source.new(__FILE__, __LINE__) - end - - # Dummy symbolic flag. - def symbolic? : Bool - @symbolic - end - - # Dummy instance. - def instance - nil - end - - # Run the example that always passes. - # If this doesn't something broke. - private def run_instance - report_expectations(1, 0) - end - - # Creates a passing example. - def self.create(hooks = Spectator::ExampleHooks.empty, conditions = Spectator::ExampleConditions.empty) - group = Spectator::RootExampleGroup.new(hooks, conditions) - values = Spectator::Internals::SampleValues.empty - new(group, values).tap do |example| - group.children = [example.as(Spectator::ExampleComponent)] - end - end -end diff --git a/spec/helpers/result_call_spy.cr b/spec/helpers/result_call_spy.cr deleted file mode 100644 index 30830fb..0000000 --- a/spec/helpers/result_call_spy.cr +++ /dev/null @@ -1,16 +0,0 @@ -# Spy class for testing `Spectator::Result#call`. -class ResultCallSpy - {% for name in %i[success failure error pending] %} - getter? {{name.id}} = false - - def {{name.id}} - @{{name.id}} = true - {{name}} - end - - def {{name.id}}(arg) - @{{name.id}} = true - arg - end - {% end %} -end diff --git a/spec/helpers/spy_example.cr b/spec/helpers/spy_example.cr deleted file mode 100644 index 32b6474..0000000 --- a/spec/helpers/spy_example.cr +++ /dev/null @@ -1,69 +0,0 @@ -# Example that invokes a closure when it is run. -# This is useful for capturing what's going on when an event is running. -class SpyExample < Spectator::RunnableExample - # Dummy description. - def what : Symbol | String - "SPY" - end - - # Dummy source. - def source : ::Spectator::Source - ::Spectator::Source.new(__FILE__, __LINE__) - end - - # Dummy symbolic flag. - def symbolic? : Bool - false - end - - # Dummy instance. - def instance - nil - end - - # Captures the sample values when the example is created. - def initialize(group, @sample_values) - super(group, @sample_values) - end - - # Sample values given to the example. - getter sample_values : Spectator::Internals::SampleValues - - # Harness that was used while running the example. - getter! harness : Spectator::Internals::Harness - - setter block : Proc(Nil)? = nil - - # Method called by the framework to run the example code. - private def run_instance - @harness = Spectator::Internals::Harness.current - if block = @block - block.call - end - end - - # Creates a spy example. - # The block passed to this method will be executed when the example runs. - def self.create(hooks = Spectator::ExampleHooks.empty, conditions = Spectator::ExampleConditions.empty, &block) - group = Spectator::RootExampleGroup.new(hooks, conditions) - values = Spectator::Internals::SampleValues.empty - new(group, values).tap do |example| - group.children = [example.as(Spectator::ExampleComponent)] - example.block = block - end - end - - # Creates a group of spy examples. - # Specify the number of examplese to create and a block to invoke when the example is run. - # The block is given the index of the example in the group. - def self.create_group(count, &block : Int32 -> Nil) - values = Spectator::Internals::SampleValues.empty - Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |group| - group.children = Array.new(count) do |index| - new(group, values).tap do |example| - example.block = block.partial(index) - end.as(Spectator::ExampleComponent) - end - end - end -end diff --git a/spec/helpers/spy_formatter.cr b/spec/helpers/spy_formatter.cr deleted file mode 100644 index 394200e..0000000 --- a/spec/helpers/spy_formatter.cr +++ /dev/null @@ -1,47 +0,0 @@ -# Formatter that doubles as a spy. -# This class tracks calls made to it. -class SpyFormatter < Spectator::Formatting::Formatter - {% for item in [ - {"start_suite", "Spectator::TestSuite"}, - {"start_example", "Spectator::Example"}, - {"end_example", "Spectator::Result"}, - ] %} - {% method_name = item[0].id %} - {% argument_type = item[1].id %} - - # Stores all invocations made to `#{{method_name}}`. - # Each element is an invocation and the value is the argument passed to the method. - getter {{method_name}}_calls = [] of {{argument_type}} - - # Number of times the `#{{method_name}}` method was called. - def {{method_name}}_call_count - @{{method_name}}_calls.size - end - - # Increments `#{{method_name}}_call_count` and stores the argument. - def {{method_name}}(arg : {{argument_type}}) - @all_calls << {{method_name.symbolize}} - @{{method_name}}_calls << arg - end - - {% end %} - - # Stores all invocatiosn made to `#end_suite`. - # Each element is an invocation and the value is the arguments passed to the method. - getter end_suite_calls = [] of NamedTuple(report: Spectator::Report, profile: Spectator::Profile?) - - # Number of times the `#end_suite` method was called. - def end_suite_call_count - @end_suite_calls.size - end - - # Increments `#end_suite_call_count` and stores the arguments. - def end_suite(report, profile) - @all_calls << :end_suite - @end_suite_calls << {report: report, profile: profile} - end - - # Stores the methods that were called and in which order. - # The symbols will be the method name (i.e. `:start_suite`). - getter all_calls = [] of Symbol -end diff --git a/spec/helpers/spy_sut.cr b/spec/helpers/spy_sut.cr deleted file mode 100644 index 2c3feff..0000000 --- a/spec/helpers/spy_sut.cr +++ /dev/null @@ -1,28 +0,0 @@ -# Example system to test that doubles as a spy. -# This class tracks calls made to it. -class SpySUT - {% for item in [ - {"==", "eq"}, - {"!=", "ne"}, - {"<", "lt"}, - {"<=", "le"}, - {">", "gt"}, - {">=", "ge"}, - {"===", "case_eq"}, - {"=~", "match"}, - {"includes?", "includes"}, - ] %} - {% operator = item[0].id %} - {% name = item[1].id %} - - # Number of times the `#{{operator}}` method was called. - getter {{name}}_call_count = 0 - - # Returns true and increments `#{{name}}_call_count`. - def {{operator}}(other : T) forall T - @{{name}}_call_count += 1 - true - end - - {% end %} -end diff --git a/spec/internals/harness_spec.cr b/spec/internals/harness_spec.cr deleted file mode 100644 index 9a360c0..0000000 --- a/spec/internals/harness_spec.cr +++ /dev/null @@ -1,109 +0,0 @@ -require "../spec_helper" - -describe Spectator::Internals::Harness do - describe "#run" do - it "runs an example" do - run_count = 0 - spy = SpyExample.create do - run_count += 1 - end - Spectator::Internals::Harness.run(spy) - run_count.should eq(1) - end - - context "with a passing exmaple" do - it "returns a passing result" do - example = PassingExample.create - result = Spectator::Internals::Harness.run(example) - result.should be_a(Spectator::SuccessfulResult) - end - end - - context "with a failing example" do - it "returns a failing result" do - example = FailingExample.create - result = Spectator::Internals::Harness.run(example) - result.should be_a(Spectator::FailedResult) - end - end - end - - describe "#current" do - it "references the current harness" do - harness = nil.as(Spectator::Internals::Harness?) - spy = SpyExample.create do - harness = Spectator::Internals::Harness.current - end - Spectator::Internals::Harness.run(spy) - harness.should be_a(Spectator::Internals::Harness) - end - end - - describe "#example" do - it "references the current example" do - example = nil.as(Spectator::Example?) - spy = SpyExample.create do - example = Spectator::Internals::Harness.current.example - end - Spectator::Internals::Harness.run(spy) - example.should be(spy) - end - end - - describe "#report_expectation" do - context "with a successful result" do - it "stores the result" do - expectation = new_satisfied_expectation - spy = SpyExample.create do - harness = Spectator::Internals::Harness.current - harness.report_expectation(expectation) - end - result = Spectator::Internals::Harness.run(spy) - result.should be_a(Spectator::SuccessfulResult) - result.as(Spectator::SuccessfulResult).expectations.should contain(expectation) - end - end - - context "with a failed result" do - it "raises an error" do - error = nil.as(Exception?) - spy = SpyExample.create do - harness = Spectator::Internals::Harness.current - begin - harness.report_expectation(new_unsatisfied_expectation) - rescue ex - error = ex - end - end - Spectator::Internals::Harness.run(spy) - error.should be_a(Spectator::ExampleFailed) - end - - it "stores the result" do - expectation = new_unsatisfied_expectation - spy = SpyExample.create do - harness = Spectator::Internals::Harness.current - harness.report_expectation(expectation) - end - result = Spectator::Internals::Harness.run(spy) - result.should be_a(Spectator::FailedResult) - result.as(Spectator::FailedResult).expectations.should contain(expectation) - end - end - end - - describe "#expectation_results" do - it "contains the reported results" do - expectations = [new_satisfied_expectation, new_unsatisfied_expectation] - spy = SpyExample.create do - harness = Spectator::Internals::Harness.current - expectations.each do |expectation| - harness.report_expectation(expectation) - end - end - result = Spectator::Internals::Harness.run(spy) - reported_results = result.as(Spectator::FailedResult).expectations.to_a - (expectations - reported_results).size.should eq(0) - end - end -end diff --git a/spec/internals/sample_values_spec.cr b/spec/internals/sample_values_spec.cr deleted file mode 100644 index 0e0991a..0000000 --- a/spec/internals/sample_values_spec.cr +++ /dev/null @@ -1,117 +0,0 @@ -require "../spec_helper" - -private def add_sample_value(values : Spectator::Internals::SampleValues, - symbol : Symbol, name : String, value : T) forall T - values.add(symbol, name, value) -end - -private def add_sample_value(symbol, name, value : T) forall T - add_sample_value(Spectator::Internals::SampleValues.empty, symbol, name, value) -end - -private def add_sample_value(symbol, value : T) forall T - add_sample_value(symbol, symbol.to_s, value) -end - -describe Spectator::Internals::SampleValues do - describe "#add" do - it "creates a new set" do - original = Spectator::Internals::SampleValues.empty - new_set = original.add(:new, "new", 123) - new_set.should_not eq(original) - end - - it "adds a new value" do - symbol = :new - value = 123 - values = add_sample_value(symbol, value) - values.get_value(symbol, typeof(value)).should eq(value) - end - end - - describe "#get_wrapper" do - it "returns a wrapper for a value" do - symbol = :new - value = 123 - values = add_sample_value(symbol, value) - wrapper = values.get_wrapper(symbol) - wrapper.should be_a(Spectator::Internals::ValueWrapper) - end - - it "returns the correct wrapper" do - symbol = :new - value = 123 - values = add_sample_value(symbol, value) - wrapper = values.get_wrapper(symbol) - wrapper.should be_a(Spectator::Internals::TypedValueWrapper(typeof(value))) - wrapper.as(Spectator::Internals::TypedValueWrapper(typeof(value))).value.should eq(value) - end - - context "with multiple values" do - it "returns the expected value" do - symbols = { - one: 123, - two: 456, - three: 789, - } - values = Spectator::Internals::SampleValues.empty - symbols.each do |symbol, number| - values = add_sample_value(values, symbol, symbol.to_s, number) - end - selected_symbol = :one - selected_number = symbols[selected_symbol] - wrapper = values.get_wrapper(selected_symbol) - wrapper.should be_a(Spectator::Internals::TypedValueWrapper(typeof(selected_number))) - wrapper.as(Spectator::Internals::TypedValueWrapper(typeof(selected_number))).value.should eq(selected_number) - end - end - end - - describe "#get_value" do - it "returns a value" do - symbol = :new - value = 123 - values = add_sample_value(symbol, value) - values.get_value(symbol, typeof(value)).should eq(value) - end - - context "with multiple values" do - it "returns the expected value" do - symbols = { - one: 123, - two: 456, - three: 789, - } - values = Spectator::Internals::SampleValues.empty - symbols.each do |symbol, number| - values = add_sample_value(values, symbol, symbol.to_s, number) - end - selected_symbol = :one - selected_number = symbols[selected_symbol] - value = values.get_value(selected_symbol, typeof(selected_number)) - value.should eq(selected_number) - end - end - end - - describe "#each" do - it "yields each entry" do - symbols = { - one: 123, - two: 456, - three: 789, - } - values = Spectator::Internals::SampleValues.empty - symbols.each do |symbol, number| - values = add_sample_value(values, symbol, symbol.to_s, number) - end - - size = 0 - values.each do |entry| - size += 1 - symbols.keys.map(&.to_s).should contain(entry.name) - end - size.should eq(symbols.size) - end - end -end diff --git a/spec/internals/typed_value_wrapper_spec.cr b/spec/internals/typed_value_wrapper_spec.cr deleted file mode 100644 index ec997b1..0000000 --- a/spec/internals/typed_value_wrapper_spec.cr +++ /dev/null @@ -1,18 +0,0 @@ -require "../spec_helper" - -describe Spectator::Internals::TypedValueWrapper do - describe "#value" do - it "returns the expected value" do - value = 12345 - wrapper = Spectator::Internals::TypedValueWrapper.new(value) - wrapper.value.should eq(value) - end - end - - it "can be cast for storage" do - value = 12345 - wrapper = Spectator::Internals::TypedValueWrapper.new(value).as(Spectator::Internals::ValueWrapper) - typed = wrapper.as(Spectator::Internals::TypedValueWrapper(typeof(value))) - typed.value.should eq(value) - end -end diff --git a/spec/line_example_filter_spec.cr b/spec/line_example_filter_spec.cr deleted file mode 100644 index ca2b1df..0000000 --- a/spec/line_example_filter_spec.cr +++ /dev/null @@ -1,21 +0,0 @@ -require "./spec_helper" - -describe Spectator::LineExampleFilter do - describe "#includes?" do - context "with a matching example" do - it "is true" do - example = PassingExample.create - filter = Spectator::LineExampleFilter.new(example.source.line) - filter.includes?(example).should be_true - end - end - - context "with a non-matching example" do - it "is false" do - example = PassingExample.create - filter = Spectator::LineExampleFilter.new(example.source.line + 5) - filter.includes?(example).should be_false - end - end - end -end diff --git a/spec/name_example_filter_spec.cr b/spec/name_example_filter_spec.cr deleted file mode 100644 index 0fdeefb..0000000 --- a/spec/name_example_filter_spec.cr +++ /dev/null @@ -1,21 +0,0 @@ -require "./spec_helper" - -describe Spectator::NameExampleFilter do - describe "#includes?" do - context "with a matching example" do - it "is true" do - example = PassingExample.create - filter = Spectator::NameExampleFilter.new(example.to_s) - filter.includes?(example).should be_true - end - end - - context "with a non-matching example" do - it "is false" do - example = PassingExample.create - filter = Spectator::NameExampleFilter.new("BOGUS") - filter.includes?(example).should be_false - end - end - end -end diff --git a/spec/nested_example_group_spec.cr b/spec/nested_example_group_spec.cr deleted file mode 100644 index 7d102b0..0000000 --- a/spec/nested_example_group_spec.cr +++ /dev/null @@ -1,1130 +0,0 @@ -require "./spec_helper" - -def new_nested_group(what : Symbol | String = "what", hooks = Spectator::ExampleHooks.empty, conditions = Spectator::ExampleConditions.empty, parent : Spectator::ExampleGroup? = nil) - parent ||= Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - Spectator::NestedExampleGroup.new(what, parent, hooks, conditions).tap do |group| - parent.children = [group.as(Spectator::ExampleComponent)] - group.children = [] of Spectator::ExampleComponent - end -end - -def nested_group_with_examples(example_count = 5) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - examples = [] of Spectator::Example - group.children = Array(Spectator::ExampleComponent).new(example_count) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - {group, examples} -end - -def nested_group_with_sub_groups(sub_group_count = 5, example_count = 5) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - examples = [] of Spectator::Example - group.children = Array(Spectator::ExampleComponent).new(sub_group_count) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = Array(Spectator::ExampleComponent).new(example_count) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - end - end - {group, examples} -end - -def complex_nested_group - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - examples = [] of Spectator::Example - group.children = Array(Spectator::ExampleComponent).new(10) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group1| - sub_group1.children = Array(Spectator::ExampleComponent).new(10) do |j| - if i % 2 == 0 - PassingExample.new(sub_group1, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - else - Spectator::NestedExampleGroup.new(j.to_s, sub_group1, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group2| - sub_group2.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(sub_group2, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - end - end - end - end - end - end - {group, examples} -end - -describe Spectator::NestedExampleGroup do - describe "#what" do - it "is the expected value" do - what = "foobar" - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new(what, root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.what.should eq(what) - end - end - - describe "#parent" do - it "is the expected value" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.parent.should eq(root) - end - end - - describe "#run_before_hooks" do - it "runs a single before_all hook" do - called = false - hooks = new_hooks(before_all: ->{ called = true; nil }) - group = new_nested_group(hooks: hooks) - group.run_before_hooks - called.should be_true - end - - it "runs a single before_each hook" do - called = false - hooks = new_hooks(before_each: ->{ called = true; nil }) - group = new_nested_group(hooks: hooks) - group.run_before_hooks - called.should be_true - end - - it "runs multiple before_all hooks" do - call_count = 0 - hooks = new_hooks(before_all: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - group = new_nested_group(hooks: hooks) - group.run_before_hooks - call_count.should eq(6) - end - - it "runs multiple before_each hooks" do - call_count = 0 - hooks = new_hooks(before_each: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - group = new_nested_group(hooks: hooks) - group.run_before_hooks - call_count.should eq(6) - end - - it "runs hooks in the correct order" do - calls = [] of Symbol - hooks = new_hooks(before_all: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ], - before_each: [ - ->{ calls << :d; nil }, - ->{ calls << :e; nil }, - ->{ calls << :f; nil }, - ]) - group = new_nested_group(hooks: hooks) - group.run_before_hooks - calls.should eq(%i[a b c d e f]) - end - - it "runs the parent before_all hooks" do - called = false - hooks = new_hooks(before_all: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = new_nested_group(parent: root) - group.run_before_hooks - called.should be_true - end - - it "runs the parent before_each hooks" do - called = false - hooks = new_hooks(before_each: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = new_nested_group(parent: root) - group.run_before_hooks - called.should be_true - end - - it "runs the parent before_all hooks first" do - calls = [] of Symbol - root_hooks = new_hooks(before_all: ->{ calls << :a; nil }) - group_hooks = new_hooks(before_all: ->{ calls << :b; nil }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = new_nested_group(hooks: group_hooks, parent: root) - group.run_before_hooks - calls.should eq(%i[a b]) - end - - it "runs the parent before_each hooks first" do - calls = [] of Symbol - root_hooks = new_hooks(before_each: ->{ calls << :a; nil }) - group_hooks = new_hooks(before_each: ->{ calls << :b; nil }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = new_nested_group(hooks: group_hooks, parent: root) - group.run_before_hooks - calls.should eq(%i[a b]) - end - - it "runs the before_all hooks once" do - call_count = 0 - hooks = new_hooks(before_all: ->{ call_count += 1; nil }) - group = new_nested_group(hooks: hooks) - 2.times { group.run_before_hooks } - call_count.should eq(1) - end - - it "runs the before_each hooks multiple times" do - call_count = 0 - hooks = new_hooks(before_each: ->{ call_count += 1; nil }) - group = new_nested_group(hooks: hooks) - 2.times { group.run_before_hooks } - call_count.should eq(2) - end - end - - describe "#run_after_hooks" do - # No children are used for most of these examples. - # That's because `[].all?` is always true. - # Which means that all examples are considered finished, since there are none. - it "runs a single after_all hook" do - called = false - hooks = new_hooks(after_all: ->{ called = true; nil }) - group = new_nested_group(hooks: hooks) - group.run_after_hooks - called.should be_true - end - - it "runs multiple after_all hooks" do - call_count = 0 - hooks = new_hooks(after_all: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - group = new_nested_group(hooks: hooks) - group.run_after_hooks - call_count.should eq(6) - end - - it "runs hooks in the correct order" do - calls = [] of Symbol - hooks = new_hooks(after_each: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ], - after_all: [ - ->{ calls << :d; nil }, - ->{ calls << :e; nil }, - ->{ calls << :f; nil }, - ]) - group = new_nested_group(hooks: hooks) - group.run_after_hooks - calls.should eq(%i[a b c d e f]) - end - - it "runs the parent after_all hooks" do - called = false - hooks = new_hooks(after_all: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = new_nested_group(parent: root) - group.run_after_hooks - called.should be_true - end - - it "runs the parent after_each hooks" do - called = false - hooks = new_hooks(after_each: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = new_nested_group(parent: root) - group.run_after_hooks - called.should be_true - end - - it "runs the parent after_all hooks last" do - calls = [] of Symbol - root_hooks = new_hooks(after_all: ->{ calls << :a; nil }) - group_hooks = new_hooks(after_all: ->{ calls << :b; nil }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = new_nested_group(hooks: group_hooks, parent: root) - group.run_after_hooks - calls.should eq(%i[b a]) - end - - it "runs the parent after_each hooks last" do - calls = [] of Symbol - root_hooks = new_hooks(after_each: ->{ calls << :a; nil }) - group_hooks = new_hooks(after_each: ->{ calls << :b; nil }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = new_nested_group(hooks: group_hooks, parent: root) - group.run_after_hooks - calls.should eq(%i[b a]) - end - - it "runs the after_all hooks once" do - call_count = 0 - hooks = new_hooks(after_all: ->{ call_count += 1; nil }) - group = new_nested_group(hooks: hooks) - 2.times { group.run_after_hooks } - call_count.should eq(1) - end - - it "runs the after_each hooks multiple times" do - call_count = 0 - hooks = new_hooks(after_each: ->{ call_count += 1; nil }) - group = new_nested_group(hooks: hooks) - 2.times { group.run_after_hooks } - call_count.should eq(2) - end - - context "with no examples finished" do - it "doesn't run the after_all hooks" do - called = false - hooks = new_hooks(after_all: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - group.run_after_hooks - called.should be_false - end - - it "runs the after_each hooks" do - called = false - hooks = new_hooks(after_each: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - group.run_after_hooks - called.should be_true - end - - it "doesn't run the parent after_all hooks" do - called = false - hooks = new_hooks(after_all: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - group.run_after_hooks - called.should be_false - end - - it "runs the parent after_each hooks" do - called = false - hooks = new_hooks(after_each: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - group.run_after_hooks - called.should be_true - end - end - - context "with some examples finished" do - it "doesn't run the after_all hooks" do - called = false - examples = [] of Spectator::Example - hooks = new_hooks(after_all: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - examples.each_with_index do |example, index| - Spectator::Internals::Harness.run(example) if index % 2 == 0 - end - group.run_after_hooks - called.should be_false - end - - it "runs the after_each hooks" do - called = false - examples = [] of Spectator::Example - hooks = new_hooks(after_each: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - examples.each_with_index do |example, index| - Spectator::Internals::Harness.run(example) if index % 2 == 0 - end - group.run_after_hooks - called.should be_true - end - - it "doesn't run the parent after_all hooks" do - called = false - examples = [] of Spectator::Example - hooks = new_hooks(after_all: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - examples.each_with_index do |example, index| - Spectator::Internals::Harness.run(example) if index % 2 == 0 - end - group.run_after_hooks - called.should be_false - end - - it "runs the parent after_each hooks" do - called = false - examples = [] of Spectator::Example - hooks = new_hooks(after_each: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - examples.each_with_index do |example, index| - Spectator::Internals::Harness.run(example) if index % 2 == 0 - end - group.run_after_hooks - called.should be_true - end - end - end - - describe "#wrap_around_each_hooks" do - it "wraps the block" do - called = false - wrapper = new_nested_group.wrap_around_each_hooks do - called = true - end - wrapper.call - called.should be_true - end - - it "wraps a proc" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { called = true; proc.call }) - wrapper = new_nested_group(hooks: hooks).wrap_around_each_hooks { } - wrapper.call - called.should be_true - end - - it "wraps multiple procs" do - call_count = 0 - hooks = new_hooks(around_each: [ - ->(proc : ->) { call_count += 1; proc.call }, - ->(proc : ->) { call_count += 2; proc.call }, - ->(proc : ->) { call_count += 3; proc.call }, - ]) - wrapper = new_nested_group(hooks: hooks).wrap_around_each_hooks { } - wrapper.call - call_count.should eq(6) - end - - it "wraps procs in the correct order" do - calls = [] of Symbol - hooks = new_hooks(around_each: [ - ->(proc : ->) { calls << :a; proc.call }, - ->(proc : ->) { calls << :b; proc.call }, - ->(proc : ->) { calls << :c; proc.call }, - ]) - wrapper = new_nested_group(hooks: hooks).wrap_around_each_hooks { } - wrapper.call - calls.should eq(%i[a b c]) - end - - it "wraps the parent hooks" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { called = true; proc.call }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - wrapper = new_nested_group(parent: root).wrap_around_each_hooks { } - wrapper.call - called.should be_true - end - - it "wraps the parent hooks so they are outermost" do - calls = [] of Symbol - root_hooks = new_hooks(around_each: ->(proc : ->) { calls << :a; proc.call }) - group_hooks = new_hooks(around_each: ->(proc : ->) { calls << :b; proc.call }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = new_nested_group(hooks: group_hooks, parent: root) - wrapper = group.wrap_around_each_hooks { } - wrapper.call - calls.should eq(%i[a b]) - end - end - - describe "#run_pre_conditions" do - it "runs a single pre-condition" do - called = false - conditions = new_conditions(pre: ->{ called = true; nil }) - group = new_nested_group(conditions: conditions) - group.run_pre_conditions - called.should be_true - end - - it "runs multiple pre-conditions" do - call_count = 0 - conditions = new_conditions(pre: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - group = new_nested_group(conditions: conditions) - group.run_pre_conditions - call_count.should eq(6) - end - - it "runs conditions in the correct order" do - calls = [] of Symbol - conditions = new_conditions(pre: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - group = new_nested_group(conditions: conditions) - group.run_pre_conditions - calls.should eq(%i[a b c]) - end - - it "runs the parent conditions" do - called = false - conditions = new_conditions(pre: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = new_nested_group(parent: root) - group.run_pre_conditions - called.should be_true - end - - it "runs the parent conditions first" do - calls = [] of Symbol - root_conditions = new_conditions(pre: ->{ calls << :a; nil }) - group_conditions = new_conditions(pre: ->{ calls << :b; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = new_nested_group(conditions: group_conditions, parent: root) - group.run_pre_conditions - calls.should eq(%i[a b]) - end - - it "runs the conditions multiple times" do - call_count = 0 - conditions = new_conditions(pre: ->{ call_count += 1; nil }) - group = new_nested_group(conditions: conditions) - 2.times { group.run_pre_conditions } - call_count.should eq(2) - end - end - - describe "#run_post_conditions" do - it "runs a single condition" do - called = false - conditions = new_conditions(post: ->{ called = true; nil }) - group = new_nested_group(conditions: conditions) - group.run_post_conditions - called.should be_true - end - - it "runs multiple conditions" do - call_count = 0 - conditions = new_conditions(post: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - group = new_nested_group(conditions: conditions) - group.run_post_conditions - call_count.should eq(6) - end - - it "runs conditions in the correct order" do - calls = [] of Symbol - conditions = new_conditions(post: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - group = new_nested_group(conditions: conditions) - group.run_post_conditions - calls.should eq(%i[a b c]) - end - - it "runs the parent conditions" do - called = false - conditions = new_conditions(post: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = new_nested_group(parent: root) - group.run_post_conditions - called.should be_true - end - - it "runs the parent conditions last" do - calls = [] of Symbol - root_conditions = new_conditions(post: ->{ calls << :a; nil }) - group_conditions = new_conditions(post: ->{ calls << :b; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = new_nested_group(conditions: group_conditions, parent: root) - group.run_post_conditions - calls.should eq(%i[b a]) - end - - it "runs the conditions multiple times" do - call_count = 0 - conditions = new_conditions(post: ->{ call_count += 1; nil }) - group = new_nested_group(conditions: conditions) - 2.times { group.run_post_conditions } - call_count.should eq(2) - end - end - - describe "#to_s" do - it "contains #what" do - group = new_nested_group - group.to_s.should contain(group.what.to_s) - end - - it "contains the parent's #to_s" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - parent = Spectator::NestedExampleGroup.new("PARENT", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("GROUP", parent, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [parent.as(Spectator::ExampleComponent)] - parent.children = [group.as(Spectator::ExampleComponent)] - group.children = [] of Spectator::ExampleComponent - group.to_s.should contain(parent.to_s) - end - - context "when #symbolic? is true" do - context "and the parent group is symbolic" do - it "omits the space" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - parent = Spectator::NestedExampleGroup.new(:Parent, root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new(:"#foo", parent, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [parent.as(Spectator::ExampleComponent)] - parent.children = [group.as(Spectator::ExampleComponent)] - group.children = [] of Spectator::ExampleComponent - parent.symbolic?.should be_true - group.symbolic?.should be_true - group.to_s.should_not contain(' ') - end - end - - context "and the parent group isn't symbolic" do - it "inserts a space" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - parent = Spectator::NestedExampleGroup.new("PARENT", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new(:"#foo", parent, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [parent.as(Spectator::ExampleComponent)] - parent.children = [group.as(Spectator::ExampleComponent)] - group.children = [] of Spectator::ExampleComponent - parent.symbolic?.should be_false - group.symbolic?.should be_true - group.to_s.should contain(' ') - end - end - end - - context "when #symbolic? is false" do - context "and the parent group is symbolic" do - it "inserts a space" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - parent = Spectator::NestedExampleGroup.new(:Parent, root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("GROUP", parent, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [parent.as(Spectator::ExampleComponent)] - parent.children = [group.as(Spectator::ExampleComponent)] - group.children = [] of Spectator::ExampleComponent - parent.symbolic?.should be_true - group.symbolic?.should be_false - group.to_s.should contain(' ') - end - end - - context "and the parent group isn't symbolic" do - it "inserts a space" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - parent = Spectator::NestedExampleGroup.new("PARENT", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("GROUP", parent, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [parent.as(Spectator::ExampleComponent)] - parent.children = [group.as(Spectator::ExampleComponent)] - group.children = [] of Spectator::ExampleComponent - parent.symbolic?.should be_false - group.symbolic?.should be_false - group.to_s.should contain(' ') - end - end - end - - context "when the parent group is root" do - it "omits the space" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("GROUP", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = [] of Spectator::ExampleComponent - group.to_s.should_not contain(' ') - end - end - end - - describe "#children" do - it "raises an error when not set" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - expect_raises(Exception) { group.children } - end - - it "returns the expected set" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - group.children = children - group.children.should eq(children) - end - end - - describe "#children=" do - it "raises an error trying to reset" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - group.children = children - expect_raises(Exception) { group.children = children } - end - end - - describe "#each" do - it "yields each child" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = [] of Spectator::ExampleComponent - end - end - end - group.to_a.should eq(group.children) - end - - it "doesn't yield children of children" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(sub_group, Spectator::Internals::SampleValues.empty) - end - end - end - end - group.to_a.should eq(group.children) - end - end - - describe "#each : Iterator" do - it "iterates over each child" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = [] of Spectator::ExampleComponent - end - end - end - group.each.to_a.should eq(group.children) - end - - it "doesn't iterate over children of children" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(sub_group, Spectator::Internals::SampleValues.empty) - end - end - end - end - group.each.to_a.should eq(group.children) - end - end - - describe "#example_count" do - context "with no examples" do - it "is zero" do - new_nested_group.example_count.should eq(0) - end - end - - context "with empty sub-groups" do - it "is zero" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - end - group.example_count.should eq(0) - end - end - - context "with direct descendant examples" do - it "equals the number of examples" do - example_count = 10 - group, _ = nested_group_with_examples(example_count) - group.example_count.should eq(example_count) - end - end - - context "with examples sub-groups" do - it "equals the total number of examples" do - sub_group_count = 3 - example_count = 10 - group, _ = nested_group_with_sub_groups(sub_group_count, example_count) - group.example_count.should eq(sub_group_count * example_count) - end - end - - context "with examples at all levels" do - it "equals the total number of examples" do - group, examples = complex_nested_group - group.example_count.should eq(examples.size) - end - end - end - - describe "#[]" do - context "when empty" do - it "raises an error" do - group = new_nested_group - expect_raises(IndexError) { group[0] } - end - end - - context "with direct descendant examples" do - context "given 0" do - it "returns the first example" do - group, examples = nested_group_with_examples - group[0].should eq(examples.first) - end - end - - context "given -1" do - it "returns the last example" do - group, examples = nested_group_with_examples - group[-1].should eq(examples.last) - end - end - - context "given an in-bounds positive index" do - it "returns the expected example" do - group, examples = nested_group_with_examples(10) - group[3].should eq(examples[3]) - end - end - - context "given an in-bounds negative index" do - it "returns the expected example" do - group, examples = nested_group_with_examples(10) - group[-3].should eq(examples[-3]) - end - end - - context "an out-of-bounds positive index" do - it "raises an index error" do - group, _ = nested_group_with_examples(10) - expect_raises(IndexError) { group[15] } - end - - it "handles off-by-one" do - group, _ = nested_group_with_examples(10) - expect_raises(IndexError) { group[10] } - end - end - - context "an out-of-bounds negative index" do - it "raises an index error" do - group, _ = nested_group_with_examples(10) - expect_raises(IndexError) { group[-15] } - end - - it "handles off-by-one" do - group, _ = nested_group_with_examples(10) - expect_raises(IndexError) { group[-11] } - end - end - end - - context "with examples only in sub-groups" do - context "given 0" do - it "returns the first example" do - group, examples = nested_group_with_sub_groups - group[0].should eq(examples.first) - end - end - - context "given -1" do - it "returns the last example" do - group, examples = nested_group_with_sub_groups - group[-1].should eq(examples.last) - end - end - - context "given an in-bounds positive index" do - it "returns the expected example" do - group, examples = nested_group_with_sub_groups(10, 2) - group[6].should eq(examples[6]) - end - end - - context "given an in-bounds negative index" do - it "returns the expected example" do - group, examples = nested_group_with_sub_groups(10, 2) - group[-6].should eq(examples[-6]) - end - end - - context "an out-of-bounds positive index" do - it "raises an index error" do - group, _ = nested_group_with_sub_groups(10, 2) - expect_raises(IndexError) { group[25] } - end - - it "handles off-by-one" do - group, _ = nested_group_with_sub_groups(10, 2) - expect_raises(IndexError) { group[20] } - end - end - - context "an out-of-bounds negative index" do - it "raises an index error" do - group, _ = nested_group_with_sub_groups(10, 2) - expect_raises(IndexError) { group[-25] } - end - - it "handles off-by-one" do - group, _ = nested_group_with_sub_groups(10, 2) - expect_raises(IndexError) { group[-21] } - end - end - end - - context "with examples at all levels" do - context "given 0" do - it "returns the first example" do - group, examples = complex_nested_group - group[0].should eq(examples.first) - end - end - - context "given -1" do - it "returns the last example" do - group, examples = complex_nested_group - group[-1].should eq(examples.last) - end - end - - context "given an in-bounds positive index" do - it "returns the expected example" do - group, examples = complex_nested_group - group[42].should eq(examples[42]) - end - end - - context "given an in-bounds negative index" do - it "returns the expected example" do - group, examples = complex_nested_group - group[-42].should eq(examples[-42]) - end - end - - context "an out-of-bounds positive index" do - it "raises an index error" do - group, examples = complex_nested_group - expect_raises(IndexError) { group[examples.size + 5] } - end - - it "handles off-by-one" do - group, examples = complex_nested_group - expect_raises(IndexError) { group[examples.size] } - end - end - - context "an out-of-bounds negative index" do - it "raises an index error" do - group, examples = complex_nested_group - expect_raises(IndexError) { group[-examples.size - 5] } - end - - it "handles off-by-one" do - group, examples = complex_nested_group - expect_raises(IndexError) { group[-examples.size - 1] } - end - end - end - - context "with only sub-groups and no examples" do - it "raises an index error" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = [] of Spectator::ExampleComponent - end - end - expect_raises(IndexError) { group[0] } - end - end - end - - describe "#finished?" do - context "with no children" do - it "is true" do - new_nested_group.finished?.should be_true - end - end - - context "with all unfinished children" do - it "is false" do - group, _ = nested_group_with_examples - group.finished?.should be_false - end - end - - context "with some finished children" do - it "is false" do - group, examples = nested_group_with_examples - examples.each_with_index do |example, index| - Spectator::Internals::Harness.run(example) if index % 2 == 0 - end - group.finished?.should be_false - end - end - - context "with all finished children" do - it "is true" do - group, examples = nested_group_with_examples - examples.each do |example| - Spectator::Internals::Harness.run(example) - end - group.finished?.should be_true - end - end - - context "with a sub-group" do - context "with no children" do - it "is true" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = [] of Spectator::ExampleComponent - end - end - group.finished?.should be_true - end - end - - context "with all unfinished children" do - it "is false" do - group, _ = nested_group_with_sub_groups - group.finished?.should be_false - end - end - - context "with some finished children" do - it "is false" do - group, examples = nested_group_with_sub_groups - examples.each_with_index do |example, index| - Spectator::Internals::Harness.run(example) if index % 2 == 0 - end - group.finished?.should be_false - end - end - - context "with all finished children" do - it "is true" do - group, examples = nested_group_with_sub_groups - examples.each do |example| - Spectator::Internals::Harness.run(example) - end - group.finished?.should be_true - end - end - end - end - - describe "#symbolic?" do - context "when 'what' is a Symbol" do - it "is true" do - new_nested_group(:What).symbolic?.should be_true - end - end - - context "when 'what' is a String" do - it "is false" do - new_nested_group("what").symbolic?.should be_false - end - end - end -end diff --git a/spec/null_example_filter_spec.cr b/spec/null_example_filter_spec.cr deleted file mode 100644 index 42722a6..0000000 --- a/spec/null_example_filter_spec.cr +++ /dev/null @@ -1,11 +0,0 @@ -require "./spec_helper" - -describe Spectator::NullExampleFilter do - describe "#includes?" do - it "returns true" do - example = PassingExample.create - filter = Spectator::NullExampleFilter.new - filter.includes?(example).should be_true - end - end -end diff --git a/spec/pending_example_spec.cr b/spec/pending_example_spec.cr deleted file mode 100644 index 8ed23df..0000000 --- a/spec/pending_example_spec.cr +++ /dev/null @@ -1,122 +0,0 @@ -require "./spec_helper" - -class ConcretePendingExample < Spectator::PendingExample - def what : Symbol | String - "PENDING_TEST_EXAMPLE" - end - - def source : ::Spectator::Source - ::Spectator::Source.new(__FILE__, __LINE__) - end - - def symbolic? : Bool - false - end - - def instance - nil - end -end - -def new_pending_example(group : Spectator::ExampleGroup? = nil) - ConcretePendingExample.new(group || new_root_group, Spectator::Internals::SampleValues.empty) -end - -def new_pending_example_with_hooks(hooks) - group = new_root_group(hooks) - new_pending_example(group) -end - -describe Spectator::PendingExample do - describe "#run" do - it "returns a pending result" do - new_pending_example.run.should be_a(Spectator::PendingResult) - end - - it "doesn't run before_all hooks" do - called = false - hooks = new_hooks(before_all: ->{ called = true; nil }) - example = new_pending_example_with_hooks(hooks) - Spectator::Internals::Harness.run(example) - called.should be_false - end - - it "doesn't run before_each hooks" do - called = false - hooks = new_hooks(before_each: ->{ called = true; nil }) - example = new_pending_example_with_hooks(hooks) - Spectator::Internals::Harness.run(example) - called.should be_false - end - - it "doesn't run after_all hooks" do - called = false - hooks = new_hooks(after_all: ->{ called = true; nil }) - example = new_pending_example_with_hooks(hooks) - Spectator::Internals::Harness.run(example) - called.should be_false - end - - it "doesn't run after_each hooks" do - called = false - hooks = new_hooks(after_each: ->{ called = true; nil }) - example = new_pending_example_with_hooks(hooks) - Spectator::Internals::Harness.run(example) - called.should be_false - end - - it "doesn't run around_each hooks" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { called = true; proc.call }) - example = new_pending_example_with_hooks(hooks) - Spectator::Internals::Harness.run(example) - called.should be_false - end - end - - describe "#finished?" do - it "is initially false" do - new_pending_example.finished?.should be_false - end - - it "is true after #run is called" do - example = new_pending_example - Spectator::Internals::Harness.run(example) - example.finished?.should be_true - end - end - - describe "#group" do - it "is the expected value" do - group = new_root_group - example = new_pending_example(group) - example.group.should eq(group) - end - end - - describe "#example_count" do - it "is one" do - new_pending_example.example_count.should eq(1) - end - end - - describe "#[]" do - it "returns self" do - example = new_pending_example - example[0].should eq(example) - end - end - - describe "#to_s" do - it "contains #what" do - example = new_pending_example - example.to_s.should contain(example.what) - end - - it "contains the group's #what" do - group = new_nested_group - example = new_pending_example(group) - example.to_s.should contain(group.what.to_s) - end - end -end diff --git a/spec/pending_result_spec.cr b/spec/pending_result_spec.cr deleted file mode 100644 index c83b1fb..0000000 --- a/spec/pending_result_spec.cr +++ /dev/null @@ -1,53 +0,0 @@ -require "./spec_helper" - -def new_pending_result(example : Spectator::Example? = nil) - Spectator::PendingResult.new(example || FailingExample.create) -end - -describe Spectator::PendingResult do - describe "#call" do - context "without a block" do - it "invokes #pending on an instance" do - spy = ResultCallSpy.new - new_pending_result.call(spy) - spy.pending?.should be_true - end - - it "returns the value of #pending" do - result = new_pending_result - returned = result.call(ResultCallSpy.new) - returned.should eq(:pending) - end - end - - context "with a block" do - it "invokes #pending on an instance" do - spy = ResultCallSpy.new - new_pending_result.call(spy) { nil } - spy.pending?.should be_true - end - - it "yields itself" do - result = new_pending_result - value = nil.as(Spectator::Result?) - result.call(ResultCallSpy.new) { |r| value = r } - value.should eq(result) - end - - it "returns the value of #pending" do - result = new_pending_result - value = 42 - returned = result.call(ResultCallSpy.new) { value } - returned.should eq(value) - end - end - end - - describe "#example" do - it "is the expected value" do - example = PassingExample.create - result = new_pending_result(example: example) - result.example.should eq(example) - end - end -end diff --git a/spec/report_spec.cr b/spec/report_spec.cr deleted file mode 100644 index 5566674..0000000 --- a/spec/report_spec.cr +++ /dev/null @@ -1,237 +0,0 @@ -require "./spec_helper" - -def new_passing_result - example = PassingExample.create - elapsed = Time::Span.new(nanoseconds: 1_000_000) - expectations = Spectator::Expectations::ExampleExpectations.new([] of Spectator::Expectations::Expectation) - Spectator::SuccessfulResult.new(example, elapsed, expectations) -end - -def new_failure_result(result_type : Spectator::Result.class = Spectator::FailedResult) - example = FailingExample.create # Doesn't matter what type of example is used here. - elapsed = Time::Span.new(nanoseconds: 1_000_000) - expectations = Spectator::Expectations::ExampleExpectations.new([] of Spectator::Expectations::Expectation) - error = Exception.new("foobar") - result_type.new(example, elapsed, expectations, error) -end - -def new_results(successful_count = 5, failed_count = 5, error_count = 5, pending_count = 5) - total = successful_count + failed_count + error_count + pending_count - results = Array(Spectator::Result).new(total) - successful_count.times { results << new_passing_result } - failed_count.times { results << new_failure_result } - error_count.times { results << new_failure_result(Spectator::ErroredResult) } - pending_count.times { results << new_pending_result } - results -end - -def new_report(successful_count = 5, failed_count = 5, error_count = 5, pending_count = 5, overhead_time = 1_000_000i64, fail_blank = false) - results = new_results(successful_count, failed_count, error_count, pending_count) - example_runtime = results.compact_map(&.as?(Spectator::FinishedResult)).sum(&.elapsed) - total_runtime = example_runtime + Time::Span.new(nanoseconds: overhead_time) - Spectator::Report.new(results, total_runtime, fail_blank: fail_blank) -end - -describe Spectator::Report do - describe "#initialize(results)" do - describe "#runtime" do - it "is the sum of all results' runtimes" do - results = new_results - runtime = results.compact_map(&.as?(Spectator::FinishedResult)).sum(&.elapsed) - report = Spectator::Report.new(results) - report.runtime.should eq(runtime) - end - end - end - - describe "#each" do - it "yields all results" do - results = new_results - report = Spectator::Report.new(results) - # The `#each` method is tested through `Enumerable#to_a`. - report.to_a.should eq(results) - end - end - - describe "#runtime" do - it "is the expected value" do - span = Time::Span.new(10, 10, 10) - report = Spectator::Report.new([] of Spectator::Result, span) - report.runtime.should eq(span) - end - end - - describe "#example_count" do - it "is the expected value" do - report = new_report(5, 4, 3, 2) - report.example_count.should eq(14) - end - end - - describe "#examples_ran" do - it "is the number of non-skipped examples" do - report = new_report(5, 4, 3, 2) - report.examples_ran.should eq(12) - end - end - - describe "#successful_count" do - it "is the expected value" do - report = new_report(5, 4, 3, 2) - report.successful_count.should eq(5) - end - end - - describe "#failed_count" do - it "is the expected value" do - report = new_report(5, 4, 3, 2) - report.failed_count.should eq(7) - end - end - - describe "#error_count" do - it "is the expected value" do - report = new_report(5, 4, 3, 2) - report.error_count.should eq(3) - end - end - - describe "#pending_count" do - it "is the expected value" do - report = new_report(5, 4, 3, 2) - report.pending_count.should eq(2) - end - end - - describe "#remaining_count" do - it "is the expected value" do - results = [] of Spectator::Result - remaining = 5 - report = Spectator::Report.new(results, Time::Span.zero, remaining) - report.remaining_count.should eq(remaining) - end - end - - describe "#failed?" do - context "with a failed test suite" do - it "is true" do - report = new_report(5, 4, 3, 2) - report.failed?.should be_true - end - end - - context "with a passing test suite" do - it "is false" do - report = new_report(5, 0, 0, 0) - report.failed?.should be_false - end - end - - context "with fail-blank enabled" do - context "when no tests run" do - it "is true" do - report = new_report(0, 0, 0, 5, fail_blank: true) - report.failed?.should be_true - end - end - - context "when tests run" do - context "and there are failures" do - it "is true" do - report = new_report(5, 4, 3, 2, fail_blank: true) - report.failed?.should be_true - end - end - - context "and there are no failures" do - it "is false" do - report = new_report(5, 0, 0, 2, fail_blank: true) - report.failed?.should be_false - end - end - end - end - end - - describe "#remaining?" do - context "with remaining tests" do - it "is true" do - results = [] of Spectator::Result - report = Spectator::Report.new(results, Time::Span.zero, 5) - report.remaining?.should be_true - end - end - - context "without remaining tests" do - it "is false" do - results = [] of Spectator::Result - report = Spectator::Report.new(results, Time::Span.zero, 0) - report.remaining?.should be_false - end - end - end - - describe "#failures" do - it "returns the expected results" do - results = Array.new(5) { new_failure_result.as(Spectator::Result) } - report = Spectator::Report.new(results, Time::Span.zero) - report.failures.to_a.should eq(results) - end - - it "includes errors" do - results = Array(Spectator::Result).new(5) do |index| - if index.odd? - new_failure_result - else - new_failure_result(Spectator::ErroredResult) - end - end - report = Spectator::Report.new(results, Time::Span.zero) - report.failures.to_a.should eq(results) - end - end - - describe "#errors" do - it "returns the expected results" do - results = Array.new(5) { new_failure_result(Spectator::ErroredResult).as(Spectator::Result) } - report = Spectator::Report.new(results, Time::Span.zero) - report.errors.to_a.should eq(results) - end - - it "does not include failures" do - results = Array(Spectator::Result).new(5) do |index| - if index.odd? - new_failure_result - else - new_failure_result(Spectator::ErroredResult) - end - end - report = Spectator::Report.new(results, Time::Span.zero) - errors_only = results.select(&.is_a?(Spectator::ErroredResult)) - report.errors.to_a.should eq(errors_only) - end - end - - describe "#example_runtime" do - it "is the sum of all example run-times" do - passing_results = Array.new(5) { new_passing_result } - runtime = passing_results.sum(&.elapsed) - results = passing_results.map(&.as(Spectator::Result)) - total_runtime = runtime + Time::Span.new(nanoseconds: 1_234_567) - report = Spectator::Report.new(results, total_runtime) - report.example_runtime.should eq(runtime) - end - end - - describe "#overhead_time" do - it "is the difference between total runtime and the sum of all example run-times" do - passing_results = Array.new(5) { new_passing_result } - runtime = passing_results.sum(&.elapsed) - results = passing_results.map(&.as(Spectator::Result)) - overhead = Time::Span.new(nanoseconds: 1_234_567) - total_runtime = runtime + overhead - report = Spectator::Report.new(results, total_runtime) - report.overhead_time.should eq(overhead) - end - end -end diff --git a/spec/root_example_group_spec.cr b/spec/root_example_group_spec.cr deleted file mode 100644 index d69c80b..0000000 --- a/spec/root_example_group_spec.cr +++ /dev/null @@ -1,810 +0,0 @@ -require "./spec_helper" - -def new_root_group(hooks = Spectator::ExampleHooks.empty, conditions = Spectator::ExampleConditions.empty) - Spectator::RootExampleGroup.new(hooks, conditions).tap do |group| - group.children = [] of Spectator::ExampleComponent - end -end - -def root_group_with_examples(example_count = 5) - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - examples = [] of Spectator::Example - group.children = Array(Spectator::ExampleComponent).new(example_count) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - {group, examples} -end - -def root_group_with_sub_groups(sub_group_count = 5, example_count = 5) - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - examples = [] of Spectator::Example - group.children = Array(Spectator::ExampleComponent).new(sub_group_count) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = Array(Spectator::ExampleComponent).new(example_count) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - end - end - {group, examples} -end - -def complex_root_group - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - examples = [] of Spectator::Example - group.children = Array(Spectator::ExampleComponent).new(10) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group1| - sub_group1.children = Array(Spectator::ExampleComponent).new(10) do |j| - if i % 2 == 0 - PassingExample.new(sub_group1, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - else - Spectator::NestedExampleGroup.new(j.to_s, sub_group1, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group2| - sub_group2.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(sub_group2, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - end - end - end - end - end - end - {group, examples} -end - -describe Spectator::RootExampleGroup do - describe "#run_before_hooks" do - it "runs a before_all hook" do - called = false - hooks = new_hooks(before_all: ->{ called = true; nil }) - group = new_root_group(hooks) - group.run_before_hooks - called.should be_true - end - - it "runs a before_each hook" do - called = false - hooks = new_hooks(before_each: ->{ called = true; nil }) - group = new_root_group(hooks) - group.run_before_hooks - called.should be_true - end - - it "runs multiple before_all hooks" do - call_count = 0 - hooks = new_hooks(before_all: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - group = new_root_group(hooks) - group.run_before_hooks - call_count.should eq(6) - end - - it "runs multiple before_each hooks" do - call_count = 0 - hooks = new_hooks(before_each: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - group = new_root_group(hooks) - group.run_before_hooks - call_count.should eq(6) - end - - it "runs hooks in the correct order" do - calls = [] of Symbol - hooks = new_hooks(before_all: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ], - before_each: [ - ->{ calls << :d; nil }, - ->{ calls << :e; nil }, - ->{ calls << :f; nil }, - ]) - group = new_root_group(hooks) - group.run_before_hooks - calls.should eq(%i[a b c d e f]) - end - - it "runs the before_all hooks once" do - call_count = 0 - hooks = new_hooks(before_all: ->{ call_count += 1; nil }) - group = new_root_group(hooks) - 2.times { group.run_before_hooks } - call_count.should eq(1) - end - - it "runs the before_each hooks multiple times" do - call_count = 0 - hooks = new_hooks(before_each: ->{ call_count += 1; nil }) - group = new_root_group(hooks) - 2.times { group.run_before_hooks } - call_count.should eq(2) - end - end - - describe "#run_after_hooks" do - # No children are used for most of these examples. - # That's because `[].all?` is always true. - # Which means that all examples are considered finished, since there are none. - it "runs a single after_all hook" do - called = false - hooks = new_hooks(after_all: ->{ called = true; nil }) - group = new_root_group(hooks) - group.run_after_hooks - called.should be_true - end - - it "runs a single after_each hook" do - called = false - hooks = new_hooks(after_each: ->{ called = true; nil }) - group = new_root_group(hooks) - group.run_after_hooks - called.should be_true - end - - it "runs multiple after_all hooks" do - call_count = 0 - hooks = new_hooks(after_all: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - group = new_root_group(hooks) - group.run_after_hooks - call_count.should eq(6) - end - - it "runs multiple after_each hooks" do - call_count = 0 - hooks = new_hooks(after_all: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - group = new_root_group(hooks) - group.run_after_hooks - call_count.should eq(6) - end - - it "runs hooks in the correct order" do - calls = [] of Symbol - hooks = new_hooks(after_each: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ], - after_all: [ - ->{ calls << :d; nil }, - ->{ calls << :e; nil }, - ->{ calls << :f; nil }, - ]) - group = new_root_group(hooks) - group.run_after_hooks - calls.should eq(%i[a b c d e f]) - end - - it "runs the after_all hooks once" do - call_count = 0 - hooks = new_hooks(after_all: ->{ call_count += 1; nil }) - group = new_root_group(hooks) - 2.times { group.run_after_hooks } - call_count.should eq(1) - end - - it "runs the after_each hooks multiple times" do - call_count = 0 - hooks = new_hooks(after_each: ->{ call_count += 1; nil }) - group = new_root_group(hooks) - 2.times { group.run_after_hooks } - call_count.should eq(2) - end - - context "with no examples finished" do - it "doesn't run the after_all hooks" do - called = false - hooks = new_hooks(after_all: ->{ called = true; nil }) - group = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - group.run_after_hooks - called.should be_false - end - - it "runs the after_each hooks" do - called = false - hooks = new_hooks(after_each: ->{ called = true; nil }) - group = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - group.run_after_hooks - called.should be_true - end - end - - context "with some examples finished" do - it "doesn't run the after_all hooks" do - called = false - examples = [] of Spectator::Example - hooks = new_hooks(after_all: ->{ called = true; nil }) - group = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - examples.each_with_index do |example, index| - Spectator::Internals::Harness.run(example) if index % 2 == 0 - end - group.run_after_hooks - called.should be_false - end - - it "runs the after_each hooks" do - called = false - examples = [] of Spectator::Example - hooks = new_hooks(after_each: ->{ called = true; nil }) - group = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).tap do |example| - examples << example - end - end - examples.each_with_index do |example, index| - Spectator::Internals::Harness.run(example) if index % 2 == 0 - end - group.run_after_hooks - called.should be_true - end - end - end - - describe "#wrap_around_each_hooks" do - it "wraps the block" do - called = false - wrapper = new_root_group.wrap_around_each_hooks do - called = true - end - wrapper.call - called.should be_true - end - - it "wraps a proc" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { called = true; proc.call }) - wrapper = new_root_group(hooks).wrap_around_each_hooks { } - wrapper.call - called.should be_true - end - - it "wraps multiple procs" do - call_count = 0 - hooks = new_hooks(around_each: [ - ->(proc : ->) { call_count += 1; proc.call }, - ->(proc : ->) { call_count += 2; proc.call }, - ->(proc : ->) { call_count += 3; proc.call }, - ]) - wrapper = new_root_group(hooks).wrap_around_each_hooks { } - wrapper.call - call_count.should eq(6) - end - - it "wraps procs in the correct order" do - calls = [] of Symbol - hooks = new_hooks(around_each: [ - ->(proc : ->) { calls << :a; proc.call }, - ->(proc : ->) { calls << :b; proc.call }, - ->(proc : ->) { calls << :c; proc.call }, - ]) - wrapper = new_root_group(hooks).wrap_around_each_hooks { } - wrapper.call - calls.should eq(%i[a b c]) - end - end - - describe "#run_pre_conditions" do - it "runs a condition" do - called = false - conditions = new_conditions(pre: ->{ called = true; nil }) - group = new_root_group(conditions: conditions) - group.run_pre_conditions - called.should be_true - end - - it "runs multiple conditions" do - call_count = 0 - conditions = new_conditions(pre: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - group = new_root_group(conditions: conditions) - group.run_pre_conditions - call_count.should eq(6) - end - - it "runs conditions in the correct order" do - calls = [] of Symbol - conditions = new_conditions(pre: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - group = new_root_group(conditions: conditions) - group.run_pre_conditions - calls.should eq(%i[a b c]) - end - - it "runs the conditions multiple times" do - call_count = 0 - conditions = new_conditions(pre: ->{ call_count += 1; nil }) - group = new_root_group(conditions: conditions) - 2.times { group.run_pre_conditions } - call_count.should eq(2) - end - end - - describe "#run_post_conditions" do - it "runs a single condition" do - called = false - conditions = new_conditions(post: ->{ called = true; nil }) - group = new_root_group(conditions: conditions) - group.run_post_conditions - called.should be_true - end - - it "runs multiple conditions" do - call_count = 0 - conditions = new_conditions(post: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - group = new_root_group(conditions: conditions) - group.run_post_conditions - call_count.should eq(6) - end - - it "runs conditions in the correct order" do - calls = [] of Symbol - conditions = new_conditions(post: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - group = new_root_group(conditions: conditions) - group.run_post_conditions - calls.should eq(%i[a b c]) - end - - it "runs the conditions multiple times" do - call_count = 0 - conditions = new_conditions(post: ->{ call_count += 1; nil }) - group = new_root_group(conditions: conditions) - 2.times { group.run_post_conditions } - call_count.should eq(2) - end - end - - describe "#to_s" do - it "is empty" do - new_root_group.to_s.should be_empty - end - end - - describe "#children" do - it "raises an error when not set" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - expect_raises(Exception) { group.children } - end - - it "returns the expected set" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - group.children = children - group.children.should eq(children) - end - end - - describe "#children=" do - it "raises an error trying to reset" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - end - group.children = children - expect_raises(Exception) { group.children = children } - end - end - - describe "#each" do - it "yields each child" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = [] of Spectator::ExampleComponent - end - end - end - group.to_a.should eq(group.children) - end - - it "doesn't yield children of children" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(sub_group, Spectator::Internals::SampleValues.empty) - end - end - end - end - group.to_a.should eq(group.children) - end - end - - describe "#each : Iterator" do - it "iterates over each child" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = [] of Spectator::ExampleComponent - end - end - end - group.each.to_a.should eq(group.children) - end - - it "doesn't iterate over children of children" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - if i % 2 == 0 - PassingExample.new(group, Spectator::Internals::SampleValues.empty) - else - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = Array(Spectator::ExampleComponent).new(5) do - PassingExample.new(sub_group, Spectator::Internals::SampleValues.empty) - end - end - end - end - group.each.to_a.should eq(group.children) - end - end - - describe "#example_count" do - context "with no examples" do - it "is zero" do - new_root_group.example_count.should eq(0) - end - end - - context "with empty sub-groups" do - it "is zero" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - end - group.example_count.should eq(0) - end - end - - context "with direct descendant examples" do - it "equals the number of examples" do - example_count = 10 - group, _ = root_group_with_examples(example_count) - group.example_count.should eq(example_count) - end - end - - context "with examples sub-groups" do - it "equals the total number of examples" do - sub_group_count = 3 - example_count = 10 - group, _ = root_group_with_sub_groups(sub_group_count, example_count) - group.example_count.should eq(sub_group_count * example_count) - end - end - - context "with examples at all levels" do - it "equals the total number of examples" do - group, examples = complex_root_group - group.example_count.should eq(examples.size) - end - end - end - - describe "#[]" do - context "when empty" do - it "raises an error" do - group = new_root_group - expect_raises(IndexError) { group[0] } - end - end - - context "with direct descendant examples" do - context "given 0" do - it "returns the first example" do - group, examples = root_group_with_examples - group[0].should eq(examples.first) - end - end - - context "given -1" do - it "returns the last example" do - group, examples = root_group_with_examples - group[-1].should eq(examples.last) - end - end - - context "given an in-bounds positive index" do - it "returns the expected example" do - group, examples = root_group_with_examples(10) - group[3].should eq(examples[3]) - end - end - - context "given an in-bounds negative index" do - it "returns the expected example" do - group, examples = root_group_with_examples(10) - group[-3].should eq(examples[-3]) - end - end - - context "an out-of-bounds positive index" do - it "raises an index error" do - group, _ = root_group_with_examples(10) - expect_raises(IndexError) { group[15] } - end - - it "handles off-by-one" do - group, _ = root_group_with_examples(10) - expect_raises(IndexError) { group[10] } - end - end - - context "an out-of-bounds negative index" do - it "raises an index error" do - group, _ = root_group_with_examples(10) - expect_raises(IndexError) { group[-15] } - end - - it "handles off-by-one" do - group, _ = root_group_with_examples(10) - expect_raises(IndexError) { group[-11] } - end - end - end - - context "with examples only in sub-groups" do - context "given 0" do - it "returns the first example" do - group, examples = root_group_with_sub_groups - group[0].should eq(examples.first) - end - end - - context "given -1" do - it "returns the last example" do - group, examples = root_group_with_sub_groups - group[-1].should eq(examples.last) - end - end - - context "given an in-bounds positive index" do - it "returns the expected example" do - group, examples = root_group_with_sub_groups(10, 2) - group[6].should eq(examples[6]) - end - end - - context "given an in-bounds negative index" do - it "returns the expected example" do - group, examples = root_group_with_sub_groups(10, 2) - group[-6].should eq(examples[-6]) - end - end - - context "an out-of-bounds positive index" do - it "raises an index error" do - group, _ = root_group_with_sub_groups(10, 2) - expect_raises(IndexError) { group[25] } - end - - it "handles off-by-one" do - group, _ = root_group_with_sub_groups(10, 2) - expect_raises(IndexError) { group[20] } - end - end - - context "an out-of-bounds negative index" do - it "raises an index error" do - group, _ = root_group_with_sub_groups(10, 2) - expect_raises(IndexError) { group[-25] } - end - - it "handles off-by-one" do - group, _ = root_group_with_sub_groups(10, 2) - expect_raises(IndexError) { group[-21] } - end - end - end - - context "with examples at all levels" do - context "given 0" do - it "returns the first example" do - group, examples = complex_root_group - group[0].should eq(examples.first) - end - end - - context "given -1" do - it "returns the last example" do - group, examples = complex_root_group - group[-1].should eq(examples.last) - end - end - - context "given an in-bounds positive index" do - it "returns the expected example" do - group, examples = complex_root_group - group[42].should eq(examples[42]) - end - end - - context "given an in-bounds negative index" do - it "returns the expected example" do - group, examples = complex_root_group - group[-42].should eq(examples[-42]) - end - end - - context "an out-of-bounds positive index" do - it "raises an index error" do - group, examples = complex_root_group - expect_raises(IndexError) { group[examples.size + 5] } - end - - it "handles off-by-one" do - group, examples = complex_root_group - expect_raises(IndexError) { group[examples.size] } - end - end - - context "an out-of-bounds negative index" do - it "raises an index error" do - group, examples = complex_root_group - expect_raises(IndexError) { group[-examples.size - 5] } - end - - it "handles off-by-one" do - group, examples = complex_root_group - expect_raises(IndexError) { group[-examples.size - 1] } - end - end - end - - context "with only sub-groups and no examples" do - it "raises an index error" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = [] of Spectator::ExampleComponent - end - end - expect_raises(IndexError) { group[0] } - end - end - end - - describe "#finished?" do - context "with no children" do - it "is true" do - new_root_group.finished?.should be_true - end - end - - context "with all unfinished children" do - it "is false" do - group, _ = root_group_with_examples - group.finished?.should be_false - end - end - - context "with some finished children" do - it "is false" do - group, examples = root_group_with_examples - examples.each_with_index do |example, index| - Spectator::Internals::Harness.run(example) if index % 2 == 0 - end - group.finished?.should be_false - end - end - - context "with all finished children" do - it "is true" do - group, examples = root_group_with_examples - examples.each do |example| - Spectator::Internals::Harness.run(example) - end - group.finished?.should be_true - end - end - - context "with a sub-group" do - context "with no children" do - it "is true" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array(Spectator::ExampleComponent).new(5) do |i| - Spectator::NestedExampleGroup.new(i.to_s, group, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty).tap do |sub_group| - sub_group.children = [] of Spectator::ExampleComponent - end - end - group.finished?.should be_true - end - end - - context "with all unfinished children" do - it "is false" do - group, _ = root_group_with_sub_groups - group.finished?.should be_false - end - end - - context "with some finished children" do - it "is false" do - group, examples = root_group_with_sub_groups - examples.each_with_index do |example, index| - Spectator::Internals::Harness.run(example) if index % 2 == 0 - end - group.finished?.should be_false - end - end - - context "with all finished children" do - it "is true" do - group, examples = root_group_with_sub_groups - examples.each do |example| - Spectator::Internals::Harness.run(example) - end - group.finished?.should be_true - end - end - end - end - - describe "#symbolic?" do - it "is true" do - new_root_group.symbolic?.should be_true - end - end -end diff --git a/spec/runnable_example_spec.cr b/spec/runnable_example_spec.cr deleted file mode 100644 index 5907454..0000000 --- a/spec/runnable_example_spec.cr +++ /dev/null @@ -1,1568 +0,0 @@ -require "./spec_helper" - -def new_runnable_example(group : Spectator::ExampleGroup? = nil, symbolic = false) - actual_group = group || Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - PassingExample.new(actual_group, Spectator::Internals::SampleValues.empty, symbolic).tap do |example| - actual_group.children = [example.as(Spectator::ExampleComponent)] - end -end - -def run_example(example_type : Spectator::Example.class, hooks : Spectator::ExampleHooks? = nil, conditions : Spectator::ExampleConditions? = nil) - group = Spectator::RootExampleGroup.new(hooks || Spectator::ExampleHooks.empty, conditions || Spectator::ExampleConditions.empty) - run_example(example_type, group) -end - -def run_example(example_type : Spectator::Example.class, group : Spectator::ExampleGroup? = nil) - actual_group = group || Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - example = example_type.new(actual_group, Spectator::Internals::SampleValues.empty) - actual_group.children = [example.as(Spectator::ExampleComponent)] - Spectator::Internals::Harness.run(example) -end - -def run_example(hooks : Spectator::ExampleHooks? = nil, conditions : Spectator::ExampleConditions? = nil, &block) - example = SpyExample.create(hooks || Spectator::ExampleHooks.empty, conditions || Spectator::ExampleConditions.empty, &block) - Spectator::Internals::Harness.run(example) -end - -describe Spectator::RunnableExample do - describe "#run" do - context "with a passing test" do - it "returns a successful result" do - run_example(PassingExample).should be_a(Spectator::SuccessfulResult) - end - - {% for hook_type in %i[before_all before_each] %} - context "{{hook_type.id}} hooks" do - it "runs a hook" do - called = false - hooks = new_hooks({{hook_type.id}}: ->{ called = true; nil }) - run_example(PassingExample, hooks) - called.should be_true - end - - it "runs multiple hooks" do - call_count = 0 - hooks = new_hooks({{hook_type.id}}: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - run_example(PassingExample, hooks) - call_count.should eq(6) - end - - it "runs them in the correct order" do - calls = [] of Symbol - hooks = new_hooks({{hook_type.id}}: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - run_example(PassingExample, hooks) - calls.should eq(\%i[a b c]) - end - - it "runs parent group hooks" do - called = false - hooks = new_hooks({{hook_type.id}}: -> { called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(PassingExample, group) - called.should be_true - end - - it "runs parent group hooks first" do - calls = [] of Symbol - root_hooks = new_hooks({{hook_type.id}}: -> { calls << :a; nil }) - group_hooks = new_hooks({{hook_type.id}}: -> { calls << :b; nil }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, group_hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(PassingExample, group) - calls.should eq(\%i[a b]) - end - - it "runs the hook before the test code" do - calls = [] of Symbol - hooks = new_hooks({{hook_type.id}}: ->{ calls << :a; nil }) - run_example(hooks) do - calls << :b - end - calls.should eq(\%i[a b]) - end - end - {% end %} - - it "runs before_all hooks prior to before_each hooks" do - calls = [] of Symbol - hooks = new_hooks( - before_all: ->{ calls << :a; nil }, - before_each: ->{ calls << :b; nil } - ) - run_example(PassingExample, hooks) - calls.should eq(%i[a b]) - end - - {% for hook_type in %i[after_all after_each] %} - context "{{hook_type.id}} hooks" do - it "runs a hook" do - called = false - hooks = new_hooks({{hook_type.id}}: ->{ called = true; nil }) - run_example(PassingExample, hooks) - called.should be_true - end - - it "runs multiple hooks" do - call_count = 0 - hooks = new_hooks({{hook_type.id}}: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - run_example(PassingExample, hooks) - call_count.should eq(6) - end - - it "runs them in the correct order" do - calls = [] of Symbol - hooks = new_hooks({{hook_type.id}}: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - run_example(PassingExample, hooks) - calls.should eq(\%i[a b c]) - end - - it "runs parent group hooks" do - called = false - hooks = new_hooks({{hook_type.id}}: -> { called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(PassingExample, group) - called.should be_true - end - - it "runs parent group hooks last" do - calls = [] of Symbol - root_hooks = new_hooks({{hook_type.id}}: -> { calls << :a; nil }) - group_hooks = new_hooks({{hook_type.id}}: -> { calls << :b; nil }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, group_hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(PassingExample, group) - calls.should eq(\%i[b a]) - end - - it "runs the hook after the test code" do - calls = [] of Symbol - hooks = new_hooks({{hook_type.id}}: ->{ calls << :a; nil }) - run_example(hooks) do - calls << :b - end - calls.should eq(\%i[b a]) - end - end - {% end %} - - it "runs after_each hooks prior to after_all hooks" do - calls = [] of Symbol - hooks = new_hooks( - after_each: ->{ calls << :a; nil }, - after_all: ->{ calls << :b; nil } - ) - run_example(PassingExample, hooks) - calls.should eq(%i[a b]) - end - - context "around_each hooks" do - it "runs a hook" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { called = true; proc.call }) - run_example(PassingExample, hooks) - called.should be_true - end - - it "runs multiple hooks" do - call_count = 0 - hooks = new_hooks(around_each: [ - ->(proc : ->) { call_count += 1; proc.call }, - ->(proc : ->) { call_count += 2; proc.call }, - ->(proc : ->) { call_count += 3; proc.call }, - ]) - run_example(PassingExample, hooks) - call_count.should eq(6) - end - - it "runs them in the correct order" do - calls = [] of Symbol - hooks = new_hooks(around_each: [ - ->(proc : ->) { calls << :a; proc.call }, - ->(proc : ->) { calls << :b; proc.call }, - ->(proc : ->) { calls << :c; proc.call }, - ]) - run_example(PassingExample, hooks) - calls.should eq(%i[a b c]) - end - - it "runs parent group hooks" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { called = true; proc.call }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(PassingExample, group) - called.should be_true - end - - it "runs parent group hooks first" do - calls = [] of Symbol - root_hooks = new_hooks(around_each: ->(proc : ->) { calls << :a; proc.call }) - group_hooks = new_hooks(around_each: ->(proc : ->) { calls << :b; proc.call }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, group_hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(PassingExample, group) - calls.should eq(%i[a b]) - end - - it "runs around_each hooks after the before hooks" do - calls = [] of Symbol - hooks = new_hooks( - before_all: ->{ calls << :b; nil }, - before_each: ->{ calls << :b; nil }, - around_each: ->(proc : ->) { calls << :c; proc.call } - ) - run_example(PassingExample, hooks) - calls.should eq(%i[b b c]) - end - - it "runs around_each hooks prior to after hooks" do - calls = [] of Symbol - hooks = new_hooks( - around_each: ->(proc : ->) { calls << :c; proc.call }, - after_each: ->{ calls << :a; nil }, - after_all: ->{ calls << :a; nil }, - ) - run_example(PassingExample, hooks) - calls.should eq(%i[c a a]) - end - end - - {% for condition in %i[pre post] %} - context "{{condition.id}}-conditions" do - it "checks a single condition" do - called = false - conditions = new_conditions({{condition.id}}: -> { called = true; nil }) - run_example(PassingExample, conditions: conditions) - called.should be_true - end - - it "checks multiple conditions" do - call_count = 0 - conditions = new_conditions({{condition.id}}: [ - -> { call_count += 1; nil }, - -> { call_count += 2; nil }, - -> { call_count += 3; nil }, - ]) - run_example(PassingExample, conditions: conditions) - call_count.should eq(6) - end - - it "checks them in the correct order" do - calls = [] of Symbol - conditions = new_conditions({{condition.id}}: [ - -> { calls << :a; nil }, - -> { calls << :b; nil }, - -> { calls << :c; nil }, - ]) - run_example(PassingExample, conditions: conditions) - calls.should eq(\%i[a b c]) - end - - it "checks parent group conditions" do - called = false - conditions = new_conditions({{condition.id}}: -> { called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(PassingExample, group) - called.should be_true - end - - {% if condition == :pre %} - it "runs prior to the test code" do - calls = [] of Symbol - conditions = new_conditions({{condition.id}}: ->{ calls << :a; nil }) - run_example(conditions: conditions) do - calls << :b - end - calls.should eq(\%i[a b]) - end - - it "checks parent group conditions first" do - calls = [] of Symbol - root_conditions = new_conditions({{condition.id}}: -> { calls << :a; nil }) - group_conditions = new_conditions({{condition.id}}: -> { calls << :b; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, group_conditions) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(PassingExample, group) - calls.should eq(\%i[a b]) - end - {% else %} - it "runs after the test code" do - calls = [] of Symbol - conditions = new_conditions({{condition.id}}: ->{ calls << :b; nil }) - run_example(conditions: conditions) do - calls << :a - end - calls.should eq(\%i[a b]) - end - - it "checks parent group conditions last" do - calls = [] of Symbol - root_conditions = new_conditions({{condition.id}}: -> { calls << :a; nil }) - group_conditions = new_conditions({{condition.id}}: -> { calls << :b; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, group_conditions) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(PassingExample, group) - calls.should eq(\%i[b a]) - end - {% end %} - end - {% end %} - - it "runs before hooks prior to pre-conditions" do - calls = [] of Symbol - hooks = new_hooks( - before_all: ->{ calls << :b; nil }, - before_each: ->{ calls << :b; nil } - ) - conditions = new_conditions(pre: ->{ calls << :p; nil }) - run_example(PassingExample, hooks, conditions) - calls.should eq(%i[b b p]) - end - - it "runs around_each hooks prior to pre-conditions" do - calls = [] of Symbol - hooks = new_hooks( - around_each: ->(proc : ->) { calls << :c; proc.call } - ) - conditions = new_conditions(pre: ->{ calls << :p; nil }) - run_example(PassingExample, hooks, conditions) - calls.should eq(%i[c p]) - end - - it "runs post-conditions prior to after hooks" do - calls = [] of Symbol - hooks = new_hooks( - after_all: ->{ calls << :a; nil }, - after_each: ->{ calls << :a; nil } - ) - conditions = new_conditions(pre: ->{ calls << :p; nil }) - run_example(PassingExample, hooks, conditions) - calls.should eq(%i[p a a]) - end - - context "failing pre-condition" do - it "fails the test" do - conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - result = run_example(PassingExample, conditions: conditions) - result.should be_a(Spectator::FailedResult) - end - - it "prevents the test code from running" do - called = false - conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - run_example(conditions: conditions) do - called = true - end - called.should be_false - end - - it "prevents additional pre-conditions from running" do - called = false - conditions = new_conditions(pre: [ - ->{ report_expectations(0, 1) }, - ->{ called = true; nil }, - ]) - run_example(PassingExample, conditions: conditions) - called.should be_false - end - - it "prevents additional post-conditions from running" do - called = false - conditions = new_conditions( - pre: ->{ report_expectations(0, 1) }, - post: ->{ called = true; nil } - ) - run_example(PassingExample, conditions: conditions) - called.should be_false - end - - context "in a parent group" do - it "fails the test" do - conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - result = run_example(PassingExample, group) - result.should be_a(Spectator::FailedResult) - end - - it "prevents the test code from running" do - called = false - conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - example = SpyExample.new(group, Spectator::Internals::SampleValues.empty) - group.children = [example.as(Spectator::ExampleComponent)] - example.block = ->{ called = true; nil } - Spectator::Internals::Harness.run(example) - called.should be_false - end - - it "doesn't run child pre-conditions" do - called = false - root_conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - group_conditions = new_conditions(pre: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, group_conditions) - root.children = [group.as(Spectator::ExampleComponent)] - example = PassingExample.new(group, Spectator::Internals::SampleValues.empty) - group.children = [example.as(Spectator::ExampleComponent)] - Spectator::Internals::Harness.run(example) - called.should be_false - end - - it "doesn't run child post-conditions" do - called = false - root_conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - group_conditions = new_conditions(post: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, group_conditions) - root.children = [group.as(Spectator::ExampleComponent)] - example = PassingExample.new(group, Spectator::Internals::SampleValues.empty) - group.children = [example.as(Spectator::ExampleComponent)] - Spectator::Internals::Harness.run(example) - called.should be_false - end - end - end - - context "failing post-condition" do - it "fails the test" do - conditions = new_conditions(post: ->{ report_expectations(0, 1) }) - result = run_example(PassingExample, conditions: conditions) - result.should be_a(Spectator::FailedResult) - end - - it "prevents additional post-conditions from running" do - called = false - conditions = new_conditions(post: [ - ->{ report_expectations(0, 1) }, - ->{ called = true; nil }, - ]) - run_example(PassingExample, conditions: conditions) - called.should be_false - end - - context "in a parent group" do - it "fails the test" do - conditions = new_conditions(post: ->{ report_expectations(0, 1) }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - result = run_example(PassingExample, group) - result.should be_a(Spectator::FailedResult) - end - - it "doesn't run parent post-conditions" do - called = false - root_conditions = new_conditions(post: ->{ called = true; nil }) - group_conditions = new_conditions(post: ->{ report_expectations(0, 1) }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, group_conditions) - root.children = [group.as(Spectator::ExampleComponent)] - example = PassingExample.new(group, Spectator::Internals::SampleValues.empty) - group.children = [example.as(Spectator::ExampleComponent)] - Spectator::Internals::Harness.run(example) - called.should be_false - end - end - end - end - - context "with a failing test" do - it "returns a failed result" do - run_example(FailingExample).should be_a(Spectator::FailedResult) - end - - {% for hook_type in %i[before_all before_each] %} - context "{{hook_type.id}} hooks" do - it "runs a hook" do - called = false - hooks = new_hooks({{hook_type.id}}: ->{ called = true; nil }) - run_example(FailingExample, hooks) - called.should be_true - end - - it "runs multiple hooks" do - call_count = 0 - hooks = new_hooks({{hook_type.id}}: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - run_example(FailingExample, hooks) - call_count.should eq(6) - end - - it "runs them in the correct order" do - calls = [] of Symbol - hooks = new_hooks({{hook_type.id}}: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - run_example(FailingExample, hooks) - calls.should eq(\%i[a b c]) - end - - it "runs parent group hooks" do - called = false - hooks = new_hooks({{hook_type.id}}: -> { called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(FailingExample, group) - called.should be_true - end - - it "runs parent group hooks first" do - calls = [] of Symbol - root_hooks = new_hooks({{hook_type.id}}: -> { calls << :a; nil }) - group_hooks = new_hooks({{hook_type.id}}: -> { calls << :b; nil }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, group_hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(FailingExample, group) - calls.should eq(\%i[a b]) - end - end - {% end %} - - it "runs before_all hooks prior to before_each hooks" do - calls = [] of Symbol - hooks = new_hooks( - before_all: ->{ calls << :a; nil }, - before_each: ->{ calls << :b; nil } - ) - run_example(FailingExample, hooks) - calls.should eq(%i[a b]) - end - - {% for hook_type in %i[after_all after_each] %} - context "{{hook_type.id}} hooks" do - it "runs a hook" do - called = false - hooks = new_hooks({{hook_type.id}}: ->{ called = true; nil }) - run_example(FailingExample, hooks) - called.should be_true - end - - it "runs multiple hooks" do - call_count = 0 - hooks = new_hooks({{hook_type.id}}: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - run_example(FailingExample, hooks) - call_count.should eq(6) - end - - it "runs them in the correct order" do - calls = [] of Symbol - hooks = new_hooks({{hook_type.id}}: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - run_example(FailingExample, hooks) - calls.should eq(\%i[a b c]) - end - - it "runs parent group hooks" do - called = false - hooks = new_hooks({{hook_type.id}}: -> { called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(FailingExample, group) - called.should be_true - end - - it "runs parent group hooks last" do - calls = [] of Symbol - root_hooks = new_hooks({{hook_type.id}}: -> { calls << :a; nil }) - group_hooks = new_hooks({{hook_type.id}}: -> { calls << :b; nil }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, group_hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(FailingExample, group) - calls.should eq(\%i[b a]) - end - end - {% end %} - - it "runs after_each hooks prior to after_all hooks" do - calls = [] of Symbol - hooks = new_hooks( - after_each: ->{ calls << :a; nil }, - after_all: ->{ calls << :b; nil } - ) - run_example(FailingExample, hooks) - calls.should eq(%i[a b]) - end - - context "around_each hooks" do - it "runs a hook" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { called = true; proc.call }) - run_example(FailingExample, hooks) - called.should be_true - end - - it "runs multiple hooks" do - call_count = 0 - hooks = new_hooks(around_each: [ - ->(proc : ->) { call_count += 1; proc.call }, - ->(proc : ->) { call_count += 2; proc.call }, - ->(proc : ->) { call_count += 3; proc.call }, - ]) - run_example(FailingExample, hooks) - call_count.should eq(6) - end - - it "runs them in the correct order" do - calls = [] of Symbol - hooks = new_hooks(around_each: [ - ->(proc : ->) { calls << :a; proc.call }, - ->(proc : ->) { calls << :b; proc.call }, - ->(proc : ->) { calls << :c; proc.call }, - ]) - run_example(FailingExample, hooks) - calls.should eq(%i[a b c]) - end - - it "runs parent group hooks" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { called = true; proc.call }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(FailingExample, group) - called.should be_true - end - - it "runs parent group hooks first" do - calls = [] of Symbol - root_hooks = new_hooks(around_each: ->(proc : ->) { calls << :a; proc.call }) - group_hooks = new_hooks(around_each: ->(proc : ->) { calls << :b; proc.call }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, group_hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(FailingExample, group) - calls.should eq(%i[a b]) - end - - it "runs around_each hooks after the before hooks" do - calls = [] of Symbol - hooks = new_hooks( - before_all: ->{ calls << :b; nil }, - before_each: ->{ calls << :b; nil }, - around_each: ->(proc : ->) { calls << :c; proc.call } - ) - run_example(FailingExample, hooks) - calls.should eq(%i[b b c]) - end - - it "runs around_each hooks prior to after hooks" do - calls = [] of Symbol - hooks = new_hooks( - around_each: ->(proc : ->) { calls << :c; proc.call }, - after_each: ->{ calls << :a; nil }, - after_all: ->{ calls << :a; nil }, - ) - run_example(FailingExample, hooks) - calls.should eq(%i[c a a]) - end - end - - context "pre-conditions" do - it "checks a single condition" do - called = false - conditions = new_conditions(pre: ->{ called = true; nil }) - run_example(FailingExample, conditions: conditions) - called.should be_true - end - - it "checks multiple conditions" do - call_count = 0 - conditions = new_conditions(pre: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - run_example(FailingExample, conditions: conditions) - call_count.should eq(6) - end - - it "checks them in the correct order" do - calls = [] of Symbol - conditions = new_conditions(pre: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - run_example(FailingExample, conditions: conditions) - calls.should eq(%i[a b c]) - end - - it "checks parent group conditions" do - called = false - conditions = new_conditions(pre: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(FailingExample, group) - called.should be_true - end - - it "checks parent group conditions first" do - calls = [] of Symbol - root_conditions = new_conditions(pre: ->{ calls << :a; nil }) - group_conditions = new_conditions(pre: ->{ calls << :b; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, group_conditions) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(FailingExample, group) - calls.should eq(%i[a b]) - end - end - - it "runs before hooks prior to pre-conditions" do - calls = [] of Symbol - hooks = new_hooks( - before_all: ->{ calls << :b; nil }, - before_each: ->{ calls << :b; nil } - ) - conditions = new_conditions(pre: ->{ calls << :p; nil }) - run_example(FailingExample, hooks, conditions) - calls.should eq(%i[b b p]) - end - - it "runs around_each hooks prior to pre-conditions" do - calls = [] of Symbol - hooks = new_hooks( - around_each: ->(proc : ->) { calls << :c; proc.call } - ) - conditions = new_conditions(pre: ->{ calls << :p; nil }) - run_example(FailingExample, hooks, conditions) - calls.should eq(%i[c p]) - end - - context "failing pre-condition" do - it "fails the test" do - conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - result = run_example(FailingExample, conditions: conditions) - result.should be_a(Spectator::FailedResult) - end - - it "prevents additional pre-conditions from running" do - called = false - conditions = new_conditions(pre: [ - ->{ report_expectations(0, 1) }, - ->{ called = true; nil }, - ]) - run_example(FailingExample, conditions: conditions) - called.should be_false - end - - it "prevents additional post-conditions from running" do - called = false - conditions = new_conditions( - pre: ->{ report_expectations(0, 1) }, - post: ->{ called = true; nil } - ) - run_example(FailingExample, conditions: conditions) - called.should be_false - end - - context "in a parent group" do - it "fails the test" do - conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - result = run_example(FailingExample, group) - result.should be_a(Spectator::FailedResult) - end - - it "doesn't run child pre-conditions" do - called = false - root_conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - group_conditions = new_conditions(pre: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, group_conditions) - root.children = [group.as(Spectator::ExampleComponent)] - example = FailingExample.new(group, Spectator::Internals::SampleValues.empty) - group.children = [example.as(Spectator::ExampleComponent)] - Spectator::Internals::Harness.run(example) - called.should be_false - end - - it "doesn't run child post-conditions" do - called = false - root_conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - group_conditions = new_conditions(post: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, group_conditions) - root.children = [group.as(Spectator::ExampleComponent)] - example = FailingExample.new(group, Spectator::Internals::SampleValues.empty) - group.children = [example.as(Spectator::ExampleComponent)] - Spectator::Internals::Harness.run(example) - called.should be_false - end - end - end - - it "doesn't run post-conditions" do - called = false - conditions = new_conditions(post: ->{ called = true; nil }) - run_example(FailingExample, conditions: conditions) - called.should be_false - end - - it "doesn't run parent group post-conditions" do - called = false - conditions = new_conditions(post: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(FailingExample, group) - called.should be_false - end - end - - context "with an errored test" do - it "returns an errored result" do - run_example(ErroredExample).should be_a(Spectator::ErroredResult) - end - - {% for hook_type in %i[before_all before_each] %} - context "{{hook_type.id}} hooks" do - it "runs a hook" do - called = false - hooks = new_hooks({{hook_type.id}}: ->{ called = true; nil }) - run_example(ErroredExample, hooks) - called.should be_true - end - - it "runs multiple hooks" do - call_count = 0 - hooks = new_hooks({{hook_type.id}}: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - run_example(ErroredExample, hooks) - call_count.should eq(6) - end - - it "runs them in the correct order" do - calls = [] of Symbol - hooks = new_hooks({{hook_type.id}}: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - run_example(ErroredExample, hooks) - calls.should eq(\%i[a b c]) - end - - it "runs parent group hooks" do - called = false - hooks = new_hooks({{hook_type.id}}: -> { called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(ErroredExample, group) - called.should be_true - end - - it "runs parent group hooks first" do - calls = [] of Symbol - root_hooks = new_hooks({{hook_type.id}}: -> { calls << :a; nil }) - group_hooks = new_hooks({{hook_type.id}}: -> { calls << :b; nil }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, group_hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(ErroredExample, group) - calls.should eq(\%i[a b]) - end - end - {% end %} - - it "runs before_all hooks prior to before_each hooks" do - calls = [] of Symbol - hooks = new_hooks( - before_all: ->{ calls << :a; nil }, - before_each: ->{ calls << :b; nil } - ) - run_example(ErroredExample, hooks) - calls.should eq(%i[a b]) - end - - {% for hook_type in %i[after_all after_each] %} - context "{{hook_type.id}} hooks" do - it "runs a hook" do - called = false - hooks = new_hooks({{hook_type.id}}: ->{ called = true; nil }) - run_example(ErroredExample, hooks) - called.should be_true - end - - it "runs multiple hooks" do - call_count = 0 - hooks = new_hooks({{hook_type.id}}: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - run_example(ErroredExample, hooks) - call_count.should eq(6) - end - - it "runs them in the correct order" do - calls = [] of Symbol - hooks = new_hooks({{hook_type.id}}: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - run_example(ErroredExample, hooks) - calls.should eq(\%i[a b c]) - end - - it "runs parent group hooks" do - called = false - hooks = new_hooks({{hook_type.id}}: -> { called = true; nil }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(ErroredExample, group) - called.should be_true - end - - it "runs parent group hooks last" do - calls = [] of Symbol - root_hooks = new_hooks({{hook_type.id}}: -> { calls << :a; nil }) - group_hooks = new_hooks({{hook_type.id}}: -> { calls << :b; nil }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, group_hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(ErroredExample, group) - calls.should eq(\%i[b a]) - end - end - {% end %} - - it "runs after_each hooks prior to after_all hooks" do - calls = [] of Symbol - hooks = new_hooks( - after_each: ->{ calls << :a; nil }, - after_all: ->{ calls << :b; nil } - ) - run_example(ErroredExample, hooks) - calls.should eq(%i[a b]) - end - - context "around_each hooks" do - it "runs a hook" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { called = true; proc.call }) - run_example(ErroredExample, hooks) - called.should be_true - end - - it "runs multiple hooks" do - call_count = 0 - hooks = new_hooks(around_each: [ - ->(proc : ->) { call_count += 1; proc.call }, - ->(proc : ->) { call_count += 2; proc.call }, - ->(proc : ->) { call_count += 3; proc.call }, - ]) - run_example(ErroredExample, hooks) - call_count.should eq(6) - end - - it "runs them in the correct order" do - calls = [] of Symbol - hooks = new_hooks(around_each: [ - ->(proc : ->) { calls << :a; proc.call }, - ->(proc : ->) { calls << :b; proc.call }, - ->(proc : ->) { calls << :c; proc.call }, - ]) - run_example(ErroredExample, hooks) - calls.should eq(%i[a b c]) - end - - it "runs parent group hooks" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { called = true; proc.call }) - root = Spectator::RootExampleGroup.new(hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(ErroredExample, group) - called.should be_true - end - - it "runs parent group hooks first" do - calls = [] of Symbol - root_hooks = new_hooks(around_each: ->(proc : ->) { calls << :a; proc.call }) - group_hooks = new_hooks(around_each: ->(proc : ->) { calls << :b; proc.call }) - root = Spectator::RootExampleGroup.new(root_hooks, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("what", root, group_hooks, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(ErroredExample, group) - calls.should eq(%i[a b]) - end - - it "runs around_each hooks after the before hooks" do - calls = [] of Symbol - hooks = new_hooks( - before_all: ->{ calls << :b; nil }, - before_each: ->{ calls << :b; nil }, - around_each: ->(proc : ->) { calls << :c; proc.call } - ) - run_example(ErroredExample, hooks) - calls.should eq(%i[b b c]) - end - - it "runs around_each hooks prior to after hooks" do - calls = [] of Symbol - hooks = new_hooks( - around_each: ->(proc : ->) { calls << :c; proc.call }, - after_each: ->{ calls << :a; nil }, - after_all: ->{ calls << :a; nil }, - ) - run_example(ErroredExample, hooks) - calls.should eq(%i[c a a]) - end - end - - context "pre-conditions" do - it "checks a single condition" do - called = false - conditions = new_conditions(pre: ->{ called = true; nil }) - run_example(ErroredExample, conditions: conditions) - called.should be_true - end - - it "checks multiple conditions" do - call_count = 0 - conditions = new_conditions(pre: [ - ->{ call_count += 1; nil }, - ->{ call_count += 2; nil }, - ->{ call_count += 3; nil }, - ]) - run_example(ErroredExample, conditions: conditions) - call_count.should eq(6) - end - - it "checks them in the correct order" do - calls = [] of Symbol - conditions = new_conditions(pre: [ - ->{ calls << :a; nil }, - ->{ calls << :b; nil }, - ->{ calls << :c; nil }, - ]) - run_example(ErroredExample, conditions: conditions) - calls.should eq(%i[a b c]) - end - - it "checks parent group conditions" do - called = false - conditions = new_conditions(pre: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(ErroredExample, group) - called.should be_true - end - - it "checks parent group conditions first" do - calls = [] of Symbol - root_conditions = new_conditions(pre: ->{ calls << :a; nil }) - group_conditions = new_conditions(pre: ->{ calls << :b; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, group_conditions) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(ErroredExample, group) - calls.should eq(%i[a b]) - end - end - - it "runs before hooks prior to pre-conditions" do - calls = [] of Symbol - hooks = new_hooks( - before_all: ->{ calls << :b; nil }, - before_each: ->{ calls << :b; nil } - ) - conditions = new_conditions(pre: ->{ calls << :p; nil }) - run_example(ErroredExample, hooks, conditions) - calls.should eq(%i[b b p]) - end - - it "runs around_each hooks prior to pre-conditions" do - calls = [] of Symbol - hooks = new_hooks( - around_each: ->(proc : ->) { calls << :c; proc.call } - ) - conditions = new_conditions(pre: ->{ calls << :p; nil }) - run_example(ErroredExample, hooks, conditions) - calls.should eq(%i[c p]) - end - - context "failing pre-condition" do - it "fails the test" do - conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - result = run_example(ErroredExample, conditions: conditions) - result.should be_a(Spectator::FailedResult) - end - - it "prevents additional pre-conditions from running" do - called = false - conditions = new_conditions(pre: [ - ->{ report_expectations(0, 1) }, - ->{ called = true; nil }, - ]) - run_example(ErroredExample, conditions: conditions) - called.should be_false - end - - it "prevents additional post-conditions from running" do - called = false - conditions = new_conditions( - pre: ->{ report_expectations(0, 1) }, - post: ->{ called = true; nil } - ) - run_example(ErroredExample, conditions: conditions) - called.should be_false - end - - context "in a parent group" do - it "fails the test" do - conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - result = run_example(ErroredExample, group) - result.should be_a(Spectator::FailedResult) - end - - it "doesn't run child pre-conditions" do - called = false - root_conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - group_conditions = new_conditions(pre: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, group_conditions) - root.children = [group.as(Spectator::ExampleComponent)] - example = ErroredExample.new(group, Spectator::Internals::SampleValues.empty) - group.children = [example.as(Spectator::ExampleComponent)] - Spectator::Internals::Harness.run(example) - called.should be_false - end - - it "doesn't run child post-conditions" do - called = false - root_conditions = new_conditions(pre: ->{ report_expectations(0, 1) }) - group_conditions = new_conditions(post: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, root_conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, group_conditions) - root.children = [group.as(Spectator::ExampleComponent)] - example = ErroredExample.new(group, Spectator::Internals::SampleValues.empty) - group.children = [example.as(Spectator::ExampleComponent)] - Spectator::Internals::Harness.run(example) - called.should be_false - end - end - end - - it "doesn't run post-conditions" do - called = false - conditions = new_conditions(post: ->{ called = true; nil }) - run_example(ErroredExample, conditions: conditions) - called.should be_false - end - - it "doesn't run parent group post-conditions" do - called = false - conditions = new_conditions(post: ->{ called = true; nil }) - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, conditions) - group = Spectator::NestedExampleGroup.new("what", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - run_example(ErroredExample, group) - called.should be_false - end - end - - context "when an error is raised in a before_all hook" do - it "raises the exception" do - hooks = new_hooks(before_all: ->{ raise "oops"; nil }) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - end - - it "passes along the original exception" do - error = Exception.new("oops") - hooks = new_hooks(before_all: ->{ raise error; nil }) - begin - run_example(PassingExample, hooks) - rescue ex - ex.cause.should eq(error) - end - end - - it "doesn't run the test code" do - called = false - hooks = new_hooks(before_all: ->{ raise "oops"; nil }) - expect_raises(Exception) do - run_example(hooks) do - called = true - end - end - called.should be_false - end - - it "doesn't run any additional before_all hooks" do - called = false - hooks = new_hooks(before_all: [ - ->{ raise "oops"; nil }, - ->{ called = true; nil }, - ]) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - called.should be_false - end - - it "doesn't run any additional hooks" do - called = :none - hooks = new_hooks( - before_all: ->{ raise "oops"; nil }, - before_each: ->{ called = :before_each; nil }, - after_all: ->{ called = :after_all; nil }, - after_each: ->{ called = :after_each; nil }, - around_each: ->(proc : ->) { called = :around_each; proc.call }) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - called.should eq(:none) - end - end - - context "when an error is raised in a before_each hook" do - it "raises the exception" do - hooks = new_hooks(before_each: ->{ raise "oops"; nil }) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - end - - it "passes along the original exception" do - error = Exception.new("oops") - hooks = new_hooks(before_each: ->{ raise error; nil }) - begin - run_example(PassingExample, hooks) - rescue ex - ex.cause.should eq(error) - end - end - - it "doesn't run the test code" do - called = false - hooks = new_hooks(before_each: ->{ raise "oops"; nil }) - expect_raises(Exception) do - run_example(hooks) do - called = true - end - end - called.should be_false - end - - it "doesn't run any additional before_each hooks" do - called = false - hooks = new_hooks(before_each: [ - ->{ raise "oops"; nil }, - ->{ called = true; nil }, - ]) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - called.should be_false - end - - it "doesn't run any additional hooks" do - called = :none - hooks = new_hooks( - before_each: ->{ raise "oops"; nil }, - after_all: ->{ called = :after_all; nil }, - after_each: ->{ called = :after_each; nil }, - around_each: ->(proc : ->) { called = :around_each; proc.call }) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - called.should eq(:none) - end - end - - context "when an error is raised in an after_all hook" do - it "raises the exception" do - hooks = new_hooks(after_all: ->{ raise "oops"; nil }) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - end - - it "passes along the original exception" do - error = Exception.new("oops") - hooks = new_hooks(after_all: ->{ raise error; nil }) - begin - run_example(PassingExample, hooks) - rescue ex - ex.cause.should eq(error) - end - end - - it "doesn't run any additional after_all hooks" do - called = false - hooks = new_hooks(after_all: [ - ->{ raise "oops"; nil }, - ->{ called = true; nil }, - ]) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - called.should be_false - end - end - - context "when an error is raised in an after_each hook" do - it "raises the exception" do - hooks = new_hooks(after_each: ->{ raise "oops"; nil }) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - end - - it "passes along the original exception" do - error = Exception.new("oops") - hooks = new_hooks(after_each: ->{ raise error; nil }) - begin - run_example(PassingExample, hooks) - rescue ex - ex.cause.should eq(error) - end - end - - it "doesn't run any additional after_each hooks" do - called = false - hooks = new_hooks(after_each: [ - ->{ raise "oops"; nil }, - ->{ called = true; nil }, - ]) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - called.should be_false - end - - it "doesn't run any additional hooks" do - called = :none - hooks = new_hooks( - before_all: ->{ nil }, - before_each: ->{ nil }, - after_all: ->{ called = :after_all; nil }, - after_each: ->{ raise "oops"; nil }, - around_each: ->(proc : ->) { proc.call }) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - called.should eq(:none) - end - end - - context "when an error is raised in an around_each hook" do - it "raises the exception" do - hooks = new_hooks(around_each: ->(proc : ->) { raise "oops"; proc.call }) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - end - - it "passes along the original exception" do - error = Exception.new("oops") - hooks = new_hooks(around_each: ->(proc : ->) { raise error; proc.call }) - begin - run_example(PassingExample, hooks) - rescue ex - ex.cause.should eq(error) - end - end - - it "doesn't run the test code" do - called = false - hooks = new_hooks(around_each: ->(proc : ->) { raise "oops"; proc.call }) - expect_raises(Exception) do - run_example(hooks) do - called = true - end - end - called.should be_false - end - - it "doesn't run any additional around_each hooks" do - called = false - hooks = new_hooks(around_each: [ - ->(proc : ->) { raise "oops"; proc.call }, - ->(proc : ->) { called = true; proc.call }, - ]) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - called.should be_false - end - - it "doesn't run any additional hooks" do - called = :none - hooks = new_hooks( - after_all: ->{ called = :after_all; nil }, - after_each: ->{ called = :after_each; nil }, - around_each: ->(proc : ->) { raise "oops"; proc.call }) - expect_raises(Exception) do - run_example(PassingExample, hooks) - end - called.should eq(:none) - end - end - end - - describe "#finished?" do - it "is initially false" do - new_runnable_example.finished?.should be_false - end - - it "is true after #run is called" do - example = new_runnable_example - Spectator::Internals::Harness.run(example) - example.finished?.should be_true - end - end - - describe "#group" do - it "is the expected value" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - example = new_runnable_example(group) - example.group.should eq(group) - end - end - - describe "#example_count" do - it "is one" do - new_runnable_example.example_count.should eq(1) - end - end - - describe "#[]" do - it "returns self" do - example = new_runnable_example - example[0].should eq(example) - end - end - - describe "#to_s" do - it "contains #what" do - example = new_runnable_example - example.to_s.should contain(example.what) - end - - it "contains the group's #what" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("GROUP", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - example = new_runnable_example(group) - example.to_s.should contain(group.what.to_s) - end - - context "when #symbolic? is true" do - context "and the group is symbolic" do - it "omits the space" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new(:Group, root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - example = new_runnable_example(group, true) - group.symbolic?.should be_true - example.symbolic?.should be_true - example.to_s.should_not contain(' ') - end - end - - context "and the group isn't symbolic" do - it "inserts a space" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("GROUP", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - example = new_runnable_example(group, true) - group.symbolic?.should be_false - example.symbolic?.should be_true - example.to_s.should contain(' ') - end - end - end - - context "when #symbolic? is false" do - context "and the group is symbolic" do - it "inserts a space" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new(:Group, root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - example = new_runnable_example(group, false) - group.symbolic?.should be_true - example.symbolic?.should be_false - example.to_s.should contain(' ') - end - end - - context "and the group isn't symbolic" do - it "inserts a space" do - root = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group = Spectator::NestedExampleGroup.new("GROUP", root, Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - root.children = [group.as(Spectator::ExampleComponent)] - example = new_runnable_example(group, false) - group.symbolic?.should be_false - example.symbolic?.should be_false - example.to_s.should contain(' ') - end - end - end - end -end diff --git a/spec/runner_spec.cr b/spec/runner_spec.cr deleted file mode 100644 index c2c4dfb..0000000 --- a/spec/runner_spec.cr +++ /dev/null @@ -1,270 +0,0 @@ -require "./spec_helper" - -# Creates a `Config` for Spectator that is suited for testing it. -def spectator_test_config(formatter : Spectator::Formatting::Formatter? = nil, fail_fast = false) - builder = Spectator::ConfigBuilder.new - builder.formatter = formatter || Spectator::Formatting::SilentFormatter.new - builder.fail_fast = fail_fast - builder.build -end - -def new_test_suite(group : Spectator::ExampleGroup? = nil) - filter = Spectator::NullExampleFilter.new - Spectator::TestSuite.new(group || PassingExample.create.group, filter) -end - -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 - -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 - suite = new_test_suite(group) - runner = Spectator::Runner.new(suite, spectator_test_config) - runner.run - called.should eq([0, 1, 2, 3, 4]) - end - - 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 - suite = new_test_suite(group) - runner = Spectator::Runner.new(suite, spectator_test_config(fail_fast: true)) - runner.run - called.should eq([0, 1, 2, 3, 4, 5, 6]) - end - - 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 - - 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 - - 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 - suite = new_test_suite(group) - runner = Spectator::Runner.new(suite, spectator_test_config(spy, true)) - runner.run - args = spy.end_suite_calls.first - args[:report].remaining_count.should eq(3) - end - end - end - - 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 - group = SpyExample.create_group(5) { nil } - suite = new_test_suite(group) - 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 - group = SpyExample.create_group(5) { nil } - suite = new_test_suite(group) - 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 - group = SpyExample.create_group(5) { nil } - suite = new_test_suite(group) - 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 - suite = new_test_suite(group) - 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 - suite = new_test_suite(group) - spy = SpyFormatter.new - runner = Spectator::Runner.new(suite, spectator_test_config(spy)) - runner.run - args = spy.end_suite_calls.first - args[:report].each_with_index do |result, index| - 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 - group = SpyExample.create_group(5) { nil } - suite = new_test_suite(group) - spy = SpyFormatter.new - runner = Spectator::Runner.new(suite, spectator_test_config(spy)) - max_time = Time.measure { runner.run } - min_time = spy.end_example_calls.each.map(&.as(Spectator::FinishedResult)).sum(&.elapsed) - args = spy.end_suite_calls.first - report = args[:report] - report.runtime.should be <= max_time - report.runtime.should be >= min_time - end - end - end -end diff --git a/spec/source_example_filter_spec.cr b/spec/source_example_filter_spec.cr deleted file mode 100644 index f8e5e0a..0000000 --- a/spec/source_example_filter_spec.cr +++ /dev/null @@ -1,22 +0,0 @@ -require "./spec_helper" - -describe Spectator::SourceExampleFilter do - describe "#includes?" do - context "with a matching example" do - it "is true" do - example = PassingExample.create - filter = Spectator::SourceExampleFilter.new(example.source) - filter.includes?(example).should be_true - end - end - - context "with a non-matching example" do - it "is false" do - example = PassingExample.create - source = Spectator::Source.new(__FILE__, __LINE__) - filter = Spectator::SourceExampleFilter.new(source) - filter.includes?(example).should be_false - end - end - end -end diff --git a/spec/source_spec.cr b/spec/source_spec.cr deleted file mode 100644 index 1ad6950..0000000 --- a/spec/source_spec.cr +++ /dev/null @@ -1,76 +0,0 @@ -require "./spec_helper" - -describe Spectator::Source do - describe "#file" do - it "is the expected value" do - file = __FILE__ - source = Spectator::Source.new(file, __LINE__) - source.file.should eq(file) - end - end - - describe "#line" do - it "is the expected value" do - line = __LINE__ - source = Spectator::Source.new(__FILE__, line) - source.line.should eq(line) - end - end - - describe "#path" do - context "with a relative file" do - it "is shortened" do - file = "test.cr" - absolute = File.join(Dir.current, file) - source = Spectator::Source.new(absolute, __LINE__) - source.path.should eq(file) - end - end - - context "with a different directory" do - it "is the absolute path" do - file = "/foo/bar/baz.cr" - source = Spectator::Source.new(file, __LINE__) - source.path.should eq(file) - end - end - end - - describe "#to_s" do - it "contains #path" do - file = __FILE__ - source = Spectator::Source.new(file, __LINE__) - source.to_s.should contain(source.path) - end - - it "contains #line" do - line = __LINE__ - source = Spectator::Source.new(__FILE__, line) - source.to_s.should contain(line.to_s) - end - - it "is formatted correctly" do - source = Spectator::Source.new(__FILE__, __LINE__) - source.to_s.should match(/^(.+?)\:(\d+)$/) - end - end - - describe "#parse" do - it "gets the absolute path" do - file = "foo.cr" - path = File.expand_path(file) - source = Spectator::Source.parse("#{file}:42") - source.file.should eq(path) - end - - it "gets the relative path" do - source = Spectator::Source.parse("foo.cr:42") - source.path.should eq("foo.cr") - end - - it "gets the line number" do - source = Spectator::Source.parse("foo.cr:42") - source.line.should eq(42) - end - end -end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 32978f5..30b0ee7 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -1,6 +1,4 @@ -require "spec" require "../src/spectator" -require "./helpers/*" -# Prevent Spectator from trying to run tests. +# Prevent Spectator from trying to run tests on its own. Spectator.autorun = false diff --git a/spec/successful_result_spec.cr b/spec/successful_result_spec.cr deleted file mode 100644 index 2f6be49..0000000 --- a/spec/successful_result_spec.cr +++ /dev/null @@ -1,77 +0,0 @@ -require "./spec_helper" - -def new_successful_result( - example : Spectator::Example? = nil, - elapsed : Time::Span? = nil, - expectations : Spectator::Expectations::ExampleExpectations? = nil -) - Spectator::SuccessfulResult.new( - example || PassingExample.create, - elapsed || Time::Span.zero, - expectations || Spectator::Expectations::ExampleExpectations.new([new_satisfied_expectation]) - ) -end - -describe Spectator::SuccessfulResult do - describe "#call" do - context "without a block" do - it "invokes #success on an instance" do - spy = ResultCallSpy.new - new_successful_result.call(spy) - spy.success?.should be_true - end - - it "returns the value of #success" do - result = new_successful_result - returned = result.call(ResultCallSpy.new) - returned.should eq(:success) - end - end - - context "with a block" do - it "invokes #success on an instance" do - spy = ResultCallSpy.new - new_successful_result.call(spy) { nil } - spy.success?.should be_true - end - - it "yields itself" do - result = new_successful_result - value = nil.as(Spectator::Result?) - result.call(ResultCallSpy.new) { |r| value = r } - value.should eq(result) - end - - it "returns the value of #success" do - result = new_successful_result - value = 42 - returned = result.call(ResultCallSpy.new) { value } - returned.should eq(value) - end - end - end - - describe "#example" do - it "is the expected value" do - example = PassingExample.create - result = new_successful_result(example: example) - result.example.should eq(example) - end - end - - describe "#elapsed" do - it "is the expected value" do - elapsed = Time::Span.new(10, 10, 10) - result = new_successful_result(elapsed: elapsed) - result.elapsed.should eq(elapsed) - end - end - - describe "#expectations" do - it "is the expected value" do - expectations = Spectator::Expectations::ExampleExpectations.new(create_expectations(5, 0)) - result = new_successful_result(expectations: expectations) - result.expectations.should eq(expectations) - end - end -end diff --git a/spec/test_suite_spec.cr b/spec/test_suite_spec.cr deleted file mode 100644 index 5e41278..0000000 --- a/spec/test_suite_spec.cr +++ /dev/null @@ -1,31 +0,0 @@ -require "./spec_helper" - -describe Spectator::TestSuite do - describe "#each" do - it "yields each example" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array.new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).as(Spectator::ExampleComponent) - end - test_suite = Spectator::TestSuite.new(group, Spectator::NullExampleFilter.new) - examples = [] of Spectator::Example - test_suite.each do |example| - examples << example - end - examples.should eq(group.children) - end - - it "skips examples not in the filter" do - group = Spectator::RootExampleGroup.new(Spectator::ExampleHooks.empty, Spectator::ExampleConditions.empty) - group.children = Array.new(5) do - PassingExample.new(group, Spectator::Internals::SampleValues.empty).as(Spectator::ExampleComponent) - end - test_suite = Spectator::TestSuite.new(group, Spectator::CompositeExampleFilter.new([] of Spectator::ExampleFilter)) - examples = [] of Spectator::Example - test_suite.each do |example| - examples << example - end - examples.should be_empty - end - end -end