Class: Given::Expectation

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/given/expectation.rb

Direct Known Subclasses

NegativeExpectation

Instance Method Summary collapse

Constructor Details

#initialize(value, test_case) ⇒ Expectation

Returns a new instance of Expectation.



5
6
7
8
# File 'lib/given/expectation.rb', line 5

def initialize(value, test_case)
  @value = value
  @test_case = test_case
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/given/expectation.rb', line 72

def method_missing(sym, *args, &block)
  method_name = sym.to_s
  if method_name =~ /\?$/
    check(lambda { |value| value.send(sym, *args, &block) },
      "<%s> expected to %sbe %s",
      method_name[0..-2])
  else
    fail Given::UsageError.new("cannot expect anything about #{sym}")
  end
end

Instance Method Details

#<(other) ⇒ Object



43
44
45
46
47
# File 'lib/given/expectation.rb', line 43

def <(other)
  check(lambda { |value| value < other },
    "<%s> expected to %sbe less than\n<%s>.\n",
    other.inspect)
end

#<=(other) ⇒ Object



49
50
51
52
53
# File 'lib/given/expectation.rb', line 49

def <=(other)
  check(lambda { |value| value <= other },
    "<%s> expected to %sbe less than or equal to\n<%s>.\n",
    other.inspect)
end

#==(other) ⇒ Object



30
31
32
33
34
35
# File 'lib/given/expectation.rb', line 30

def ==(other)
  check(
    lambda { |value| value == other },
    "<%s> expected to %sbe equal to\n<%s>.\n",
    other.inspect)
end

#=~(pattern) ⇒ Object



61
62
63
64
65
# File 'lib/given/expectation.rb', line 61

def =~(pattern)
  check(lambda { |value| value =~ pattern },
    "<%s> expected to %sbe matched by\n<%s>.\n",
    pattern.inspect)
end

#>(other) ⇒ Object



37
38
39
40
41
# File 'lib/given/expectation.rb', line 37

def >(other)
  check(lambda { |value| value > other },
    "<%s> expected to %sbe greater than\n<%s>.\n",
    other.inspect)
end

#>=(other) ⇒ Object



55
56
57
58
59
# File 'lib/given/expectation.rb', line 55

def >=(other)
  check(lambda { |value| value >= other },
    "<%s> expected to %sbe greater than or equal to\n<%s>.\n",
    other.inspect)
end

#check(condition, msg, *args) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/given/expectation.rb', line 18

def check(condition, msg, *args)
  bool = evaluate_condition(&condition)
  @test_case.given_check(bool,
    msg,
    [@value.inspect, twist] + args)
  true
end

#evaluate_condition {|@value| ... } ⇒ Object

Yields:

  • (@value)


14
15
16
# File 'lib/given/expectation.rb', line 14

def evaluate_condition
  yield(@value)
end

#nil?Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/given/expectation.rb', line 67

def nil?
  check(lambda { |value| value.nil? },
    "<%s> expected to %sbe nil")
end

#notObject



10
11
12
# File 'lib/given/expectation.rb', line 10

def not()
  NegativeExpectation.new(@value, @test_case)
end

#twistObject



26
27
28
# File 'lib/given/expectation.rb', line 26

def twist
  ""
end