Add support for pending tests

This commit is contained in:
Michael Miller 2019-09-26 20:46:28 -06:00
parent 25778d7b41
commit 07aeaecca4
5 changed files with 71 additions and 5 deletions

View file

@ -25,5 +25,40 @@ module Spectator
{{@type.name}} {{@type.name}}
) { |test| test.as({{@type.name}}).%run } ) { |test| test.as({{@type.name}}).%run }
end 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
end end

View file

@ -45,7 +45,17 @@ module Spectator
def add_example(description : String, source : Source, def add_example(description : String, source : Source,
example_type : ::SpectatorTest.class, &runner : ::SpectatorTest ->) : Nil example_type : ::SpectatorTest.class, &runner : ::SpectatorTest ->) : Nil
builder = ->(values : TestValues) { example_type.new(values).as(::SpectatorTest) } 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) @@stack.current.add_child(factory)
end end

View file

@ -3,16 +3,17 @@ require "../test_values"
require "../test_wrapper" require "../test_wrapper"
module Spectator::SpecBuilder module Spectator::SpecBuilder
class ExampleBuilder abstract class ExampleBuilder
alias FactoryMethod = TestValues -> ::SpectatorTest alias FactoryMethod = TestValues -> ::SpectatorTest
def initialize(@description : String, @source : Source, @builder : FactoryMethod, @runner : TestMethod) def initialize(@description : String, @source : Source, @builder : FactoryMethod, @runner : TestMethod)
end end
def build(group) abstract def build(group) : ExampleComponent
private def build_test_wrapper(group)
test = @builder.call(group.context.values) test = @builder.call(group.context.values)
wrapper = TestWrapper.new(@description, @source, test, @runner) TestWrapper.new(@description, @source, test, @runner)
RunnableExample.new(group, wrapper).as(ExampleComponent)
end end
end end
end end

View file

@ -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

View file

@ -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