class FTest::Assert::Syntax

Constants

MISSING

Public Class Methods

ambiguous_subject!() click to toggle source
# File lib/ftest/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) click to toggle source

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.

# File lib/ftest/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) click to toggle source
# File lib/ftest/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!() click to toggle source
# File lib/ftest/assert/syntax.rb, line 66
def missing_subject!
  raise ArgumentError, "must supply either a positional subject *or* a block subject (but not both)"
end

Public Instance Methods

extended(base) click to toggle source
# File lib/ftest/assert/syntax.rb, line 71
def extended base
  graft base.singleton_class
end
graft(base) click to toggle source
# File lib/ftest/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) click to toggle source
# File lib/ftest/assert/syntax.rb, line 75
def included base
  graft base
end