From 033f753dcadc27e9483f4353d42588233f6a10d4 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 2 Jan 2019 13:24:30 -0700 Subject: [PATCH] Initial implementation of one-liner "it" syntax --- src/spectator/dsl/structure_dsl.cr | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index fc9f0d0..424d3b9 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -1052,9 +1052,27 @@ module Spectator::DSL # # See `ExampleDSL` and `MatcherDSL` for additional macros and methods # that can be used in example code blocks. + # + # A short-hand, one-liner syntax can also be used. + # Typically, this is combined with `#subject`. + # For instance: + # ``` + # subject { 1 + 2 } + # it is_expected.to eq(3) + # ``` macro it(what, &block) # Create the wrapper class for the test code. - _spectator_example_wrapper(Wrapper%example, %run) {{block}} + {% if block.is_a?(Nop) %} + {% if what.is_a?(Call) %} + _spectator_example_wrapper(Wrapper%example, %run) do + {{what}} + end + {% else %} + {% raise "Unrecognized syntax: `it #{what}`" %} + {% end %} + {% else %} + _spectator_example_wrapper(Wrapper%example, %run) {{block}} + {% end %} # Create a class derived from `RunnableExample` to run the test code. _spectator_example(Example%example, Wrapper%example, ::Spectator::RunnableExample, {{what}}) do