File: test_nanotest.rb

package info (click to toggle)
ruby-nanotest 0.9.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 108 kB
  • ctags: 15
  • sloc: ruby: 123; makefile: 3
file content (90 lines) | stat: -rw-r--r-- 2,225 bytes parent folder | download | duplicates (2)
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
require 'minitest/autorun'
require 'test/test_helper'

module Nanotest
  class << self
    def failures() @@failures end
    def dots() @@dots end
  end

  # don't autorun
  def at_exit() end
end
require 'nanotest'

# fixture class for nanotest mixin
class Foo
  include Nanotest
end

class TestNanotest < MiniTest::Unit::TestCase
  def self.test(name, &block)
    define_method("test_#{name.gsub(/\s/,'_')}", &block)
  end

  def teardown
    Nanotest::dots.clear
    Nanotest::failures.clear
  end

  test "api" do
    assert_respond_to Foo.new, :assert
    assert_respond_to Nanotest, :assert
  end

  test "assertion passes" do
    Nanotest.assert { true }
    assert_equal '.', Nanotest::dots.last
    assert_empty Nanotest::failures
  end

  test "assertion fails (false)" do
    Nanotest.assert { false }
    assert_equal 'F', Nanotest::dots.last
    refute_empty Nanotest::failures
  end

  test "assertion fails (nil)" do
    Nanotest.assert { nil }
    assert_equal 'F', Nanotest::dots.last
    refute_empty Nanotest::failures
  end

  test "failure message" do
    @line = __LINE__; Nanotest.assert { false }
    assert_equal 1, Nanotest::failures.size
    assert_includes Nanotest::failures, "(%s:%0.3d) assertion failed" % [__FILE__, @line]
  end

  test "custom failure message, file, line" do
    Nanotest.assert('foo','bar',2) { false }
    assert_includes Nanotest::failures, "(bar:002) foo"
  end

  test "displays results" do
    Nanotest.assert { true }
    Nanotest.assert { false }; line1 = __LINE__
    Nanotest.assert { false }; line2 = __LINE__
    expected = <<-OUT.gsub(/^\s*/,'').strip % [__FILE__, line1, __FILE__, line2]
      .FF
      (%s:%0.3d) assertion failed
      (%s:%0.3d) assertion failed
    OUT
    assert_equal expected, Nanotest.results
  end

  test "displays results with no assertions" do
    assert_empty Nanotest.results.strip
  end

  test "handles origin lines that contain colons" do
    stack = ['o:hai:e:20:blah']
    Nanotest.assert('', nil, nil, stack) { false }
    assert_equal "(o:hai:e:020) ", Nanotest.failures.last

    stack = ['o:hai:e:20']
    Nanotest.assert('', nil, nil, stack) { false }
    assert_equal "(o:hai:e:020) ", Nanotest.failures.last
  end
end