Class: Minitest::ValueMonad

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

Direct Known Subclasses

SelfMonad

Constant Summary collapse

VERSION =
"1.0.4"

Instance Method Summary collapse

Constructor Details

#initialize(v) ⇒ ValueMonad

Returns a new instance of ValueMonad.



36
37
38
39
# File 'lib/minitest/bacon.rb', line 36

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



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/minitest/bacon.rb', line 78

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



68
69
70
71
72
73
74
75
76
# File 'lib/minitest/bacon.rb', line 68

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



47
48
49
50
51
52
53
54
55
# File 'lib/minitest/bacon.rb', line 47

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



64
65
66
# File 'lib/minitest/bacon.rb', line 64

def be_false
  assert { !@val }
end

#be_trueObject



60
61
62
# File 'lib/minitest/bacon.rb', line 60

def be_true
  assert { @val }
end

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

Returns:

  • (Boolean)


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

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

#equal(value) ⇒ Object



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

def equal(value)         self == value      end

#flunk(reason = "Flunked") ⇒ Object



99
100
101
# File 'lib/minitest/bacon.rb', line 99

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

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



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

def identical_to(value)  self.equal? value  end

#match(value) ⇒ Object



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

def match(value)         self =~ value      end

#not(*args, &block) ⇒ Object



41
42
43
44
45
# File 'lib/minitest/bacon.rb', line 41

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

  be(*args, &block)
end

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

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

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