Module: FunWith::Testing::Assertions::Basics

Defined in:
lib/fun_with/testing/assertions/basics.rb

Instance Method Summary collapse

Instance Method Details

#assert_at_least(reference_value, amount, msg = nil) ⇒ Object



168
169
170
171
172
# File 'lib/fun_with/testing/assertions/basics.rb', line 168

def assert_at_least( reference_value, amount, msg = nil )
  msg = message(msg) { "Value must be at least #{reference_value}. #{mu_pp(amount)} is too small." }
  
  assert( amount >= reference_value, msg )
end

#assert_at_most(reference_value, amount, msg = nil) ⇒ Object



174
175
176
177
178
# File 'lib/fun_with/testing/assertions/basics.rb', line 174

def assert_at_most( reference_value, amount, msg = nil )
  msg = message(msg) { "Value can be at most #{reference_value}. #{mu_pp(amount)} is too large." }
  
  assert( amount <= reference_value, msg )
end

#assert_blank(obj, msg = nil) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/fun_with/testing/assertions/basics.rb', line 112

def assert_blank( obj, msg = nil )
  assert_responds_to_blank( obj )
  
  msg = message(msg) { "#{mu_pp(obj)} should be blank." }
  
  assert( (obj.respond_to?(:blank?) && obj.blank?) || (obj.respond_to?(:fwf_blank?) && obj.fwf_blank?), msg )
end

#assert_constant_defined(const, msg = nil) ⇒ Object



290
291
292
293
294
295
296
297
# File 'lib/fun_with/testing/assertions/basics.rb', line 290

def assert_constant_defined( const, msg = nil )
  begin
    Object.const_get( const )
    assert true
  rescue NameError
    assert false, message(msg){ "expected constant is not defined: #{const}" }
  end
end

#assert_doesnt_match(string, regexp_or_string, msg = nil) ⇒ Object Also known as: refute_matches



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/fun_with/testing/assertions/basics.rb', line 134

def assert_doesnt_match( string, regexp_or_string, msg = nil)
  msg = message(msg) { "<#{mu_pp(string)}> should NOT match string/regex <#{mu_pp(regexp_or_string)}>" }
  
  if regexp_or_string.is_a?(Regexp)
    assert_nil string.match(regexp_or_string), msg
  elsif regexp_or_string.is_a?(String)
    refute string.include?(regexp_or_string), msg
  else
    raise ArgumentError.new( "assert_doesnt_match takes a regular expression or string as second argument, not #{regexp_or_string}(#{regexp_or_string.class})")
  end
  
  true
end

#assert_equal_length(expected, actual, msg = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
195
# File 'lib/fun_with/testing/assertions/basics.rb', line 186

def assert_equal_length( expected, actual, msg = nil )
  assert_respond_to expected, :length, message(nil){ "#{mu_pp(expected)} (expected value) doesn't respond to length()." }
  assert_respond_to actual, :length, message(nil){ "#{mu_pp(actual)} (actual value) doesn't respond to length()." }
  
  msg = message(msg){ 
    "items should be of equal length: expected length: <#{mu_pp(expected.length)}>, actual length: <#{mu_pp(actual.length)}>"
  }
  
  assert_equal expected.length, actual.length, msg
end

#assert_equality_of_methods(expected, actual, *methods) ⇒ Object

Tries the given methods on both objects, reports on differing results Doesn't take a custom message. Methods given must take zero arguments.



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/fun_with/testing/assertions/basics.rb', line 236

def assert_equality_of_methods( expected, actual, *methods )
  failed = false
  
  results_msg = {}
  results_msg[:expected] = "The following methods were not equal: \nExpected: #{mu_pp(expected)}"
  results_msg[:actual]   = "\n--------------------\nActual: #{mu_pp(actual)}"
  
  
  for method in methods
    no_error_on_method = true
    responses = {}
    
    for obj, response_sym in [[expected, :expected], [actual, :actual]]
      responses[response_sym] = begin
                                  obj.send( method )
                                rescue StandardError => e
                                  e
                                end
                                
      if responses[response_sym].is_a?( StandardError )
        failed = true
        no_error_on_method = false
        results_msg[response_sym] << "\n\t#{method}(): ERROR: #{responses[response_sym].class} #{responses[response_sym].message}"
      end
    end
    
    if responses[:expected] != responses[:actual] && no_error_on_method
      failed = true
      
      for response_sym in [:expected, :actual]
        results_msg[response_sym] << "\n\t#{method}(): #{responses[response_sym].inspect}"
      end
    end
  end
    
  
  assert !failed, results_msg[:expected] + results_msg[:actual]
end

#assert_false(actual, msg = nil) ⇒ Object

rejects anything but an actual false, instance of the FalseClass



82
83
84
85
# File 'lib/fun_with/testing/assertions/basics.rb', line 82

def assert_false( actual, msg = nil )
  msg = message(msg) { "should be false (instance of FalseClass), not <#{mu_pp(actual)}>" }
  assert actual == false, msg
end

#assert_greater_than(reference_value, amount, msg = nil) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/fun_with/testing/assertions/basics.rb', line 151

