2018-09-27 22:11:45 +00:00
|
|
|
class Object
|
|
|
|
# Extension method to create an expectation for an object.
|
|
|
|
# This is part of the spec DSL and mimics Crystal Spec's default should-syntax.
|
|
|
|
# A matcher should immediately follow this method, or be the only argument to it.
|
|
|
|
# Example usage:
|
|
|
|
# ```
|
|
|
|
# it "equals the expected value" do
|
|
|
|
# subject.should eq(42)
|
|
|
|
# end
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# NOTE: By default, the should-syntax is disabled.
|
|
|
|
# The expect-syntax is preferred,
|
|
|
|
# since it doesn't [monkey-patch](https://en.wikipedia.org/wiki/Monkey_patch) all objects.
|
|
|
|
# To enable should-syntax, add the following to your `spec_helper.cr` file:
|
|
|
|
# ```
|
|
|
|
# require "spectator/should"
|
|
|
|
# ```
|
2019-11-15 01:23:45 +00:00
|
|
|
def should(matcher)
|
2018-09-27 22:11:45 +00:00
|
|
|
# First argument of the `Expectation` initializer is the expression label.
|
|
|
|
# However, since this isn't a macro and we can't "look behind" this method call
|
|
|
|
# to see what it was invoked on, the argument is an empty string.
|
2019-03-24 02:15:35 +00:00
|
|
|
# Additionally, the source file and line can't be obtained.
|
2019-08-01 21:44:04 +00:00
|
|
|
actual = ::Spectator::TestValue.new(self)
|
2019-08-01 21:00:56 +00:00
|
|
|
source = ::Spectator::Source.new(__FILE__, __LINE__)
|
|
|
|
::Spectator::Expectations::ExpectationPartial.new(actual, source).to(matcher)
|
2018-09-27 22:11:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Works the same as `#should` except the condition is inverted.
|
|
|
|
# When `#should` succeeds, this method will fail, and vice-versa.
|
2019-11-15 01:23:45 +00:00
|
|
|
def should_not(matcher)
|
2019-08-01 21:44:04 +00:00
|
|
|
actual = ::Spectator::TestValue.new(self)
|
2019-08-01 21:00:56 +00:00
|
|
|
source = ::Spectator::Source.new(__FILE__, __LINE__)
|
|
|
|
::Spectator::Expectations::ExpectationPartial.new(actual, source).to_not(matcher)
|
2018-09-27 22:11:45 +00:00
|
|
|
end
|
2019-11-15 01:26:24 +00:00
|
|
|
|
|
|
|
# Works the same as `#should` except that the condition check is postphoned.
|
|
|
|
# The expectation is checked after the example finishes and all hooks have run.
|
|
|
|
def should_eventually(matcher)
|
|
|
|
::Spectator::Harness.current.defer { should(matcher) }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Works the same as `#should_not` except that the condition check is postphoned.
|
|
|
|
# The expectation is checked after the example finishes and all hooks have run.
|
|
|
|
def should_never(matcher)
|
|
|
|
::Spectator::Harness.current.defer { should_not(matcher) }
|
|
|
|
end
|
2018-09-27 22:11:45 +00:00
|
|
|
end
|
2019-03-24 02:20:15 +00:00
|
|
|
|
|
|
|
struct Proc(*T, R)
|
|
|
|
# Extension method to create an expectation for a block of code (proc).
|
|
|
|
# Depending on the matcher, the proc may be executed multiple times.
|
2019-11-15 01:23:45 +00:00
|
|
|
def should(matcher)
|
2019-08-01 21:44:04 +00:00
|
|
|
actual = ::Spectator::TestBlock.new(self)
|
2019-08-01 21:00:56 +00:00
|
|
|
source = ::Spectator::Source.new(__FILE__, __LINE__)
|
|
|
|
::Spectator::Expectations::ExpectationPartial.new(actual, source).to(matcher)
|
2019-03-24 02:20:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Works the same as `#should` except the condition is inverted.
|
|
|
|
# When `#should` succeeds, this method will fail, and vice-versa.
|
2019-11-15 01:23:45 +00:00
|
|
|
def should_not(matcher)
|
2019-08-01 21:44:04 +00:00
|
|
|
actual = ::Spectator::TestBlock.new(self)
|
2019-08-01 21:00:56 +00:00
|
|
|
source = ::Spectator::Source.new(__FILE__, __LINE__)
|
|
|
|
::Spectator::Expectations::BlockExpectationPartial.new(actual, source).to_not(matcher)
|
2019-03-24 02:20:15 +00:00
|
|
|
end
|
|
|
|
end
|
2021-01-10 18:09:28 +00:00
|
|
|
|
|
|
|
module Spectator::DSL::Assertions
|
|
|
|
macro should(matcher)
|
|
|
|
expect(subject).to({{matcher}})
|
|
|
|
end
|
|
|
|
|
|
|
|
macro should_not(matcher)
|
|
|
|
expect(subject).to_not({{matcher}})
|
|
|
|
end
|
|
|
|
|
|
|
|
macro should_eventually(matcher)
|
|
|
|
expect(subject).to_eventually({{matcher}})
|
|
|
|
end
|
|
|
|
|
|
|
|
macro should_never(matcher)
|
|
|
|
expect(subject).to_never({{matcher}})
|
|
|
|
end
|
|
|
|
end
|