Add ability to test expectations directly

This commit is contained in:
Michael Miller 2020-08-16 12:04:45 -06:00
parent fab216419c
commit 62fd289b0f
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
2 changed files with 30 additions and 0 deletions

View file

@ -28,6 +28,10 @@ Spectator.describe "Runtime compilation" do
it "does something" do it "does something" do
expect(true).to be_false expect(true).to be_false
end end
it "doesn't run" do
expect(true).to be_false
end
end end
it "detects failed examples" do it "detects failed examples" do
@ -43,4 +47,12 @@ Spectator.describe "Runtime compilation" do
it "raises on compilation errors" do it "raises on compilation errors" do
expect { malformed_example }.to raise_error(/compilation/i) expect { malformed_example }.to raise_error(/compilation/i)
end 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 end

View file

@ -26,3 +26,21 @@ macro given_example(id, &block)
).result ).result
end end
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