Class: Assay::Assertor

Inherits:
Object
  • Object
show all
Defined in:
lib/assay/assertor.rb

Overview

Assertor delegates to Assay class. It provides an object-oriented interface to makeing assertions, as opposed to the functional interface of the Assay classes themselves.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assay_class, *criteria, &block) ⇒ Assertor

Returns a new instance of Assertor.



11
12
13
14
15
16
# File 'lib/assay/assertor.rb', line 11

def initialize(assay_class, *criteria, &block)
  @assay    = assay_class
  @criteria = criteria
  @block    = block
  @not      = false
end

Instance Attribute Details

#assayObject (readonly)

The assay class to which this assertor delegates.



21
22
23
# File 'lib/assay/assertor.rb', line 21

def assay
  @assay
end

#blockObject (readonly)

Block criterion.



31
32
33
# File 'lib/assay/assertor.rb', line 31

def block
  @block
end

#criteriaObject (readonly)

The criteria for applying the assertor.



26
27
28
# File 'lib/assay/assertor.rb', line 26

def criteria
  @criteria
end

Instance Method Details

#!@Object

TODO:

Should this be @! method instead?

Create a negated form of the matcher.



103
104
105
# File 'lib/assay/assertor.rb', line 103

def !@
  dup.negate!
end

#assert!(subject, &block) ⇒ Object Also known as: =~, ===



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/assay/assertor.rb', line 56

def assert!(subject, &block)
  # technically this needs to be controlled by the assay class
  if block.nil? && Proc === subject
    block   = subject
    subject = NA
  end

  arguments, block = complete_criteria(subject, &block)

  if @not
    @assay.refute!(*arguments, &block)
  else
    @assay.assert!(*arguments, &block)
  end
end

#assert_message(subject, &block) ⇒ Object Also known as: failure_message_for_should

Assertion message. This is only used by RSpec compatibility methods.



119
120
121
122
# File 'lib/assay/assertor.rb', line 119

def assert_message(subject, &block)
  arguments, block = complete_criteria(subject, &block)
  @assay.assert_message(*arguments, &block)
end

#fail?(subject, &block) ⇒ Boolean Also known as: !=, does_not_match?

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/assay/assertor.rb', line 49

def fail?(subject, &block)
  arguments, block = complete_criteria(subject, &block)
  @not ^ @assay.fail?(*arguments, &block)
end

#notObject

TODO:

Best name for this method?

Create a negated form of the matcher.



112
113
114
# File 'lib/assay/assertor.rb', line 112

def not
  dup.negate!
end

#not?Boolean

Is the assertor negated?

Returns:

  • (Boolean)


36
37
38
# File 'lib/assay/assertor.rb', line 36

def not?
  @not
end

#pass?(subject, &block) ⇒ Boolean Also known as: ==, matches?

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/assay/assertor.rb', line 42

def pass?(subject, &block)
  arguments, block = complete_criteria(subject, &block)
  @not ^ @assay.pass?(*arguments, &block)
end

#refute!(subject, &block) ⇒ Object Also known as: !~



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/assay/assertor.rb', line 74

def refute!(subject, &block)
  # technically this needs to be controlled by the assay class
  if block.nil? && Proc === subject
    block   = subject
    subject = NA
  end

  arguments, block = complete_criteria(subject, &block)

  if @not
    @assay.assert!(*arguments, &block)
  else
    @assay.refute!(*arguments, &block)
  end
end

#refute_message(subject, &block) ⇒ Object Also known as: failure_message_for_should_not

Refutation message. This is only used by RSpec compatibility methods.



127
128
129
130
# File 'lib/assay/assertor.rb', line 127

def refute_message(subject, &block)
  arguments, block = complete_criteria(subject, &block)
  @assay.refute_message(*arguments, &block)
end