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



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

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



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

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



98
99
100
101
102
103
104
# File 'lib/fun_with/testing/assertions/basics.rb', line 98

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_doesnt_match(string, regexp_or_string, msg = nil) ⇒ Object Also known as: refute_matches



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_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



172
173
174
175
176
177
178
179
180
181
# File 'lib/fun_with/testing/assertions/basics.rb', line 172

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.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/fun_with/testing/assertions/basics.rb', line 222

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



68
69
70
71
# File 'lib/fun_with/testing/assertions/basics.rb', line 68

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



137
138
139
140
141
142
143
# File 'lib/fun_with/testing/assertions/basics.rb', line 137

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



261
262
263
264
# File 'lib/fun_with/testing/assertions/basics.rb', line 261

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_length(expected, actual, msg = nil) ⇒ Object

‘expected` can be a numeric range



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/fun_with/testing/assertions/basics.rb', line 198

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, <test value>”



146
147
148
149
150
151
152
# File 'lib/fun_with/testing/assertions/basics.rb', line 146

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



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fun_with/testing/assertions/basics.rb', line 106

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



33
34
35
36
# File 'lib/fun_with/testing/assertions/basics.rb', line 33

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



78
79
80
81
# File 'lib/fun_with/testing/assertions/basics.rb', line 78

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



38
39
40
41
# File 'lib/fun_with/testing/assertions/basics.rb', line 38

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



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

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



26
27
28
29
# File 'lib/fun_with/testing/assertions/basics.rb', line 26

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



50
51
52
53
# File 'lib/fun_with/testing/assertions/basics.rb', line 50

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)to add…”`



9
10
11
12
# File 'lib/fun_with/testing/assertions/basics.rb', line 9

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



266
267
268
269
270
271
272
273
274
# File 'lib/fun_with/testing/assertions/basics.rb', line 266

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



21
22
23
24
# File 'lib/fun_with/testing/assertions/basics.rb', line 21

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



45
46
47
48
# File 'lib/fun_with/testing/assertions/basics.rb', line 45

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



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

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



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

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



57
58
59
60
# File 'lib/fun_with/testing/assertions/basics.rb', line 57

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



183
184
185
186
187
188
189
190
191
192
# File 'lib/fun_with/testing/assertions/basics.rb', line 183

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



16
17
18
19
# File 'lib/fun_with/testing/assertions/basics.rb', line 16

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



73
74
75
76
# File 'lib/fun_with/testing/assertions/basics.rb', line 73

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

#refute_true(actual, msg = nil) ⇒ Object



62
63
64
65
# File 'lib/fun_with/testing/assertions/basics.rb', line 62

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