From 07aeaecca4d123e5533c838be98fe13141d48718 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 26 Sep 2019 20:46:28 -0600 Subject: [PATCH] Add support for pending tests --- src/spectator/dsl/examples.cr | 35 +++++++++++++++++++ src/spectator/spec_builder.cr | 12 ++++++- src/spectator/spec_builder/example_builder.cr | 9 ++--- .../spec_builder/pending_example_builder.cr | 10 ++++++ .../spec_builder/runnable_example_builder.cr | 10 ++++++ 5 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 src/spectator/spec_builder/pending_example_builder.cr create mode 100644 src/spectator/spec_builder/runnable_example_builder.cr diff --git a/src/spectator/dsl/examples.cr b/src/spectator/dsl/examples.cr index 3b0d81e..041b1a8 100644 --- a/src/spectator/dsl/examples.cr +++ b/src/spectator/dsl/examples.cr @@ -25,5 +25,40 @@ module Spectator {{@type.name}} ) { |test| test.as({{@type.name}}).%run } end + + macro specify(description, &block) + it({{description}}) {{block}} + end + + macro pending(description, _source_file = __FILE__, _source_line = __LINE__, &block) + {% if block.is_a?(Nop) %} + {% if description.is_a?(Call) %} + def %run + {{description}} + end + {% else %} + {% raise "Unrecognized syntax: `pending #{description}` at #{_source_file}:#{_source_line}" %} + {% end %} + {% else %} + def %run + {{block.body}} + end + {% end %} + + %source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) + ::Spectator::SpecBuilder.add_pending_example( + {{description.is_a?(StringLiteral) ? description : description.stringify}}, + %source, + {{@type.name}} + ) { |test| test.as({{@type.name}}).%run } + end + + macro skip(description, &block) + pending({{description}}) {{block}} + end + + macro xit(description, &block) + pending({{description}}) {{block}} + end end end diff --git a/src/spectator/spec_builder.cr b/src/spectator/spec_builder.cr index 7b0c93c..ed897da 100644 --- a/src/spectator/spec_builder.cr +++ b/src/spectator/spec_builder.cr @@ -45,7 +45,17 @@ module Spectator def add_example(description : String, source : Source, example_type : ::SpectatorTest.class, &runner : ::SpectatorTest ->) : Nil builder = ->(values : TestValues) { example_type.new(values).as(::SpectatorTest) } - factory = ExampleBuilder.new(description, source, builder, runner) + factory = RunnableExampleBuilder.new(description, source, builder, runner) + @@stack.current.add_child(factory) + end + + # Adds an example type to the current group. + # The class name of the example should be passed as an argument. + # The example will be instantiated later. + def add_pending_example(description : String, source : Source, + example_type : ::SpectatorTest.class, &runner : ::SpectatorTest ->) : Nil + builder = ->(values : TestValues) { example_type.new(values).as(::SpectatorTest) } + factory = PendingExampleBuilder.new(description, source, builder, runner) @@stack.current.add_child(factory) end diff --git a/src/spectator/spec_builder/example_builder.cr b/src/spectator/spec_builder/example_builder.cr index d249214..44f23fe 100644 --- a/src/spectator/spec_builder/example_builder.cr +++ b/src/spectator/spec_builder/example_builder.cr @@ -3,16 +3,17 @@ require "../test_values" require "../test_wrapper" module Spectator::SpecBuilder - class ExampleBuilder + abstract class ExampleBuilder alias FactoryMethod = TestValues -> ::SpectatorTest def initialize(@description : String, @source : Source, @builder : FactoryMethod, @runner : TestMethod) end - def build(group) + abstract def build(group) : ExampleComponent + + private def build_test_wrapper(group) test = @builder.call(group.context.values) - wrapper = TestWrapper.new(@description, @source, test, @runner) - RunnableExample.new(group, wrapper).as(ExampleComponent) + TestWrapper.new(@description, @source, test, @runner) end end end diff --git a/src/spectator/spec_builder/pending_example_builder.cr b/src/spectator/spec_builder/pending_example_builder.cr new file mode 100644 index 0000000..731b9ab --- /dev/null +++ b/src/spectator/spec_builder/pending_example_builder.cr @@ -0,0 +1,10 @@ +require "./example_builder" + +module Spectator::SpecBuilder + class PendingExampleBuilder < ExampleBuilder + def build(group) : ExampleComponent + wrapper = build_test_wrapper(group) + PendingExample.new(group, wrapper).as(ExampleComponent) + end + end +end diff --git a/src/spectator/spec_builder/runnable_example_builder.cr b/src/spectator/spec_builder/runnable_example_builder.cr new file mode 100644 index 0000000..8c22a15 --- /dev/null +++ b/src/spectator/spec_builder/runnable_example_builder.cr @@ -0,0 +1,10 @@ +require "./example_builder" + +module Spectator::SpecBuilder + class RunnableExampleBuilder < ExampleBuilder + def build(group) : ExampleComponent + wrapper = build_test_wrapper(group) + RunnableExample.new(group, wrapper).as(ExampleComponent) + end + end +end