1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
require File.dirname(__FILE__) + '/test_helper.rb'
class Exister
def initialize(what)
@what = what
end
def exist?
@what
end
end
class TestTruthExpectations < Test::Unit::TestCase
def setup
@obj = Object.new
end
def test_equal
3.should equal(3)
end
def test_negative_equal
instance = String.new
instance.should_not equal(String.new)
end
def test_equal_fail
lambda {
3.should equal(4)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_negative_equal_fail
lambda {
3.should_not equal(3)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_eql
3.should eql(3)
end
def test_eql_array
[1,2,3].should eql([1,2,3])
end
def test_negative_eql
3.should_not eql(9)
end
def test_eql_fail
lambda {
3.should eql(13)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_negative_eql_fail
lambda {
3.should_not eql(3)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_exists
thing = Exister.new(true)
thing.should exist
end
def test_negative_exists
thing = Exister.new(false)
thing.should_not exist
end
def test_exists_fail
lambda {
thing = Exister.new(false)
thing.should exist
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_negative_exists_fail
lambda {
thing = Exister.new(true)
thing.should_not exist
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_be
true.should be(true)
end
def test_be_array
[1,2,3].should be([1,2,3])
end
def test_negative_be
true.should_not be(false)
end
def test_be_fail
lambda {
true.should be(false)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_be_close
(5.0 - 2.0).should be_close(3.0)
end
def test_be_close_with_delta
(5.0 - 2.0).should be_close(3.0, 0.2)
end
def test_be_close_fail
lambda {
(19.0 - 13.0).should be_close(33.04)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_be_close_with_delta_fail
lambda {
(19.0 - 13.0).should be_close(6.0, 0.0)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_satisfy
13.should satisfy(lambda {|i| i < 15})
end
def test_negative_satisfy
13.should_not satisfy(lambda {|i| i < 10})
end
def test_satisfy_fail
lambda {
13.should satisfy(lambda {|i| i > 15})
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_negative_satisfy_fail
lambda {
13.should_not satisfy(lambda {|i| i < 15})
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_equal_fail_message
obj = equal(4)
obj.matches?(5)
obj.failure_message.should be("Expected 5 to return true for equal?, with '4'.")
end
def test_equal_negative_fail_message
obj = equal(5)
obj.matches?(5)
obj.negative_failure_message.should be("Expected 5 to not return true for equal?, with '5'.")
end
def test_eql_fail_message
obj = eql(4)
obj.matches?(5)
obj.failure_message.should be("Expected 5 to return true for eql?, with '4'.")
end
def test_eql_negative_fail_message_for_eql
obj = eql(5)
obj.matches?(5)
obj.negative_failure_message.should be("Expected 5 to not return true for eql?, with '5'.")
end
def test_exist_fail_message
obj = exist
obj.matches?(Exister.new(false))
obj.failure_message.should =~ /Expected #<(.*)> to return true for exist?./
end
def test_exist_negative_fail_message
obj = exist
obj.matches?(Exister.new(true))
obj.negative_failure_message.should =~ /Expected #<(.*)> to not return true for exist?./
end
def test_be_close_fail_message
obj = be_close(3.0)
obj.matches?(6.0)
obj.failure_message.should be("Expected 6.0 to be close to 3.0 (delta: 0.3).")
end
def test_be_close_negative_fail_message
obj = be_close(5.0)
obj.matches?(5.0)
obj.negative_failure_message.should be("Expected 5.0 to not be close to 5.0 (delta: 0.3).")
end
def test_be_fail_message
obj = be(4)
obj.matches?(5)
obj.failure_message.should be("Expected 5 to be 4.")
end
def test_be_negative_fail_message
obj = be(5)
obj.matches?(5)
obj.negative_failure_message.should be("Expected 5 to not be 5.")
end
def test_satisfy_fail_message
obj = satisfy(lambda {|x| x == 4})
obj.matches?(6)
obj.failure_message.should be("Expected 6 to satisfy given block.")
end
def test_eql_negative_fail_message_for_matches
obj = satisfy(lambda {|x| x == 4})
obj.matches?(4)
obj.negative_failure_message.should be("Expected 4 to not satisfy given block.")
end
def test_kind_of
3.should be_kind_of(Fixnum)
end
def test_kind_of_fail
lambda {
3.should be_kind_of(Hash)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_negative_kind_of
3.should_not be_kind_of(Hash)
end
def test_negative_kind_of_fail
lambda {
3.should_not be_kind_of(Fixnum)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_respond_to
"foo".should respond_to(:length)
end
def test_respond_to_fail
lambda {
"foo".should respond_to(:nonexistant_method)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_negative_respond_to
"foo".should_not respond_to(:nonexistant_method)
end
def test_negative_respond_to_fail
lambda {
"foo".should_not respond_to(:length)
}.should raise_error(Test::Unit::AssertionFailedError)
end
# be_something
def test_positive_be_something_method_missing_pass
def @obj.something?
true
end
@obj.should be_something
end
def test_positive_be_something_method_missing_fails
def @obj.something?
false
end
lambda {@obj.should be_something}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_negative_be_something_method_missing_pass
def @obj.something?
false
end
@obj.should_not be_something
end
def test_negative_be_something_method_missing_fails
def @obj.something?
true
end
lambda {@obj.should_not be_something}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_be_something_method_missing_fail_message
obj = "foo"
def obj.something?
true
end
matcher_obj = be_something
obj.should matcher_obj
matcher_obj.failure_message.should be("Expected \"foo\" to return true for something?, with 'no args'.")
end
def test_be_something_method_missing_negative_fail_message
obj = "foo"
def obj.something?
false
end
matcher_obj = be_something
obj.should_not matcher_obj
matcher_obj.negative_failure_message.should =~ /Expected \"foo\" to not return true for something?/
end
# be_something(arg)
def test_positive_be_something_with_arg_method_missing_pass
def @obj.something?(arg)
true
end
@obj.should be_something(1)
end
def test_positive_be_something_with_arg_method_missing_fails
def @obj.something?(arg)
false
end
lambda {@obj.should be_something(1)}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_negative_be_something_method_missing_pass
def @obj.something?(arg)
false
end
@obj.should_not be_something(1)
end
def test_negative_be_something_method_missing_fails
def @obj.something?(arg)
true
end
lambda {@obj.should_not be_something(1)}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_be_something_method_missing_fail_message
obj = "foo"
def obj.something?(arg)
true
end
matcher_obj = be_something(1)
obj.should matcher_obj
matcher_obj.failure_message.should be("Expected \"foo\" to return true for something?, with '1'.")
end
def test_be_something_method_missing_negative_fail_message
obj = "foo"
def obj.something?(arg)
false
end
matcher_obj = be_something(1)
obj.should_not matcher_obj
matcher_obj.negative_failure_message.should be("Expected \"foo\" to not return true for something?, with '1'.")
end
end
|