Class: Minitest::ValueMonad

Inherits:
Object show all
Defined in:
lib/minitest/bacon.rb

Direct Known Subclasses

SelfMonad

Constant Summary collapse

VERSION =
"1.0.6"

Instance Method Summary collapse

Constructor Details

#initialize(v) ⇒ ValueMonad

Returns a new instance of ValueMonad.



50
51
52
53
# File 'lib/minitest/bacon.rb', line 50

def initialize v
  @val = v
  @pos = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/minitest/bacon.rb', line 92

def method_missing(name, *args, &block)
  name = name.to_s.sub(/^be_/, '')
  name = "#{name}?" if name =~ /\w[^?]\z/

  msg = []
  msg << "not" unless @pos
  msg << @val.inspect << ".#{name}"
  msg << "(#{args.map(&:inspect).join ", "}) failed"

  assert(msg.join) { @val.__send__(name, *args, &block) }
end

Instance Method Details

#assert(msg = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/minitest/bacon.rb', line 82

def assert msg = nil
  r = yield @val

  Minitest.poke
  msg ||= "boom"
  raise Minitest::Assertion, msg if @pos ^ r

  r
end

#be(*args, &block) ⇒ Object Also known as: a, an



61
62
63
64
65
66
67
68
69
# File 'lib/minitest/bacon.rb', line 61

def be(*args, &block)
  if args.empty?
    self
  else
    block = args.shift unless block_given?
    block ||= lambda { @val.send(*args) }
    assert(&block)
  end
end

#be_falseObject



78
79
80
# File 'lib/minitest/bacon.rb', line 78

def be_false
  assert { !@val }
end

#be_trueObject



74
75
76
# File 'lib/minitest/bacon.rb', line 74

def be_true
  assert { @val }
end

#change?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


111
# File 'lib/minitest/bacon.rb', line 111

def change?(*args, &block); block.change?(*args); end

#equal(value) ⇒ Object



104
# File 'lib/minitest/bacon.rb', line 104

def equal(value)         self == value      end

#flunk(reason = "Flunked") ⇒ Object



113
114
115
# File 'lib/minitest/bacon.rb', line 113

def flunk(reason="Flunked")
  raise Minitest::Assertion, reason
end

#identical_to(value) ⇒ Object Also known as: same_as



106
# File 'lib/minitest/bacon.rb', line 106

def identical_to(value)  self.equal? value  end

#match(value) ⇒ Object



105
# File 'lib/minitest/bacon.rb', line 105

def match(value)         self =~ value      end

#not(*args, &block) ⇒ Object



55
56
57
58
59
# File 'lib/minitest/bacon.rb', line 55

def not(*args, &block)
  @pos = !@pos

  be(*args, &block)
end

#raise?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


109
# File 'lib/minitest/bacon.rb', line 109

def raise?(*args,  &block); block.raise?(*args);  end

#throw?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


110
# File 'lib/minitest/bacon.rb', line 110

def throw?(*args,  &block); block.throw?(*args);  end