Module: Moon::Test::Assert

Included in:
Moon::Test, SpecSuite
Defined in:
lib/moon/packages/test/assert.rb

Overview

Basic Assertion Framework

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.fail_on_assert_errorObject

Returns the value of attribute fail_on_assert_error


15
16
17
# File 'lib/moon/packages/test/assert.rb', line 15

def fail_on_assert_error
  @fail_on_assert_error
end

Instance Attribute Details

#assertion_errorsObject (readonly)

Returns the value of attribute assertion_errors


24
25
26
# File 'lib/moon/packages/test/assert.rb', line 24

def assertion_errors
  @assertion_errors
end

#assertion_quietObject

whetehr or not to print out the err, only valid if fail_on_assert_error is false


22
23
24
# File 'lib/moon/packages/test/assert.rb', line 22

def assertion_quiet
  @assertion_quiet
end

#assertionsObject (readonly)

Returns the value of attribute assertions


23
24
25
# File 'lib/moon/packages/test/assert.rb', line 23

def assertions
  @assertions
end

#fail_on_assert_errorObject

Returns the value of attribute fail_on_assert_error


20
21
22
# File 'lib/moon/packages/test/assert.rb', line 20

def fail_on_assert_error
  @fail_on_assert_error
end

Instance Method Details

#assert(obj, msg = nil) ⇒ Object

Parameters:


75
76
77
78
# File 'lib/moon/packages/test/assert.rb', line 75

def assert(obj, msg = nil)
  @assertions += 1
  obj ? true : raise_assert_error(msg)
end

#assert_ary_include(expected, ary, msg = nil) ⇒ Object

extended


217
218
219
220
# File 'lib/moon/packages/test/assert.rb', line 217

def assert_ary_include(expected, ary, msg = nil)
  assert_true(ary.include?(expected),
              msg || "expected Array of to include #{expected.inspect}.")
end

#assert_ary_size(expected, ary, msg = nil) ⇒ Object

extended


211
212
213
214
# File 'lib/moon/packages/test/assert.rb', line 211

def assert_ary_size(expected, ary, msg = nil)
  assert_equal(expected, ary.size,
               msg || "expected Array of size #{expected.inspect}.")
end

#assert_const_defined(constname, parent = nil, msg = nil) ⇒ Object

Parameters:


225
226
227
228
229
# File 'lib/moon/packages/test/assert.rb', line 225

def assert_const_defined(constname, parent = nil, msg = nil)
  parent ||= ::Object
  assert_true(parent.const_defined?(constname),
              msg || "expected #{constname.inspect} to be defined under #{parent.inspect}.")
end

#assert_equal(a, b, msg = nil) ⇒ Object

Parameters:


98
99
100
# File 'lib/moon/packages/test/assert.rb', line 98

def assert_equal(a, b, msg = nil)
  assert_true(a == b, msg || "expected #{a.inspect} == #{b.inspect}.")
end

#assert_false(obj, msg = nil) ⇒ Object

Parameters:


90
91
92
# File 'lib/moon/packages/test/assert.rb', line 90

def assert_false(obj, msg = nil)
  assert_true(!obj, msg || "expected #{obj.inspect} to be false.")
end

#assert_float(exp, act, msg = nil) ⇒ Object

Parameters:


187
188
189
190
# File 'lib/moon/packages/test/assert.rb', line 187

def assert_float(exp, act, msg = nil)
  assert_true(check_float(exp, act),
              msg || "expected #{exp.inspect} to equal #{act.inspect}.")
end

#assert_kind_of(klass, obj, msg = nil) ⇒ Object

Parameters:

  • klass (Class)
  • obj (Object)
  • msg (String, nil) (defaults to: nil)

162
163
164
165
# File 'lib/moon/packages/test/assert.rb', line 162

def assert_kind_of(klass, obj, msg = nil)
  assert_true(obj.kind_of?(klass),
              msg || "expected #{obj.inspect} to be kind_of? #{klass.inspect}.")
end

#assert_kind_of_any(klasses, obj, msg = nil) ⇒ Object

Parameters:


169
170
171
172
# File 'lib/moon/packages/test/assert.rb', line 169

def assert_kind_of_any(klasses, obj, msg = nil)
  assert_true(klasses.any? { |klass| obj.kind_of?(klass) },
              msg || "expected #{obj.inspect} to be kind_of any? #{klasses.inspect}")
