GUnit 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,98 @@
1
+ module GUnit
2
+
3
+ # How many tests have run
4
+ # How many passing test responses
5
+ # How many failing test responses
6
+ # How many exception test responses
7
+
8
+ # A TestRunner object discovers TestCase classes
9
+ # The TestRunner object calls suite() on all TestCase classes
10
+ # Each TestCase class returns a TestSuite object with instances of itself (TestCases) each with a method to be executed
11
+ # The TestRunner object calls run() on all TestSuite objects, collecting TestResponses
12
+ # Each TestSuite object calls run() on all of its TestCase objects, yielding TestResponses
13
+ # Each TestCase object executes its method, returning a TestResponse
14
+ # The TestRunner displays the TestResponses as they are yielded
15
+ # After all tests have run, the TestRunner displays a summery of results
16
+
17
+ class TestRunner
18
+
19
+ PASS_CHAR = '.'
20
+ FAIL_CHAR = 'F'
21
+ TODO_CHAR = '*'
22
+ EXCEPTION_CHAR = 'E'
23
+
24
+ # TestSuites and/or TestCases
25
+ attr_accessor :io, :silent
26
+ attr_writer :tests
27
+ attr_reader :responses
28
+
29
+ def initialize(*args)
30
+ @responses = []
31
+ @io = STDOUT
32
+ STDOUT.sync = true
33
+ @silent = false
34
+ end
35
+
36
+ def tests
37
+ @tests ||= []
38
+ end
39
+
40
+ def run
41
+ self.tests.each do |test|
42
+ case
43
+ when test.is_a?(TestSuite)
44
+ test.run do |response|
45
+ @responses << response
46
+ @io.print self.class.response_character(response) unless self.silent
47
+ end
48
+ when test.is_a?(TestCase)
49
+ response = test.run
50
+ @responses << response
51
+ @io.print self.class.response_character(response) unless self.silent
52
+ end
53
+ end
54
+
55
+ unless self.silent
56
+ @io.puts ""
57
+ fails.each do |fail|
58
+ @io.puts ""
59
+ @io.puts fail.message
60
+ end
61
+ @io.puts "#{@responses.length} verifications: #{passes.length} passed, #{fails.length} failed, #{exceptions.length} exceptions, #{to_dos.length} to-dos"
62
+ end
63
+ end
64
+
65
+ def passes
66
+ @responses.find_all{|r| r.is_a? PassResponse }
67
+ end
68
+
69
+ def fails
70
+ @responses.find_all{|r| r.is_a? FailResponse }
71
+ end
72
+
73
+ def exceptions
74
+ @responses.find_all{|r| r.is_a? ExceptionResponse }
75
+ end
76
+
77
+ def to_dos
78
+ @responses.find_all{|r| r.is_a? ToDoResponse }
79
+ end
80
+
81
+ protected
82
+
83
+ def self.response_character(response)
84
+ case
85
+ when response.is_a?(PassResponse)
86
+ PASS_CHAR
87
+ when response.is_a?(FailResponse)
88
+ FAIL_CHAR
89
+ when response.is_a?(ExceptionResponse)
90
+ EXCEPTION_CHAR
91
+ when response.is_a?(ToDoResponse)
92
+ TODO_CHAR
93
+ end
94
+ end
95
+
96
+ end
97
+
98
+ end
@@ -0,0 +1,29 @@
1
+ module GUnit
2
+
3
+ class TestSuite
4
+
5
+ # TestCases (TODO and/or TestSuites)
6
+ attr_writer :tests
7
+
8
+ def initialize()
9
+ end
10
+
11
+ def tests
12
+ @tests ||= []
13
+ end
14
+
15
+ def run(&blk)
16
+ self.tests.each do |test|
17
+ case
18
+ when test.is_a?(TestSuite)
19
+ test.run{|response| blk.call(response) if blk }
20
+ when test.is_a?(TestCase)
21
+ response = test.run
22
+ blk.call(response) if blk
23
+ end
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,6 @@
1
+ module GUnit
2
+
3
+ class ToDoResponse < TestResponse
4
+ end
5
+
6
+ end
@@ -0,0 +1,43 @@
1
+ module GUnit
2
+
3
+ class Verification
4
+ attr_writer :message
5
+ attr_accessor :task
6
+
7
+ # Verification.new("my message")
8
+ # Verification.new("my message") { assert true }
9
+ # Verification.new() { assert true }
10
+ def initialize(*args, &blk)
11
+ self.message = args[0]
12
+ self.task = blk if blk
13
+ end
14
+
15
+ def run(binding=self)
16
+ begin
17
+ if @task.is_a?(Proc)
18
+ bound_task = @task.bind(binding)
19
+ bound_task.call
20
+ PassResponse.new
21
+ else
22
+ ToDoResponse.new
23
+ end
24
+ rescue GUnit::AssertionFailure => e
25
+ FailResponse.new(e.message)
26
+ rescue ::StandardError => e
27
+ ExceptionResponse.new
28
+ end
29
+ end
30
+
31
+ def message
32
+ @message || default_message
33
+ end
34
+
35
+ def default_message
36
+ "Verification failed!"
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+
data/lib/gunit.rb ADDED
@@ -0,0 +1,26 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'gunit/proc_extensions'
4
+
5
+ require 'gunit/assertions'
6
+
7
+ require 'gunit/context'
8
+
9
+ require 'gunit/exercise'
10
+ require 'gunit/setup'
11
+ require 'gunit/teardown'
12
+ require 'gunit/verification'
13
+
14
+ require 'gunit/test_response'
15
+ require 'gunit/exception_response'
16
+ require 'gunit/fail_response'
17
+ require 'gunit/pass_response'
18
+ require 'gunit/to_do_response'
19
+
20
+ require 'gunit/test_case'
21
+ require 'gunit/test_suite'
22
+ require 'gunit/test_runner'
23
+
24
+
25
+
26
+
@@ -0,0 +1,182 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class Foo
4
+ end
5
+
6
+ class FooGUnitTest < GUnit::TestCase
7
+
8
+ setup do
9
+ @foo = 'bar'
10
+ end
11
+
12
+ verify "variable from setup" do
13
+ assert @foo == 'bar'
14
+ assert "foo is string" do
15
+ @foo.is_a?(String)
16
+ end
17
+ assert @foo.is_a?(String), "foo is string"
18
+ assert_equal 'bar', @foo, "foo is bar"
19
+ end
20
+
21
+ verify do
22
+ assert 1 == 1
23
+ end
24
+
25
+ verify "truth" do
26
+ assert true
27
+
28
+ assert "one is one" do
29
+ 1 == 1
30
+ end
31
+
32
+ assert_equal 1, 1
33
+ assert_equal 1, 1, "one is one"
34
+ assert_equal 1 do
35
+ 0 + 1
36
+ end
37
+ assert_equal 2, "one and one make two" do
38
+ 1 + 1
39
+ end
40
+ assert_equal 2 do
41
+ 1 + 1
42
+ end
43
+ end
44
+
45
+ verify "failure here" do
46
+ assert false
47
+ end
48
+
49
+ verify "another failure here" do
50
+ assert @foo == 'wrong'
51
+ end
52
+
53
+ verify "exceptional" do
54
+ assert_raises do
55
+ raise "BOOM!"
56
+ end
57
+ end
58
+
59
+ verify "not exceptional" do
60
+ assert_raises do
61
+ true
62
+ end
63
+ end
64
+
65
+ verify "unexpected" do
66
+ raise "BAM!!!"
67
+ end
68
+
69
+ verify "not dun yet"
70
+
71
+ context "An instance of Foo" do
72
+ # One setup per context
73
+ setup do
74
+ @foo = 'abc'
75
+ end
76
+
77
+ # # One exercise per context
78
+ # exercise do
79
+ # @foo.do_something
80
+ # end
81
+
82
+ # One teardown per context
83
+ teardown do
84
+ @foo = nil
85
+ end
86
+
87
+ # Many verifies per context
88
+ verify "abc's of foo" do
89
+ assert @foo == 'abc'
90
+ end
91
+
92
+ # many nested contexts per context
93
+ context "doing something else" do
94
+ setup do
95
+ @foo = 'xyz'
96
+ end
97
+ teardown do
98
+ @foo = nil
99
+ end
100
+ verify "xyz's of foo" do
101
+ assert @foo == 'xyz'
102
+ end
103
+ verify "xyz's of foo" do
104
+ assert @foo == 'wrong'
105
+ end
106
+ end
107
+
108
+ verify "abc's of foo" do
109
+ assert @foo == 'def'
110
+ end
111
+ end
112
+
113
+ # # Custom macros
114
+ # verify_validity @foo
115
+ # verify_dirty @foo
116
+ #
117
+ # # Many verifies per context, some share fixtures
118
+ # # setup, exercise and teardown will only be run once for all methods in this context with the second param == true
119
+ # verify "one and one is two", true do
120
+ # assert 1+1 == 2
121
+ # end
122
+ #
123
+ # # Many verifies per context, some share fixtures
124
+ # # setup, exercise and teardown will only be run once for all methods in this context with the second param == true
125
+ # verify "one is more than none", true do
126
+ # assert 1 > 0
127
+ # end
128
+ #
129
+ # # Many verifies per context
130
+ # verify "the truth" do
131
+ # assert { true }
132
+ # assert "true is true" { true }
133
+ # assert true
134
+ # assert_equal true, true
135
+ # assert_equal true, true, "true is true"
136
+ # assert_equal true, true, "true is true" {|a,b| a === b }
137
+ #
138
+ # assert_is_a @foo, Foo
139
+ #
140
+ # assert_nil nil
141
+ #
142
+ # assert_nil nil, "nil is nil"
143
+ #
144
+ # assert_raise do
145
+ # raise RuntimeError
146
+ # end
147
+ #
148
+ # assert_raise RuntimeError do
149
+ # raise RuntimeError
150
+ # end
151
+ #
152
+ # assert_raise RuntimeError, "blow up" do
153
+ # raise RuntimeError
154
+ # end
155
+ # end
156
+ #
157
+ # verify "something changed" do
158
+ # assert_change @foo.something, :by => -1
159
+ # assert_change "@foo.something_else", :from => true, :to => false
160
+ # assert_change "@foo.third_thing" {|before, after| before < after }
161
+ # end
162
+
163
+ end
164
+
165
+ class FooGUnitTestTest < Test::Unit::TestCase
166
+ def setup
167
+ @test_runner = GUnit::TestRunner.new
168
+ @test_runner.io = mock('io')
169
+ @test_runner.io.stubs(:print)
170
+ @test_runner.io.stubs(:puts)
171
+ @test_runner.tests << FooGUnitTest.suite
172
+ end
173
+
174
+ def test_run_test_runner
175
+ @test_runner.run
176
+ assert_equal 6, @test_runner.passes.length
177
+ assert_equal 5, @test_runner.fails.length
178
+ assert_equal 1, @test_runner.exceptions.length
179
+ assert_equal 1, @test_runner.to_dos.length
180
+ end
181
+
182
+ end
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'erb'
6
+
7
+ require 'ruby-debug'
8
+
9
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
11
+ require 'gunit'
12
+
13
+ class Test::Unit::TestCase
14
+ end
@@ -0,0 +1,190 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class Foo
4
+ include GUnit::Assertions
5
+ end
6
+
7
+
8
+ class FooTest < Test::Unit::TestCase
9
+ def setup
10
+ @foo1 = Foo.new
11
+ end
12
+
13
+ def test_assert_no_arg_one_block
14
+ block = Proc.new { 1==1 }
15
+ result = @foo1.assert &block
16
+ assert result === true
17
+ end
18
+
19
+ def test_assert_one_arg_one_block
20
+ message = "my message here"
21
+ block = Proc.new { 1==1 }
22
+ result = @foo1.assert message, &block
23
+ assert result === true
24
+ end
25
+
26
+ def test_assert_two_args
27
+ message = "my message here"
28
+ bool = 1==1
29
+ result = @foo1.assert bool, message
30
+ assert result === true
31
+ end
32
+
33
+ def test_assert_fails
34
+ message = "my message here"
35
+ block = Proc.new { 1==0 }
36
+ assert_raise GUnit::AssertionFailure do
37
+ @foo1.assert message, &block
38
+ end
39
+ begin
40
+ @foo1.assert message, &block
41
+ rescue GUnit::AssertionFailure => e
42
+ assert_equal message, e.message
43
+ end
44
+ end
45
+
46
+ def test_assert_fails_no_message
47
+ block = Proc.new { 1==0 }
48
+ begin
49
+ @foo1.assert &block
50
+ rescue GUnit::AssertionFailure => e
51
+ assert e.message =~ /false != true/
52
+ end
53
+ end
54
+
55
+ def test_assert_equal_two_args
56
+ expected = 1
57
+ actual = 1
58
+ result = @foo1.assert_equal expected, actual
59
+ assert result === true
60
+ end
61
+
62
+ def test_assert_equal_one_arg_one_block
63
+ expected = 1
64
+ block = Proc.new { 0 + 1 }
65
+ result = @foo1.assert_equal expected, &block
66
+ assert result === true
67
+ end
68
+
69
+ def test_assert_equal_two_args_one_block
70
+ expected = 1
71
+ block = Proc.new { 0 + 1 }
72
+ message = "my message here"
73
+ result = @foo1.assert_equal expected, message, &block
74
+ assert result === true
75
+ end
76
+
77
+ def test_assert_equal_fails
78
+ expected = 2
79
+ message = "my message here"
80
+ block = Proc.new { 0 + 1 }
81
+ assert_raise GUnit::AssertionFailure do
82
+ @foo1.assert_equal expected, message, &block
83
+ end
84
+ begin
85
+ @foo1.assert_equal expected, message, &block
86
+ rescue GUnit::AssertionFailure => e
87
+ assert_equal message, e.message
88
+ end
89
+ end
90
+
91
+ def test_assert_equal_fails_no_message
92
+ expected = 2
93
+ block = Proc.new { 0 + 1 }
94
+ begin
95
+ @foo1.assert_equal expected, &block
96
+ rescue GUnit::AssertionFailure => e
97
+ assert e.message =~ /1 != 2/
98
+ end
99
+ end
100
+
101
+ def test_assert_raises_two_args_one_block
102
+ block = Proc.new { raise "boom" }
103
+ expected = "boom"
104
+ message = "my message here"
105
+ result = @foo1.assert_raises expected, message, &block
106
+ assert result === true
107
+ end
108
+
109
+ def test_assert_raises_two_args_with_class_one_block
110
+ block = Proc.new { raise StandardError }
111
+ expected = StandardError
112
+ message = "my message here"
113
+ result = @foo1.assert_raises expected, message, &block
114
+ assert result === true
115
+ end
116
+
117
+ def test_assert_raises_one_arg_one_block
118
+ block = Proc.new { raise "boom" }
119
+ expected = "boom"
120
+ result = @foo1.assert_raises expected, &block
121
+ assert result === true
122
+ end
123
+
124
+ def test_assert_raises_one_block
125
+ block = Proc.new { raise "boom" }
126
+ result = @foo1.assert_raises &block
127
+ assert result === true
128
+ end
129
+
130
+ def test_assert_raises_fails
131
+ block = Proc.new { raise "POWWWW" }
132
+ expected = "boom"
133
+ message = "my message here"
134
+ assert_raise GUnit::AssertionFailure do
135
+ @foo1.assert_raises expected, message, &block
136
+ end
137
+ begin
138
+ @foo1.assert_raises expected, message, &block
139
+ rescue GUnit::AssertionFailure => e
140
+ assert_equal message, e.message
141
+ end
142
+ end
143
+
144
+ def test_assert_raises_with_class_fails
145
+ block = Proc.new { raise Exception }
146
+ expected = StandardError
147
+ message = "my message here"
148
+ assert_raise GUnit::AssertionFailure do
149
+ @foo1.assert_raises expected, message, &block
150
+ end
151
+ begin
152
+ @foo1.assert_raises expected, message, &block
153
+ rescue GUnit::AssertionFailure => e
154
+ assert_equal message, e.message
155
+ end
156
+ end
157
+
158
+ def test_assert_raises_fails_no_message
159
+ block = Proc.new { raise Exception }
160
+ expected = StandardError
161
+ begin
162
+ @foo1.assert_raises expected, &block
163
+ rescue GUnit::AssertionFailure => e
164
+ assert e.message =~ /Expected StandardError to be raised, but got Exception/
165
+ end
166
+ end
167
+
168
+ def test_assert_raises_fails_nothing_raised_no_message
169
+ block = Proc.new { true }
170
+ expected = StandardError
171
+ begin
172
+ @foo1.assert_raises expected, &block
173
+ rescue GUnit::AssertionFailure => e
174
+ assert e.message =~ /Expected StandardError to be raised, but nothing raised/
175
+ end
176
+ end
177
+
178
+ def test_assert_raises_fails_no_expected_nothing_raised
179
+ block = Proc.new { true }
180
+ assert_raise GUnit::AssertionFailure do
181
+ @foo1.assert_raises &block
182
+ end
183
+ begin
184
+ @foo1.assert_raises &block
185
+ rescue GUnit::AssertionFailure => e
186
+ assert e.message =~ /Nothing raised/
187
+ end
188
+ end
189
+
190
+ end