Class: TestRunner::Assert::Syntax
- Inherits:
-
Module
- Object
- Module
- TestRunner::Assert::Syntax
- Defined in:
- lib/test_runner/assert/syntax.rb
Constant Summary collapse
- MISSING =
:__test_runner_assert_arg_missing__
Class Method Summary collapse
- .ambiguous_subject! ⇒ Object
-
.decode_assert_arguments(subject_or_checks, checks, block) ⇒ Object
Public: assert is designed to be called a number of different ways, from 0 to 2 positional arguments, and maybe a block.
- .infect(target) ⇒ Object
- .missing_subject! ⇒ Object
Instance Method Summary collapse
Class Method Details
.ambiguous_subject! ⇒ Object
62 63 64 |
# File 'lib/test_runner/assert/syntax.rb', line 62 def ambiguous_subject! raise ArgumentError, "cannot supply a block subject *and* a positional subject" end |
.decode_assert_arguments(subject_or_checks, checks, block) ⇒ Object
Public: assert is designed to be called a number of different ways, from 0 to 2 positional arguments, and maybe a block.
subject_or_checks - the first optional argument passed in checks - the second optional argument passed in block - the &block parameter
Valid examples:
assert :raises => Error do … end
assert "foo"
assert 3, :included_in => [6, 3]
assert [6, 3], :includes => 3
assert :equal => 4 do 2 + 2 end
Invalid examples:
# Can't determine if the block or 2 is the subject
assert 2, :equals => 4 do ̒… end
# There's no subject at all
assert :incuded_in => 4
Returns two arguments, the subject, and a checks hash. If the checks would be empty, returns { :truthy => true }. The subject will always be a Proc that gets lazy evaluated when the assertion is checked.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/test_runner/assert/syntax.rb', line 41 def decode_assert_arguments subject_or_checks, checks, block if checks == MISSING if subject_or_checks == MISSING missing_subject! unless block subject_thunk = block checks = { :truthy => true } elsif block ambiguous_subject! unless subject_or_checks.is_a? Hash subject_thunk = block checks = subject_or_checks else subject_thunk = -> do subject_or_checks end checks = { :truthy => true } end else ambiguous_subject! if block subject_thunk = -> do subject_or_checks end end [subject_thunk, checks] end |
.infect(target) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/test_runner/assert/syntax.rb', line 7 def infect target instance = new if target.is_a? Class target.send :include, instance else target.extend instance end end |
.missing_subject! ⇒ Object
66 67 68 |
# File 'lib/test_runner/assert/syntax.rb', line 66 def missing_subject! raise ArgumentError, "must supply either a positional subject *or* a block subject (but not both)" end |
Instance Method Details
#extended(base) ⇒ Object
71 72 73 |
# File 'lib/test_runner/assert/syntax.rb', line 71 def extended base graft base.singleton_class end |
#graft(base) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/test_runner/assert/syntax.rb', line 79 def graft base method_body = -> method_name, assertion_class, syntax do define_method method_name do |arg1 = MISSING, arg2 = MISSING, &block| subject_thunk, checks = syntax.decode_assert_arguments arg1, arg2, block assertion = assertion_class.new subject_thunk, checks assertion.source = self assertion.() end end base.class_exec :assert, Assertion, self.class, &method_body base.class_exec :refute, Assertion::Refutation, self.class, &method_body end |
#included(base) ⇒ Object
75 76 77 |
# File 'lib/test_runner/assert/syntax.rb', line 75 def included base graft base end |