Class: MiniTest::ShouldSyntax

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/should_syntax.rb,
lib/minitest/should_syntax/version.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left) ⇒ ShouldSyntax

Returns a new instance of ShouldSyntax.



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

def initialize(left)
  @left = left
  if test.msg
    blaming test.msg
    test.msg = nil
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/minitest/should_syntax.rb', line 131

def method_missing(meth, *args, &blk)
  result = left.send(:"#{meth}?", *args, &blk)
  method = positive? ? :assert : :refute

  args = [result]
  args << msg  if msg

  test.send method, *args
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



35
36
37
# File 'lib/minitest/should_syntax.rb', line 35

def left
  @left
end

#msgObject (readonly)

Returns the value of attribute msg.



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

def msg
  @msg
end

Class Method Details

.add(extension) ⇒ Object

Includes a module to extend .should with more matchers.



43
44
45
# File 'lib/minitest/should_syntax.rb', line 43

def self.add(extension)
  self.send :include, extension
end

.init(test) ⇒ Object

:nodoc:



38
39
40
# File 'lib/minitest/should_syntax.rb', line 38

def self.init(test) # :nodoc:
  @@test = test
end

.versionObject



3
4
5
# File 'lib/minitest/should_syntax/version.rb', line 3

def self.version
  "1.0.2"
end

Instance Method Details

#<(right) ⇒ Object



80
# File 'lib/minitest/should_syntax.rb', line 80

def <(right)              assert_or_refute :operator, left, :<,  right; end

#<=(right) ⇒ Object



82
# File 'lib/minitest/should_syntax.rb', line 82

def <=(right)             assert_or_refute :operator, left, :<=, right; end

#==(right) ⇒ Object



77
# File 'lib/minitest/should_syntax.rb', line 77

def ==(right)             assert_or_refute :equal, right, left; end

#=~(right) ⇒ Object



78
# File 'lib/minitest/should_syntax.rb', line 78

def =~(right)             assert_or_refute :match, right, left; end

#>(right) ⇒ Object



79
# File 'lib/minitest/should_syntax.rb', line 79

def >(right)              assert_or_refute :operator, left, :>,  right; end

#>=(right) ⇒ Object



81
# File 'lib/minitest/should_syntax.rb', line 81

def >=(right)             assert_or_refute :operator, left, :>=, right; end

#aObject



56
# File 'lib/minitest/should_syntax.rb', line 56

def a()  self; end

#anObject



57
# File 'lib/minitest/should_syntax.rb', line 57

def an() self; end

#assert_or_refute(what, *args, &blk) ⇒ Object



105
106
107
108
# File 'lib/minitest/should_syntax.rb', line 105

def assert_or_refute(what, *args, &blk)
  args << msg
  test.send((positive? ? :"assert_#{what}" : :"refute_#{what}"), *args, &blk)
end

#be(right = nil) ⇒ Object



55
# File 'lib/minitest/should_syntax.rb', line 55

def be(right=nil) self.same(right)  if right; self; end

#blaming(msg) ⇒ Object



74
# File 'lib/minitest/should_syntax.rb', line 74

def blaming(msg);   @msg = msg; self; end

#close(right, d = 0.001) ⇒ Object



102
# File 'lib/minitest/should_syntax.rb', line 102

def close(right, d=0.001)       assert_or_refute :in_delta, right, left, d; end

#emptyObject



89
# File 'lib/minitest/should_syntax.rb', line 89

def empty()               assert_or_refute :empty, left; end

#equal(right) ⇒ Object



100
# File 'lib/minitest/should_syntax.rb', line 100

def equal(right)          self == right; end

#falseObject



65
# File 'lib/minitest/should_syntax.rb', line 65

def false()   true_or_false(false); end

#in_epsilon(right, d = 0.001) ⇒ Object



103
# File 'lib/minitest/should_syntax.rb', line 103

def in_epsilon(right, d=0.001)  assert_or_refute :in_epsilon, right, left, d; end

#include(right) ⇒ Object



83
# File 'lib/minitest/should_syntax.rb', line 83

def include(right)        assert_or_refute :includes, left, right; end

#instance_of(right) ⇒ Object



84
# File 'lib/minitest/should_syntax.rb', line 84

def instance_of(right)    assert_or_refute :instance_of, right, left; end

#kind_of(right) ⇒ Object



85
# File 'lib/minitest/should_syntax.rb', line 85

def kind_of(right)        assert_or_refute :kind_of, right, left; end

#match(right) ⇒ Object



99
# File 'lib/minitest/should_syntax.rb', line 99

def match(right)          self =~ right; end

#messaging(msg) ⇒ Object



75
# File 'lib/minitest/should_syntax.rb', line 75

def messaging(msg); @msg = msg; self; end

#negative?Boolean

Returns:

  • (Boolean)


59
# File 'lib/minitest/should_syntax.rb', line 59

def negative?() @neg; end

#nilObject



86
# File 'lib/minitest/should_syntax.rb', line 86

def nil()                 assert_or_refute :nil, left; end

#notObject



62
# File 'lib/minitest/should_syntax.rb', line 62

def not()       @neg = true; self; end

#positive?Boolean

Returns:

  • (Boolean)


60
# File 'lib/minitest/should_syntax.rb', line 60

def positive?() !@neg; end

#raise(ex = StandardError, &blk) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/minitest/should_syntax.rb', line 123

def raise(ex=StandardError, &blk)
  if positive?
    test.send :assert_raises, ex, msg, &blk
  else
    warn "ShouldSyntax: should.not.raise is not supported"
  end
end

#refute_or_assert(what, *args) ⇒ Object



110
111
112
113
# File 'lib/minitest/should_syntax.rb', line 110

def refute_or_assert(what, *args)
  args << msg
  test.send((negative? ? :"assert_#{what}" : :"refute_#{what}"), *args)
end

#respond_to(right) ⇒ Object



88
# File 'lib/minitest/should_syntax.rb', line 88

def respond_to(right)     assert_or_refute :respond_to, left, right; end

#same(right) ⇒ Object



87
# File 'lib/minitest/should_syntax.rb', line 87

def same(right)           assert_or_refute :same, right, left; end

#satisfy(&blk) ⇒ Object



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

def satisfy(&blk)         assert_or_refute :block, &blk; end

#testObject



61
# File 'lib/minitest/should_syntax.rb', line 61

def test()      @@test; end

#throw(what = nil, &blk) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/minitest/should_syntax.rb', line 115

def throw(what=nil, &blk)
  if positive?
    test.send :assert_throws, what, msg, &blk
  else
    warn "ShouldSyntax: should.not.throw is not supported"
  end
end

#trueObject



64
# File 'lib/minitest/should_syntax.rb', line 64

def true()    true_or_false(true); end

#true_or_false(bool) ⇒ Object



67
68
69
70
71
72
# File 'lib/minitest/should_syntax.rb', line 67

def true_or_false(bool)
  val = !! left
  val = !val  if bool == false
  method = (positive? ? :"assert" : :"refute")
  test.send method, val, [msg, 'Expected to be falsy'].compact.join("\n")
end