From ff2cbcd4c72232e2cbd53dbca4f275e9a81d29b6 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 2 Jan 2020 18:40:25 -0700 Subject: [PATCH] Allow test description to be blank Currently defaults to the example's source. --- src/spectator/dsl/examples.cr | 14 +++++++------- src/spectator/example.cr | 2 +- src/spectator/spec_builder.cr | 4 ++-- src/spectator/spec_builder/example_builder.cr | 2 +- src/spectator/test_wrapper.cr | 5 +++++ 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/spectator/dsl/examples.cr b/src/spectator/dsl/examples.cr index 041b1a8..b356ea5 100644 --- a/src/spectator/dsl/examples.cr +++ b/src/spectator/dsl/examples.cr @@ -3,7 +3,7 @@ require "../spec_builder" module Spectator module DSL - macro it(description, _source_file = __FILE__, _source_line = __LINE__, &block) + macro it(description = nil, _source_file = __FILE__, _source_line = __LINE__, &block) {% if block.is_a?(Nop) %} {% if description.is_a?(Call) %} def %run @@ -20,17 +20,17 @@ module Spectator %source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) ::Spectator::SpecBuilder.add_example( - {{description.is_a?(StringLiteral) ? description : description.stringify}}, + {{description.is_a?(StringLiteral) || description.is_a?(NilLiteral) ? description : description.stringify}}, %source, {{@type.name}} ) { |test| test.as({{@type.name}}).%run } end - macro specify(description, &block) + macro specify(description = nil, &block) it({{description}}) {{block}} end - macro pending(description, _source_file = __FILE__, _source_line = __LINE__, &block) + macro pending(description = nil, _source_file = __FILE__, _source_line = __LINE__, &block) {% if block.is_a?(Nop) %} {% if description.is_a?(Call) %} def %run @@ -47,17 +47,17 @@ module Spectator %source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) ::Spectator::SpecBuilder.add_pending_example( - {{description.is_a?(StringLiteral) ? description : description.stringify}}, + {{description.is_a?(StringLiteral) || description.is_a?(NilLiteral) ? description : description.stringify}}, %source, {{@type.name}} ) { |test| test.as({{@type.name}}).%run } end - macro skip(description, &block) + macro skip(description = nil, &block) pending({{description}}) {{block}} end - macro xit(description, &block) + macro xit(description = nil, &block) pending({{description}}) {{block}} end end diff --git a/src/spectator/example.cr b/src/spectator/example.cr index e437bc4..3cf7051 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -23,7 +23,7 @@ module Spectator @test_wrapper.source end - def description : String | Symbol + def description : String | Symbol? @test_wrapper.description end diff --git a/src/spectator/spec_builder.cr b/src/spectator/spec_builder.cr index 9e199c2..39834be 100644 --- a/src/spectator/spec_builder.cr +++ b/src/spectator/spec_builder.cr @@ -42,7 +42,7 @@ module Spectator # 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_example(description : String, source : Source, + def add_example(description : String?, source : Source, example_type : ::SpectatorTest.class, &runner : ::SpectatorTest ->) : Nil builder = ->(values : TestValues) { example_type.new(values).as(::SpectatorTest) } factory = RunnableExampleBuilder.new(description, source, builder, runner) @@ -52,7 +52,7 @@ module Spectator # 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, + 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) diff --git a/src/spectator/spec_builder/example_builder.cr b/src/spectator/spec_builder/example_builder.cr index 44f23fe..378c024 100644 --- a/src/spectator/spec_builder/example_builder.cr +++ b/src/spectator/spec_builder/example_builder.cr @@ -6,7 +6,7 @@ module Spectator::SpecBuilder abstract class ExampleBuilder 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 abstract def build(group) : ExampleComponent diff --git a/src/spectator/test_wrapper.cr b/src/spectator/test_wrapper.cr index 756f287..7bd756f 100644 --- a/src/spectator/test_wrapper.cr +++ b/src/spectator/test_wrapper.cr @@ -17,6 +17,11 @@ module Spectator def initialize(@description : String, @source : Source, @test : ::SpectatorTest, @runner : TestMethod) end + # Creates a wrapper for the test. + def initialize(description : Nil, @source : Source, @test : ::SpectatorTest, @runner : TestMethod) + @description = @source.to_s + end + def run call(@runner) end