def assert_greater_than( reference_value, amount, msg = nil )
  msg = message(msg){
    "second argument <#{mu_pp(amount)}> should be greater than reference value <#{mu_pp(reference_value)}>"
  }

  assert amount > reference_value, msg
end

#assert_has_instance_method(object, instance_method, msg = nil) ⇒ Object



275
276
277
278
# File 'lib/fun_with/testing/assertions/basics.rb', line 275

def assert_has_instance_method( object, instance_method, msg = nil )
  msg = message(msg){ "object #{mu_pp(object)} should respond to #{instance_method.inspect}" }
  assert object.instance_methods.include?( instance_method ), msg
end

#assert_includes_module(mod_or_class, mod, msg = nil) ⇒ Object

Which order makes more sense for the arguments Assert that the first argument includes the module specified in the second argument

Raises:

  • (ArgumentError)


301
302
303
304
305
306
307
308
309
# File 'lib/fun_with/testing/assertions/basics.rb', line 301

def assert_includes_module( mod_or_class, mod, msg = nil )
  # Make sure we've been passed the proper sort of objects
  assert mod_or_class.respond_to?( :included_modules, "#{mod_or_class} doesn't respond to included_modules()" )
  raise ArgumentError.new( "#{mod} is not a module" ) unless mod.kind_of?( Module )

  msg = message(msg){ "#{mod_or_class} should have included module #{mod}" }
  
  assert mod_or_class.included_modules.include?(mod), msg
end

#assert_length(expected, actual, msg = nil) ⇒ Object

expected can be a numeric range



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/fun_with/testing/assertions/basics.rb', line 212

def assert_length( expected, actual, msg = nil )
  
  no_response_msg = message(nil){ "<#{mu_pp(actual)}> doesn't respond to .length()" }
  
  assert_respond_to actual, :length, no_response_msg
  
  case expected
  when Range
    msg = message(msg){ 
      "<#{mu_pp(actual)}> has a length of #{mu_pp(actual.length)}. Length must be between <#{mu_pp(expected.min)}> and <#{mu_pp(expected.max)}>"
    }
    
    assert_at_least expected.min, actual.length, msg
    assert_at_most  expected.max, actual.length, msg
  when Integer
    msg = message(msg){ "<#{mu_pp(actual)}> has a length of <#{mu_pp(actual.length)}>. Expected length was <#{mu_pp(expected)}>" }
    assert_equal( expected, actual.length, msg )
  else
    flunk( "Bad reference value (first argument: #{expected.inspect}) to assert_length" )
  end
end

#assert_less_than(reference_value, amount, msg = nil) ⇒ Object

read as "assert less than 5, "



160
161
162
163
164
165
166
# File 'lib/fun_with/testing/assertions/basics.rb', line 160

def assert_less_than( reference_value, amount, msg = nil )
  msg = message(msg){ 
    "second argument <#{mu_pp(amount)} should be less than reference value <#{mu_pp(reference_value)}>" 
  }

  assert amount < reference_value, msg
end

#assert_matches(string, regexp_or_string, msg = nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fun_with/testing/assertions/basics.rb', line 120

def assert_matches( string, regexp_or_string, msg = nil)
  msg = message(msg) { "<#{mu_pp(string)}> should match regex <#{mu_pp(regexp_or_string)}>" }

  if regexp_or_string.is_a?(Regexp)
    assert string.match(regexp_or_string), msg
  elsif regexp_or_string.is_a?(String)
    assert string.include?(regexp_or_string), msg
  else
    raise ArgumentError.new( "assert_matches takes a regular expression or string as second argument, not #{regexp_or_string}(#{regexp_or_string.class})")
  end
  
  true   
end

#assert_negative(actual, msg = nil) ⇒ Object



47
48
49
50
# File 'lib/fun_with/testing/assertions/basics.rb', line 47

def assert_negative( actual, msg = nil )
  msg = message(msg) { "should be negative, not <#{mu_pp(actual)}>" }
  assert actual < 0, msg
end

#assert_nil(actual, msg = nil) ⇒ Object



92
93
94
95
# File 'lib/fun_with/testing/assertions/basics.rb', line 92

def assert_nil( actual, msg = nil )
  msg = message(msg) { "should be nil, not <#{mu_pp(actual)}>" }
  assert actual == nil, msg
end

#assert_not_negative(actual, msg = nil) ⇒ Object Also known as: refute_negative



52
53
54
55
# File 'lib/fun_with/testing/assertions/basics.rb', line 52

def assert_not_negative( actual, msg = nil )
  msg = message(msg) { "should NOT be negative, (actual) <#{mu_pp(actual)}>" }
  assert actual >= 0, msg
end

#assert_not_nil(actual, msg = nil) ⇒ Object Also known as: refute_nil



97
98
99
100
# File 'lib/fun_with/testing/assertions/basics.rb', line 97

def assert_not_nil( actual, msg = nil )
  msg = message(msg) { "should not be nil" }
  assert actual != nil, msg
end

#assert_not_one(actual, msg = nil) ⇒ Object Also known as: refute_one



