Class: TinyTest::TestCase
- Inherits:
-
Object
- Object
- TinyTest::TestCase
- Defined in:
- lib/tinytest.rb,
lib/tinytest/compat.rb,
lib/tinytest/compat.rb
Overview
TinyTest’s testcase definition is witten as class definition which inherit this class.
Defined Under Namespace
Modules: Util
Constant Summary collapse
- RAISES_MSG_HOOK =
lambda{|msg| msg.sub(/\.\Z/, '') }
Instance Method Summary collapse
-
#assert(cond, message = nil) ⇒ Object
Pass examination if cond was evaluated as true.
-
#assert_block(message = nil, &block) ⇒ Object
Pass examination if block returns true.
-
#assert_empty(object, message = nil) ⇒ Object
Pass examination if object is empty.
-
#assert_equal(expected, actual, message = nil) ⇒ Object
Pass examination if expected equals to actual.
- #assert_in_delta(expected, actual, delta = 0.001, message = nil) ⇒ Object
- #assert_in_epsilon(a, b, epsilon = 0.001, message = nil) ⇒ Object
-
#assert_includes(collection, object, message = nil) ⇒ Object
Pass examination if collection includes object.
-
#assert_instance_of(klass, object, message = nil) ⇒ Object
Pass examination if object is an instance of klass.
-
#assert_kind_of(klass, object, message = nil) ⇒ Object
Pass examination if object is an instance of a class which is klass or a subclass of klass.
-
#assert_match(expected, actual, message = nil) ⇒ Object
Pass examination if expected matches actual.
-
#assert_nil(object, message = nil) ⇒ Object
Pass examination if object is nil.
- #assert_operator(operand1, operator, operand2, message = nil) ⇒ Object
- #assert_raises(exceptions, message = nil, &block) ⇒ Object
-
#assert_respond_to(object, method, message = nil) ⇒ Object
Pass examination if object is respondable to method.
-
#assert_same(expected, actual, message = nil) ⇒ Object
Pass examination if expected is actual based on object identity.
- #assert_send(send_concerneds, message = nil) ⇒ Object
- #assert_throws(tag, message = nil, &block) ⇒ Object
- #bind_testsuite(suite) ⇒ Object
-
#flunk(message = nil) ⇒ Object
Certainty fail to examination.
-
#pass(message = nil) ⇒ Object
Certainty pass examination.
-
#refute(cond, message = nil) ⇒ Object
Pass examination if cond was evaluated as false.
-
#refute_block(message = nil, &block) ⇒ Object
Pass examination if block returns false.
-
#refute_empty(object, message = nil) ⇒ Object
Pass examination unless object is empty.
-
#refute_equal(expected, actual, message = nil) ⇒ Object
Pass examination unless expected equals to actual.
- #refute_in_delta(expected, actual, delta = 0.001, message = nil) ⇒ Object
- #refute_in_epsilon(a, b, epsilon = 0.001, message = nil) ⇒ Object
-
#refute_includes(collection, object, message = nil) ⇒ Object
Pass examination unless collection includes object.
-
#refute_instance_of(klass, object, message = nil) ⇒ Object
Pass examination unless object is an instance of klass.
-
#refute_kind_of(klass, object, message = nil) ⇒ Object
Pass examination unless object is an instance of a class which is klass or a subclass of klass.
-
#refute_match(expected, actual, message = nil) ⇒ Object
Pass examination unless expected matches actual.
-
#refute_nil(object, message = nil) ⇒ Object
Pass examination unless object is nil.
- #refute_operator(operand1, operator, operand2, message = nil) ⇒ Object
- #refute_raises(exceptions, message = nil, &block) ⇒ Object
-
#refute_respond_to(object, method, message = nil) ⇒ Object
Pass examination unless object is respondable to method.
-
#refute_same(expected, actual, message = nil) ⇒ Object
Pass examination unless expected is actual based on object identity.
- #refute_send(send_concerneds, message = nil) ⇒ Object
- #refute_throws(tag, message = nil, &block) ⇒ Object
-
#setup ⇒ Object
Write procedures before executing tests.
-
#skip(message = nil) ⇒ Object
Raise skipping.
-
#teardown ⇒ Object
Write procedures after executing tests.
Instance Method Details
#assert(cond, message = nil) ⇒ Object
Pass examination if cond was evaluated as true.
328 329 330 331 332 333 334 335 336 |
# File 'lib/tinytest.rb', line 328 def assert(cond, = nil) ||= "Failed assertion, no message given." suite.succ_assertion_count unless cond = .call if .respond_to?(:call) raise AssertError, end true end |
#assert_block(message = nil, &block) ⇒ Object
Pass examination if block returns true.
359 360 361 362 363 |
# File 'lib/tinytest.rb', line 359 def assert_block( = nil, &block) assertion_frame(yield(), ) do "Expected block to return a value evaluated as true" end end |
#assert_empty(object, message = nil) ⇒ Object
Pass examination if object is empty. (by #empty?)
479 480 481 482 483 484 485 486 |
# File 'lib/tinytest.rb', line 479 def assert_empty(object, = nil) suite.assertion_count_grouping do assert_respond_to(object, :empty?, ) assertion_frame(object.empty?, ) do "Expected #{object.inspect} to be empty" end end end |
#assert_equal(expected, actual, message = nil) ⇒ Object
Pass examination if expected equals to actual. (by #==)
373 374 375 376 377 |
# File 'lib/tinytest.rb', line 373 def assert_equal(expected, actual, = nil) assertion_frame(expected == actual, ) do "Expected #{expected.inspect}, not #{actual.inspect}" end end |
#assert_in_delta(expected, actual, delta = 0.001, message = nil) ⇒ Object
518 519 520 521 522 523 524 525 526 |
# File 'lib/tinytest.rb', line 518 def assert_in_delta(expected, actual, delta = 0.001, = nil) expected, actual, delta, = Util.normalize_in_delta_epsilon_arguments( expected, actual, delta, ) gap = (expected - actual).abs assertion_frame(delta >= gap, ) do "Expected #{expected} - #{actual} (#{gap}) to be < #{delta}" end end |
#assert_in_epsilon(a, b, epsilon = 0.001, message = nil) ⇒ Object
538 539 540 541 542 543 |
# File 'lib/tinytest.rb', line 538 def assert_in_epsilon(a, b, epsilon = 0.001, = nil) a, b, epsilon, = Util.normalize_in_delta_epsilon_arguments( a, b, epsilon, ) assert_in_delta(a, b, [a, b].min * epsilon, ) end |
#assert_includes(collection, object, message = nil) ⇒ Object
Pass examination if collection includes object. (by #include?)
499 500 501 502 503 504 505 506 |
# File 'lib/tinytest.rb', line 499 def assert_includes(collection, object, = nil) suite.assertion_count_grouping do assert_respond_to(collection, :include?, ) assertion_frame(collection.include?(object), ) do "Expected #{collection.inspect} to include #{object.inspect}" end end end |
#assert_instance_of(klass, object, message = nil) ⇒ Object
Pass examination if object is an instance of klass. (by #instance_of?)
387 388 389 390 391 |
# File 'lib/tinytest.rb', line 387 def assert_instance_of(klass, object, = nil) assertion_frame(object.instance_of?(klass), ) do "Expected #{object.inspect} to be an instance of #{klass}" end end |
#assert_kind_of(klass, object, message = nil) ⇒ Object
Pass examination if object is an instance of a class which is klass or a subclass of klass. (by #kind_of?)
402 403 404 405 406 |
# File 'lib/tinytest.rb', line 402 def assert_kind_of(klass, object, = nil) assertion_frame(object.kind_of?(klass), ) do "Expected #{object.inspect} to be a kind of #{klass}" end end |
#assert_match(expected, actual, message = nil) ⇒ Object
Pass examination if expected matches actual. (by =~)
417 418 419 420 421 422 |
# File 'lib/tinytest.rb', line 417 def assert_match(expected, actual, = nil) cond = Util.assertion_match_test(expected, actual) assertion_frame(cond, ) do "Expected #{expected.inspect} to match #{actual.inspect}" end end |
#assert_nil(object, message = nil) ⇒ Object
Pass examination if object is nil. (by #nil?)
433 434 435 436 437 |
# File 'lib/tinytest.rb', line 433 def assert_nil(object, = nil) assertion_frame(object.nil?, ) do "Expected #{object.inspect} to be nil" end end |
#assert_operator(operand1, operator, operand2, message = nil) ⇒ Object
552 553 554 555 556 |
# File 'lib/tinytest.rb', line 552 def assert_operator(operand1, operator, operand2, = nil) assertion_frame(operand1.__send__(operator, operand2), ) do "Expected #{operand1.inspect} to be #{operator} #{operand2.inspect}" end end |
#assert_raises(exceptions, message = nil, &block) ⇒ Object
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'lib/tinytest.rb', line 580 def assert_raises(exceptions, = nil, &block) exceptions = Util.normalize_raises_arguments(exceptions, &block) begin yield() rescue Exception => actual cond = exceptions.any?{|e| actual.kind_of?(e) } assertion_frame(cond, , RAISES_MSG_HOOK) do "#{exceptions.inspect} expected, but\n#{Util.describe_exception(actual)}" end else assertion_frame(false, , RAISES_MSG_HOOK) do "#{exceptions.inspect} expected but nothing was raised" end end end |
#assert_respond_to(object, method, message = nil) ⇒ Object
Pass examination if object is respondable to method. (by #respond_to?)
465 466 467 468 469 |
# File 'lib/tinytest.rb', line 465 def assert_respond_to(object, method, = nil) assertion_frame(object.respond_to?(method), ) do "Expected #{object.inspect} (#{object.class}) to respond to #{method}" end end |
#assert_same(expected, actual, message = nil) ⇒ Object
Pass examination if expected is actual based on object identity. (by #equal?)
447 448 449 450 451 452 453 454 455 |
# File 'lib/tinytest.rb', line 447 def assert_same(expected, actual, = nil) assertion_frame(expected.equal?(actual), ) do sprintf( "Expected %s (0x%x) to be the same as %s (0x%x)", expected.inspect, expected.object_id, actual.inspect, actual.object_id ) end end |
#assert_send(send_concerneds, message = nil) ⇒ Object
564 565 566 567 568 569 |
# File 'lib/tinytest.rb', line 564 def assert_send(send_concerneds, = nil) assertion_frame(Util.assertion_send_dispatch(*send_concerneds), ) do inspection = Util.assertion_send_inspection(*send_concerneds) "Expected #{inspection} to be evaluated as true" end end |
#assert_throws(tag, message = nil, &block) ⇒ Object
608 609 610 611 612 613 614 615 616 |
# File 'lib/tinytest.rb', line 608 def assert_throws(tag, = nil, &block) msg = "Expected #{tag.inspect} to have been thrown" thrown = true catch(tag) do yield(tag) thrown = false end assertion_frame(thrown, ){ msg } end |
#bind_testsuite(suite) ⇒ Object
323 324 325 |
# File 'lib/tinytest.rb', line 323 def bind_testsuite(suite) define_singleton_method(:suite){ suite } end |
#flunk(message = nil) ⇒ Object
Certainty fail to examination.
344 345 346 |
# File 'lib/tinytest.rb', line 344 def flunk( = nil) assert(false, || "Epic Fail!") end |
#pass(message = nil) ⇒ Object
Certainty pass examination.
349 350 351 |
# File 'lib/tinytest.rb', line 349 def pass( = nil) assert(true) end |
#refute(cond, message = nil) ⇒ Object
Pass examination if cond was evaluated as false.
339 340 341 |
# File 'lib/tinytest.rb', line 339 def refute(cond, = nil) not assert(!cond, || "Failed refutation, no message given") end |
#refute_block(message = nil, &block) ⇒ Object
Pass examination if block returns false.
366 367 368 369 370 |
# File 'lib/tinytest.rb', line 366 def refute_block( = nil, &block) refutation_frame(yield(), ) do "Expected block to return a value evaluated as false" end end |
#refute_empty(object, message = nil) ⇒ Object
Pass examination unless object is empty. (by #empty?)
489 490 491 492 493 494 495 496 |
# File 'lib/tinytest.rb', line 489 def refute_empty(object, = nil) suite.assertion_count_grouping do assert_respond_to(object, :empty?, ) refutation_frame(object.empty?, ) do "Expected #{object.inspect} to not be empty" end end end |
#refute_equal(expected, actual, message = nil) ⇒ Object
Pass examination unless expected equals to actual. (by #==)
380 381 382 383 384 |
# File 'lib/tinytest.rb', line 380 def refute_equal(expected, actual, = nil) refutation_frame(expected == actual, ) do "Expected #{actual.inspect} to not be equal to #{expected.inspect}" end end |
#refute_in_delta(expected, actual, delta = 0.001, message = nil) ⇒ Object
528 529 530 531 532 533 534 535 536 |
# File 'lib/tinytest.rb', line 528 def refute_in_delta(expected, actual, delta = 0.001, = nil) expected, actual, delta, = Util.normalize_in_delta_epsilon_arguments( expected, actual, delta, ) gap = (expected - actual).abs refutation_frame(delta >= gap, ) do "Expected #{expected} - #{actual} (#{gap}) to not be < #{delta}" end end |
#refute_in_epsilon(a, b, epsilon = 0.001, message = nil) ⇒ Object
545 546 547 548 549 550 |
# File 'lib/tinytest.rb', line 545 def refute_in_epsilon(a, b, epsilon = 0.001, = nil) a, b, epsilon, = Util.normalize_in_delta_epsilon_arguments( a, b, epsilon, ) refute_in_delta(a, b, [a, b].min * epsilon, ) end |
#refute_includes(collection, object, message = nil) ⇒ Object
Pass examination unless collection includes object. (by #include?)
509 510 511 512 513 514 515 516 |
# File 'lib/tinytest.rb', line 509 def refute_includes(collection, object, = nil) suite.assertion_count_grouping do assert_respond_to(collection, :include?, ) refutation_frame(collection.include?(object), ) do "Expected #{collection.inspect} to not include #{object.inspect}" end end end |
#refute_instance_of(klass, object, message = nil) ⇒ Object
Pass examination unless object is an instance of klass. (by #instance_of?)
394 395 396 397 398 |
# File 'lib/tinytest.rb', line 394 def refute_instance_of(klass, object, = nil) refutation_frame(object.instance_of?(klass), ) do "Expected #{object.inspect} to not be an instance of #{klass}" end end |
#refute_kind_of(klass, object, message = nil) ⇒ Object
Pass examination unless object is an instance of a class which is klass or a subclass of klass. (by #kind_of?)
410 411 412 413 414 |
# File 'lib/tinytest.rb', line 410 def refute_kind_of(klass, object, = nil) refutation_frame(object.kind_of?(klass), ) do "Expected #{object.inspect} to not be a kind of #{klass}" end end |
#refute_match(expected, actual, message = nil) ⇒ Object
Pass examination unless expected matches actual. (by =~)
425 426 427 428 429 430 |
# File 'lib/tinytest.rb', line 425 def refute_match(expected, actual, = nil) cond = Util.assertion_match_test(expected, actual) refutation_frame(cond, ) do "Expected #{expected.inspect} to not match #{actual.inspect}" end end |
#refute_nil(object, message = nil) ⇒ Object
Pass examination unless object is nil. (by #nil?)
440 441 442 443 444 |
# File 'lib/tinytest.rb', line 440 def refute_nil(object, = nil) refutation_frame(object.nil?, ) do "Expected #{object.inspect} to not be nil" end end |
#refute_operator(operand1, operator, operand2, message = nil) ⇒ Object
558 559 560 561 562 |
# File 'lib/tinytest.rb', line 558 def refute_operator(operand1, operator, operand2, = nil) refutation_frame(operand1.__send__(operator, operand2), ) do "Expected #{operand1.inspect} to not be #{operator} #{operand2.inspect}" end end |
#refute_raises(exceptions, message = nil, &block) ⇒ Object
596 597 598 599 600 601 602 603 604 605 606 |
# File 'lib/tinytest.rb', line 596 def refute_raises(exceptions, = nil, &block) exceptions = Util.normalize_raises_arguments(exceptions, &block) begin yield() rescue Exception => actual cond = exceptions.any?{|e| actual.kind_of?(e) } refutation_frame(cond, , RAISES_MSG_HOOK) do "#{exceptions.inspect} not expected, but\n#{Util.describe_exception(actual)}" end end end |
#refute_respond_to(object, method, message = nil) ⇒ Object
Pass examination unless object is respondable to method. (by #respond_to?)
472 473 474 475 476 |
# File 'lib/tinytest.rb', line 472 def refute_respond_to(object, method, = nil) refutation_frame(object.respond_to?(method), ) do "Expected #{object.inspect} to not respond to #{method}" end end |
#refute_same(expected, actual, message = nil) ⇒ Object
Pass examination unless expected is actual based on object identity. (by #equal?)
458 459 460 461 462 |
# File 'lib/tinytest.rb', line 458 def refute_same(expected, actual, = nil) refutation_frame(expected.equal?(actual), ) do "Expected #{expected.inspect} to not be the same as #{actual.inspect}" end end |
#refute_send(send_concerneds, message = nil) ⇒ Object
571 572 573 574 575 576 |
# File 'lib/tinytest.rb', line 571 def refute_send(send_concerneds, = nil) refutation_frame(Util.assertion_send_dispatch(*send_concerneds), ) do inspection = Util.assertion_send_inspection(*send_concerneds) "Expected #{inspection} to be evaluated as false" end end |
#refute_throws(tag, message = nil, &block) ⇒ Object
618 619 620 621 622 623 624 625 626 627 |
# File 'lib/tinytest.rb', line 618 def refute_throws(tag, = nil, &block) thrown = true catch(tag) do yield(tag) thrown = false end refutation_frame(thrown, ) do "Expected #{tag.inspect} to not have been thrown" end end |
#setup ⇒ Object
Write procedures before executing tests.
314 315 316 |
# File 'lib/tinytest.rb', line 314 def setup ; end |
#skip(message = nil) ⇒ Object
Raise skipping.
354 355 356 |
# File 'lib/tinytest.rb', line 354 def skip( = nil) raise(SkipError, || "Skipped, no message given") end |
#teardown ⇒ Object
Write procedures after executing tests.
319 320 321 |
# File 'lib/tinytest.rb', line 319 def teardown ; end |