end

#assert_method_defined(methodname, obj, msg = nil) ⇒ Object

Parameters:


242
243
244
245
# File 'lib/moon/packages/test/assert.rb', line 242

def assert_method_defined(methodname, obj, msg = nil)
  assert_true(obj.method_defined?(methodname),
              msg || "expected #{obj.inspect} to define #{methodname.inspect}.")
end

#assert_nil(a, msg = nil) ⇒ Object

Parameters:


129
130
131
# File 'lib/moon/packages/test/assert.rb', line 129

def assert_nil(a, msg = nil)
  assert_equal(a, nil, msg)
end

#assert_not_equal(a, b, msg = nil) ⇒ Object

Parameters:


106
107
108
# File 'lib/moon/packages/test/assert.rb', line 106

def assert_not_equal(a, b, msg = nil)
  assert_true(a != b, msg || "expected #{a.inspect} != #{b.inspect}.")
end

#assert_not_kind_of(klass, obj, msg = nil) ⇒ Object

Parameters:

  • klass (Class)
  • obj (Object)
  • msg (String, nil) (defaults to: nil)

178
179
180
181
# File 'lib/moon/packages/test/assert.rb', line 178

def assert_not_kind_of(klass, obj, msg = nil)
  assert_false(obj.kind_of?(klass),
               msg || "expected #{obj.inspect} not to be kind_of? #{klass.inspect}.")
end

#assert_not_raise(msg = nil) { ... } ⇒ Object

Parameters:

  • msg (String, nil) (defaults to: nil)

Yields:


149
150
151
152
153
154
155
156
# File 'lib/moon/packages/test/assert.rb', line 149

def assert_not_raise(msg = nil)
  @assertions += 1
  begin
    yield
  rescue Exception => e
    raise_assert_error(msg || 'expected nothing to be raised.', e)
  end
end

#assert_not_same(a, b, msg = nil) ⇒ Object

Parameters:


122
123
124
# File 'lib/moon/packages/test/assert.rb', line 122

def assert_not_same(a, b, msg = nil)
  assert_false(a.equal?(b), msg || "expected #{a.inspect} to not #equal? #{b.inspect}.")
end

#assert_raise(klass, msg = nil) { ... } ⇒ Object

Parameters:

  • klass (Class<Exception>)
  • msg (String, nil) (defaults to: nil)

Yields:


137
138
139
140
141
142
143
144
# File 'lib/moon/packages/test/assert.rb', line 137

def assert_raise(klass, msg = nil)
  begin
    yield
  rescue Exception => e
    return assert_equal(e.class, klass, msg || "expected #{klass.inspect} to be raised")
  end
  raise_assert_error("expected #{klass.inspect} to be raised, but nothing was raised.")
end

#assert_respond_to(methodname, obj, msg = nil) ⇒ Object

Parameters:


234
235
236
237
# File 'lib/moon/packages/test/assert.rb', line 234

def assert_respond_to(methodname, obj, msg = nil)
  assert_true(obj.respond_to?(methodname),
              msg || "expected #{obj.inspect} to respond_to?(#{methodname.inspect}).")
end

#assert_same(a, b, msg = nil) ⇒ Object

Parameters:


114
115
116
# File 'lib/moon/packages/test/assert.rb', line 114

def assert_same(a, b, msg = nil)
  assert_true(a.equal?(b), msg || "expected #{a.inspect} to #equal? #{b.inspect}.")
end

#assert_true(obj, msg = nil) ⇒ Object

Parameters:


83
84
85
# File 'lib/moon/packages/test/assert.rb', line 83

def assert_true(obj, msg = nil)
  assert(!!obj, msg || "expected #{obj.inspect} to be true.")
end

#catch_assertion_errors(&block) ⇒ Object


37
38
39
# File 'lib/moon/packages/test/assert.rb', line 37

def catch_assertion_errors(&block)
  @assertion_catcher = block
end

#check_float(a, b) ⇒ Object

Swiped from mruby/test/assert Performs fuzzy check for equality on methods returning floats

Parameters:


198
199
200
201
202
203
204
205
206
207
208
# File 'lib/moon/packages/test/assert.rb', line 198

def check_float(a, b)
  #tolerance = 1e-12
  tolerance = 1e-2
  a = a.to_f
  b = b.to_f
  if a.finite? and b.finite?
    (a-b).abs < tolerance
  else
    true
  end
end