40
41
42
43
# File 'lib/fun_with/testing/assertions/basics.rb', line 40

def assert_not_one( actual, msg = nil )
  msg = message(msg) { "should be 1, not <#{mu_pp(actual)}>" }
  assert actual != 1, msg
end

#assert_not_positive(actual, msg = nil) ⇒ Object Also known as: refute_positive



64
65
66
67
# File 'lib/fun_with/testing/assertions/basics.rb', line 64

def assert_not_positive( actual, msg = nil )
  msg = message(msg) { "should NOT be positive, (actual) <#{mu_pp(actual)}>" }
  assert actual <= 0, msg
end

#assert_not_zero(actual, msg = nil) ⇒ Object Also known as: refute_zero

Interesting thing about message() : returns a proc that, when called, returns the message string. I don't quite understand why it's done that way, but to add a line to an existing msg proc, do msg = message(msg){"line to add..."}



23
24
25
26
# File 'lib/fun_with/testing/assertions/basics.rb', line 23

def assert_not_zero( actual, msg = nil )
  msg = message(msg) { "Expected #{mu_pp(actual)} to not be zero" }
  assert actual != 0, msg
end

#assert_nothing_raised(msg = nil, &block) ⇒ Object



280
281
282
283
284
285
286
287
288
# File 'lib/fun_with/testing/assertions/basics.rb', line 280

def assert_nothing_raised( msg = nil, &block )
  begin 
    yield if block_given?
    assert true
  rescue Exception => e
    msg = message(msg){ "block should not raise a #{mu_pp(e.class)} (message: #{e.message})"}
    assert false, msg
  end
end

#assert_one(actual, msg = nil) ⇒ Object



35
36
37
38
# File 'lib/fun_with/testing/assertions/basics.rb', line 35

def assert_one( actual, msg = nil )
  msg = message(msg) { "should be 1, not <#{mu_pp(actual)}>" }
  assert actual == 1, msg
end

#assert_positive(actual, msg = nil) ⇒ Object



59
60
61
62
# File 'lib/fun_with/testing/assertions/basics.rb', line 59

def assert_positive( actual, msg = nil )
  msg = message(msg) { "should be positive, not <#{mu_pp(actual)}>" }
  assert actual > 0, msg
end

#assert_responds_to_blank(obj, message = nil) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/fun_with/testing/assertions/basics.rb', line 104

def assert_responds_to_blank( obj, message = nil )
  msg = message(msg){
    "<#{mu_pp(obj)}> does not respond to :blank? or :fwf_blank? methods."
  }
  
  assert obj.respond_to?(:blank?) || obj.respond_to?( :fwf_blank? ), msg
end

#assert_times_are_close(t1, t2, window = 1, msg = nil) ⇒ Object

I think "assert_delta_in_range" already does this for floats



181
182
183
184
# File 'lib/fun_with/testing/assertions/basics.rb', line 181

def assert_times_are_close( t1, t2, window = 1, msg = nil)
  msg = message(msg) { "times should be within #{mu_pp(window)} second of each other." }
  assert (t1 - t2).abs <= window
end

#assert_true(actual, msg = nil) ⇒ Object



71
72
73
74
# File 'lib/fun_with/testing/assertions/basics.rb', line 71

def assert_true( actual, msg = nil )
  msg = message(msg) { "should be true (TrueClass), not <#{mu_pp(actual)}>" }
  assert actual == true, msg
end

#assert_unequal_length(expected, actual, msg = nil) ⇒ Object Also known as: refute_equal_length



197
198
199
200
201
202
203
204
205
206
# File 'lib/fun_with/testing/assertions/basics.rb', line 197

def assert_unequal_length( expected, actual, msg = nil )
  msg = message(msg){ 
    "items should be of equal length: expected: <#{mu_pp(expected.length)}>, actual: <#{mu_pp(actual.length)}>"
  }
  
  assert_respond_to expected, :length, message(nil){ "#{mu_pp(expected)} (expected value) doesn't respond to length()." }
  assert_respond_to actual, :length, message(nil){ "#{mu_pp(actual)} (actual value) doesn't respond to length()." }
  
  assert_equal expected.length, actual.length, message
end

#assert_zero(actual, msg = nil) ⇒ Object



30
31
32
33
# File 'lib/fun_with/testing/assertions/basics.rb', line 30

def assert_zero( actual, msg = nil )
  msg = message(msg) { "should be zero, not <#{mu_pp(actual)}>" }
  assert actual == 0, msg
end

#refute_false(actual, msg = nil) ⇒ Object



87
88
89
90
# File 'lib/fun_with/testing/assertions/basics.rb', line 87

def refute_false( actual, msg = nil )
  msg = message(msg) { "shouldn't be false" }
  assert actual != false, msg
end

#refute_true(actual, msg = nil) ⇒ Object



76
77
78
79
# File 'lib/fun_with/testing/assertions/basics.rb', line 76

def refute_true( actual, msg = nil )
  msg = message(msg) { "shouldn't be true (TrueClass)" }
  assert actual != true, msg
end