Class: TinyTest::TestCase

Inherits:
Object
  • Object
show all
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

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, message = nil)
  message ||= "Failed assertion, no message given."
  suite.succ_assertion_count
  unless cond
    message = message.call if message.respond_to?(:call)
    raise AssertError, message
  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(message = nil, &block)
  assertion_frame(yield(), message) 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, message = nil)
  suite.assertion_count_grouping do
    assert_respond_to(object, :empty?, message)
    assertion_frame(object.empty?, message) 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, message = nil)
  assertion_frame(expected == actual, message) 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, message = nil)
  expected, actual, delta, message = Util.normalize_in_delta_epsilon_arguments(
    expected, actual, delta, message
  )
  gap = (expected - actual).abs
  assertion_frame(delta >= gap, message) 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, message = nil)
  a, b, epsilon, message = Util.normalize_in_delta_epsilon_arguments(
    a, b, epsilon, message
  )
  assert_in_delta(a, b, [a, b].min * epsilon, message)
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, message = nil)
  suite.assertion_count_grouping do
    assert_respond_to(collection, :include?, message)
    assertion_frame(collection.include?(object), message) 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, message = nil)
  assertion_frame(object.instance_of?(klass), message) 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, message = nil)
  assertion_frame(object.kind_of?(klass), message) 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, message = nil)
  cond = Util.assertion_match_test(expected, actual)
  assertion_frame(cond, message) 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, message = nil)
  assertion_frame(object.nil?, message) 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, message = nil)
  assertion_frame(operand1.__send__(operator, operand2), message) 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, message = 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, message, RAISES_MSG_HOOK) do
      "#{exceptions.inspect} expected, but\n#{Util.describe_exception(actual)}"
    end
  else
    assertion_frame(false, message, 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, message = nil)
  assertion_frame(object.respond_to?(method), message) 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, message = nil)
  assertion_frame(expected.equal?(actual), message) 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, message = nil)
  assertion_frame(Util.assertion_send_dispatch(*send_concerneds), message) 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, message = nil, &block)
  msg = "Expected #{tag.inspect} to have been thrown"
  thrown = true
  catch(tag) do
    yield(tag)
    thrown = false
  end
  assertion_frame(thrown, message){ 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(message = nil)
  assert(false, message || "Epic Fail!")
end

#pass(message = nil) ⇒ Object

Certainty pass examination.



349
350
351
# File 'lib/tinytest.rb', line 349

def pass(message = 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, message = nil)
  not assert(!cond, message || "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(message = nil, &block)
  refutation_frame(yield(), message) 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, message = nil)
  suite.assertion_count_grouping do
    assert_respond_to(object, :empty?, message)
    refutation_frame(object.empty?, message) 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, message = nil)
  refutation_frame(expected == actual, message) 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, message = nil)
  expected, actual, delta, message = Util.normalize_in_delta_epsilon_arguments(
    expected, actual, delta, message
  )
  gap = (expected - actual).abs
  refutation_frame(delta >= gap, message) 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, message = nil)
  a, b, epsilon, message = Util.normalize_in_delta_epsilon_arguments(
    a, b, epsilon, message
  )
  refute_in_delta(a, b, [a, b].min * epsilon, message)
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, message = nil)
  suite.assertion_count_grouping do
    assert_respond_to(collection, :include?, message)
    refutation_frame(collection.include?(object), message) 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, message = nil)
  refutation_frame(object.instance_of?(klass), message) 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, message = nil)
  refutation_frame(object.kind_of?(klass), message) 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, message = nil)
  cond = Util.assertion_match_test(expected, actual)
  refutation_frame(cond, message) 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, message = nil)
  refutation_frame(object.nil?, message) 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, message = nil)
  refutation_frame(operand1.__send__(operator, operand2), message) 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, message = 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, message, 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, message = nil)
  refutation_frame(object.respond_to?(method), message) 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, message = nil)
  refutation_frame(expected.equal?(actual), message) 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, message = nil)
  refutation_frame(Util.assertion_send_dispatch(*send_concerneds), message) 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, message = nil, &block)
  thrown = true
  catch(tag) do
    yield(tag)
    thrown = false
  end
  refutation_frame(thrown, message) do
    "Expected #{tag.inspect} to not have been thrown"
  end
end

#setupObject

Write procedures before executing tests.



314
315
316
# File 'lib/tinytest.rb', line 314

def setup
  ;
end

#skip(message = nil) ⇒ Object

Raise skipping.

Raises:



354
355
356
# File 'lib/tinytest.rb', line 354

def skip(message = nil)
  raise(SkipError, message || "Skipped, no message given")
end

#teardownObject

Write procedures after executing tests.



319
320
321
# File 'lib/tinytest.rb', line 319

def teardown
  ;
end