diff --git a/spec/runtime_example_spec.cr b/spec/runtime_example_spec.cr index 9fedc28..324f7ea 100644 --- a/spec/runtime_example_spec.cr +++ b/spec/runtime_example_spec.cr @@ -28,6 +28,10 @@ Spectator.describe "Runtime compilation" do it "does something" do expect(true).to be_false end + + it "doesn't run" do + expect(true).to be_false + end end it "detects failed examples" do @@ -43,4 +47,12 @@ Spectator.describe "Runtime compilation" do it "raises on compilation errors" do expect { malformed_example }.to raise_error(/compilation/i) end + + given_expectation satisfied_expectation do + expect(true).to be_true + end + + it "can compile and retrieve expectations" do + expect(satisfied_expectation).to be_satisfied + end end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 865a6b3..736ac91 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -26,3 +26,21 @@ macro given_example(id, &block) ).result end end + +# Defines an example ("it" block) that is lazily compiled. +# The "it" block must be omitted, as the block provided to this macro will be wrapped in one. +# When the expectation is referenced with *id*, it will be compiled and the result retrieved. +# The value returned by *id* will be a `Spectator::SpecHelpers::Expectation`. +# This allows an expectation to be inspected. +# Only the last expectation performed will be returned. +# An error is raised if no expectations ran. +macro given_expectation(id, &block) + let({{id}}) do + result = ::Spectator::SpecHelpers::Example.new( + {{__FILE__}}, + {{id.id.stringify}}, + {{"it do\n" + block.body.stringify + "\nend"}} + ).result + result.expectations.last || raise("No expectations found from {{id.id}}") + end +end