baretest 0.1.0

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,240 @@
1
+ #--
2
+ # Copyright 2009 by Stefan Rusterholz.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #++
6
+
7
+
8
+
9
+ module Test
10
+ class Assertion
11
+ # used here to test for failure
12
+ def fails # :nodoc:
13
+ failed = false
14
+ begin
15
+ failed = !yield
16
+ rescue Test::Assertion::Failure
17
+ failed = true
18
+ end
19
+ unless failed then
20
+ failure "Expected the block to fail, but it returned a true value."
21
+ end
22
+
23
+ true
24
+ end
25
+ end
26
+ end
27
+
28
+ Test.define "Test" do
29
+ suite "Assertion" do
30
+ suite "Support" do
31
+ suite "#raises" do
32
+ assert "Should not fail when used without argument and the block raises an exception derived from StandardError." do
33
+ raises do raise "foo" end
34
+ end
35
+
36
+ assert "Should not fail when the block raises an exception derived from the provided exception-class." do
37
+ raises(ArgumentError) do raise ArgumentError, "foo" end
38
+ end
39
+
40
+ assert "Should fail when used without argument and the block doesn't raise." do
41
+ begin
42
+ raises do "nothing raised -> should fail" end
43
+ rescue ::Test::Assertion::Failure
44
+ true
45
+ else
46
+ false
47
+ end
48
+ end
49
+
50
+ assert "Should fail when the block raises an exception not derived from the provided exception-class." do
51
+ begin
52
+ raises(TypeError) do raise ArgumentError, "different class -> should fail" end
53
+ rescue ::Test::Assertion::Failure
54
+ true
55
+ else
56
+ false
57
+ end
58
+ end
59
+ end # raises
60
+
61
+ suite "#raises_nothing" do
62
+ assert "Should not fail when the block doesn't raise." do
63
+ raises_nothing do; end
64
+ end
65
+
66
+ assert "Should fail when the block raises." do
67
+ begin
68
+ raises_nothing do raise "anything" end
69
+ rescue ::Test::Assertion::Failure
70
+ true
71
+ else
72
+ false
73
+ end
74
+ end
75
+ end
76
+
77
+ suite "#touch/#touched" do
78
+ suite "When you don't touch(x), touched(x) should fail" do
79
+ assert "When you don't touch at all, touched(x) should fail" do
80
+ fails do
81
+ touched :foo1
82
+ end
83
+ end
84
+
85
+ assert "When you don't touch something else, touched(x) should fail" do
86
+ fails do
87
+ touch :bar2
88
+ touched :foo2
89
+ end
90
+ end
91
+ end
92
+
93
+ suite "When you touch(x), touched(x) should not fail" do
94
+ assert "When you touch(x), touched(x) should be true" do
95
+ touch :foo3
96
+ touched :foo3
97
+ end
98
+
99
+ assert "When you touch(x) multiple times, touched(x) should be true" do
100
+ 3.times { touch :foo4 }
101
+ touched :foo4
102
+ end
103
+ end
104
+
105
+ suite "Touching in one assertion shouldn't carry over to another assertion" do
106
+ assert "Touch x. Preparation for next assertion" do
107
+ touch(:foo5)
108
+ end
109
+
110
+ assert "No touch x, touched x should raise." do
111
+ fails do touched(:foo5) end
112
+ end
113
+ end
114
+ end # #touch/#touched
115
+
116
+ suite "#within_delta" do
117
+ assert "Should not fail when the value is within the delta." do
118
+ within_delta(3.0, 3.01, 0.02)
119
+ end
120
+
121
+ assert "Should fail when the value is not within the delta." do
122
+ fails do
123
+ within_delta(3.0, 3.03, 0.02)
124
+ end
125
+ end
126
+ end # within_delta
127
+
128
+ suite "#equal_unordered" do
129
+ assert "Should not fail when the two arrays contain the same items the same number of times." do
130
+ equal_unordered([1,2,3], [3,1,2])
131
+ end
132
+
133
+ assert "Should fail when the two arrays don't contain the same items." do
134
+ fails do
135
+ equal_unordered([1,2,3], [5,6,1])
136
+ end
137
+ end
138
+
139
+ assert "Should fail when the two arrays contain the same items a different number of times." do
140
+ fails do
141
+ equal_unordered([1,2,3], [3,1,2,2])
142
+ end
143
+ end
144
+ end # equal_unordered
145
+
146
+ suite "#same" do
147
+ assert "Should not fail when the values are the same object." do
148
+ a = "foo"
149
+ same(a, a)
150
+ end
151
+
152
+ assert "Should fail when the values are not the same object." do
153
+ fails do
154
+ same("a", "b")
155
+ end
156
+ end
157
+ end # same
158
+
159
+ suite "#order_equal" do
160
+ assert "Should not fail when the values are equal by ==." do
161
+ order_equal(1, 1.0)
162
+ end
163
+
164
+ assert "Should fail when the values are not equal by ==." do
165
+ fails do
166
+ order_equal(1, 1.1)
167
+ end
168
+ end
169
+ end # order_equal
170
+
171
+ suite "#hash_key_equal" do
172
+ assert "Should not fail when the values are the same object." do
173
+ hash_key_equal("foo", "foo")
174
+ end
175
+
176
+ assert "Should fail when the values are not the same object." do
177
+ fails do
178
+ hash_key_equal("foo", "bar")
179
+ end
180
+ end
181
+ end # hash_key_equal
182
+
183
+ suite "#case_equal" do
184
+ assert "Should not fail when the values are the same object." do
185
+ case_equal(String, "foo")
186
+ end
187
+
188
+ assert "Should fail when the values are not the same object." do
189
+ fails do
190
+ case_equal(String, [])
191
+ end
192
+ end
193
+ end # case_equal
194
+
195
+ suite "#kind_of" do
196
+ assert "Should not fail when the value is an instance of the given class" do
197
+ kind_of(Array, [])
198
+ end
199
+
200
+ assert "Should not fail when the value is an instance of a subclass of the given class" do
201
+ kind_of(Enumerable, [])
202
+ end
203
+
204
+ assert "Should fail when the value is not instance of the given class or subclass" do
205
+ fails do
206
+ kind_of(String, [])
207
+ end
208
+ end
209
+ end
210
+
211
+ suite "#failure_with_optional_message" do
212
+ assert "Should raise a Test::Assertion::Failure" do
213
+ raises(::Test::Assertion::Failure) do
214
+ failure_with_optional_message "With %s", "Without message", "message"
215
+ end
216
+ end
217
+
218
+ assert "Should use the string with message if message is given" do
219
+ raises(::Test::Assertion::Failure, :with_message => "With message") do
220
+ failure_with_optional_message "With %s", "Without message", "message"
221
+ end
222
+ end
223
+
224
+ assert "Should use the string without message if no message is given" do
225
+ raises(::Test::Assertion::Failure, :with_message => "Without message") do
226
+ failure_with_optional_message "With %s", "Without message", nil
227
+ end
228
+ end
229
+ end
230
+
231
+ suite "#failure" do
232
+ assert "Should raise a Test::Assertion::Failure." do
233
+ raises(::Test::Assertion::Failure) do
234
+ failure "Should raise that exception."
235
+ end
236
+ end
237
+ end
238
+ end # Support
239
+ end # Assertion
240
+ end # Test
@@ -0,0 +1,142 @@
1
+ #--
2
+ # Copyright 2009 by Stefan Rusterholz.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #++
6
+
7
+
8
+
9
+ Test.define "Test" do
10
+ suite "Assertion" do
11
+ suite "::new" do
12
+ assert "Should return a ::Test::Assertion instance" do
13
+ ::Test::Assertion.new(nil, "description") { nil }.class ==
14
+ ::Test::Assertion
15
+ end
16
+
17
+ assert "Should expect exactly 2 arguments" do
18
+ raises(ArgumentError) { ::Test::Assertion.new() } &&
19
+ raises(ArgumentError) { ::Test::Assertion.new(nil) } &&
20
+ raises(ArgumentError) { ::Test::Assertion.new(nil, "foo", "bar") }
21
+ end
22
+ end
23
+
24
+ suite "#status" do
25
+ assert "A new Assertion should have a status of nil" do
26
+ ::Test::Assertion.new(nil, "description") {}.status.nil?
27
+ end
28
+
29
+ assert "Executing an assertion with a block that returns true should be :success" do
30
+ assertion_success = ::Test::Assertion.new(nil, "description") { true }
31
+ assertion_success.execute
32
+ assertion_success.status == :success
33
+ end
34
+
35
+ assert "Executing an assertion with a block that returns false should be :failure" do
36
+ assertion_success = ::Test::Assertion.new(nil, "description") { false }
37
+ assertion_success.execute
38
+ assertion_success.status == :failure
39
+ end
40
+
41
+ assert "Executing an assertion with a block that raises should be :error" do
42
+ assertion_success = ::Test::Assertion.new(nil, "description") { raise }
43
+ assertion_success.execute
44
+ assertion_success.status == :error
45
+ end
46
+
47
+ assert "Executing an assertion without a block should be :pending" do
48
+ assertion_success = ::Test::Assertion.new(nil, "description")
49
+ assertion_success.execute
50
+
51
+ same :expected => :pending, :actual => assertion_success.status
52
+ end
53
+ end
54
+
55
+ suite "#exception" do
56
+ assert "An assertion that doesn't raise should have nil as exception" do
57
+ assertion_success = ::Test::Assertion.new(nil, "description") { true }
58
+ assertion_success.execute
59
+ same :expected => nil, :actual => assertion_success.exception
60
+ end
61
+ end
62
+
63
+ suite "#description" do
64
+ assert "An assertion should have a description" do
65
+ description = "The assertion description"
66
+ assertion = ::Test::Assertion.new(nil, description) { true }
67
+ same :expected => description, :actual => assertion.description
68
+ end
69
+ end
70
+
71
+ suite "#suite" do
72
+ assert "An assertion can belong to a suite" do
73
+ suite = ::Test::Suite.new
74
+ assertion = ::Test::Assertion.new(suite, "") { true }
75
+ same :expected => suite, :actual => assertion.suite
76
+ end
77
+ end
78
+
79
+ suite "#block" do
80
+ assert "An assertion can have a block" do
81
+ block = proc { true }
82
+ assertion = ::Test::Assertion.new(nil, "", &block)
83
+ same :expected => block, :actual => assertion.block
84
+ end
85
+ end
86
+
87
+ suite "#setup" do
88
+ assert "Should run all enclosing suite's setup blocks, outermost first" do
89
+ executed = []
90
+ block1 = proc { executed << :block1 }
91
+ block2 = proc { executed << :block2 }
92
+ suite1 = ::Test::Suite.new("block1") do setup(&block1) end
93
+ suite2 = ::Test::Suite.new("suite2", suite1) do setup(&block2) end
94
+ assertion = ::Test::Assertion.new(suite2, "assertion")
95
+
96
+ raises_nothing do assertion.setup end &&
97
+ equal([:block1, :block2], executed)
98
+ end
99
+ end
100
+
101
+ suite "#teardown" do
102
+ assert "Should run all enclosing suite's teardown blocks, innermost first" do
103
+ executed = []
104
+ block1 = proc { executed << :block1 }
105
+ block2 = proc { executed << :block2 }
106
+ suite1 = ::Test::Suite.new("block1") do teardown(&block1) end
107
+ suite2 = ::Test::Suite.new("suite2", suite1) do teardown(&block2) end
108
+ assertion = ::Test::Assertion.new(suite2, "assertion")
109
+
110
+ raises_nothing do assertion.teardown end &&
111
+ equal([:block2, :block1], executed)
112
+ end
113
+ end
114
+
115
+ suite "#execute" do
116
+ assert "Execute will run the assertion's block" do
117
+ this = self # needed because touch is called in the block of another assertion, so otherwise it'd be local to that assertion
118
+ assertion = ::Test::Assertion.new(nil, "") { this.touch(:execute) }
119
+ assertion.execute
120
+ touched(:execute)
121
+ end
122
+ end
123
+
124
+ suite "#clean_copy" do
125
+ assert "Should return an instance of Test::Assertion" do
126
+ kind_of(::Test::Assertion, ::Test::Assertion.new("", nil).clean_copy)
127
+ end
128
+
129
+ assert "Should have the same description, suite and block" do
130
+ description = "description"
131
+ suite = ::Test::Suite.new
132
+ block = proc { true }
133
+ assertion1 = ::Test::Assertion.new(description, suite, &block)
134
+ assertion2 = assertion1.clean_copy
135
+
136
+ same(assertion1.description, assertion2.description, "description") &&
137
+ same(assertion1.suite, assertion2.suite, "suite") &&
138
+ same(assertion1.block, assertion2.block, "block")
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,63 @@
1
+ #--
2
+ # Copyright 2009 by Stefan Rusterholz.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #++
6
+
7
+
8
+
9
+ Test.define "Test with test/debug", :requires => 'test/debug' do
10
+ suite "Assertion with test/debug", :requires => 'test/debug' do
11
+ suite "#to_s" do
12
+ assert "Assertion should have a to_s which contains the classname and the description" do
13
+ description = "the description"
14
+ assertion = Test::Assertion.new(nil, description)
15
+ print_string = assertion.to_s
16
+
17
+ print_string.include?(assertion.class.name) &&
18
+ print_string.include?(description)
19
+ end
20
+ end
21
+
22
+ suite "#inspect" do
23
+ assert "Assertion should have an inspect which contains the classname, the shifted object-id in zero-padded hex, the suite's inspect and the description's inspect" do
24
+ suite = Test::Suite.new
25
+ description = "the description"
26
+ assertion = Test::Assertion.new(suite, description)
27
+ def suite.inspect; "<inspect of suite>"; end
28
+
29
+ inspect_string = assertion.inspect
30
+
31
+ inspect_string.include?(assertion.class.name) &&
32
+ inspect_string.include?("%08x" % (assertion.object_id >> 1)) &&
33
+ inspect_string.include?(suite.inspect) &&
34
+ inspect_string.include?(description.inspect)
35
+ end
36
+ end
37
+ end
38
+
39
+ suite "Suite with test/debug", :requires => 'test/debug' do
40
+ suite "#to_s" do
41
+ assert "Suite should have a to_s which contains the classname and the description" do
42
+ description = "the description"
43
+ suite = Test::Suite.new(description)
44
+ print_string = suite.to_s
45
+
46
+ print_string.include?(suite.class.name) &&
47
+ print_string.include?(description)
48
+ end
49
+ end
50
+
51
+ suite "#inspect" do
52
+ assert "Suite should have an inspect which contains the classname, the shifted object-id in zero-padded hex and the description's inspect" do
53
+ description = "the description"
54
+ suite = Test::Suite.new(description)
55
+ inspect_string = suite.inspect
56
+
57
+ inspect_string.include?(suite.class.name) &&
58
+ inspect_string.include?("%08x" % (suite.object_id >> 1)) &&
59
+ inspect_string.include?(description.inspect)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,10 @@
1
+ #--
2
+ # Copyright 2009 by Stefan Rusterholz.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #++
6
+
7
+
8
+
9
+ # If you got a viable idea on how to test this, mail me, I'll owe you a beer.
10
+ # Maybe I should mock IRB and expect the setup to happen for failing/erroring assertions
@@ -0,0 +1,9 @@
1
+ #--
2
+ # Copyright 2009 by Stefan Rusterholz.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #++
6
+
7
+
8
+
9
+ # Not yet testing runners
@@ -0,0 +1,9 @@
1
+ #--
2
+ # Copyright 2009 by Stefan Rusterholz.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #++
6
+
7
+
8
+
9
+ # Not yet testing runners
@@ -0,0 +1,9 @@
1
+ #--
2
+ # Copyright 2009 by Stefan Rusterholz.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #++
6
+
7
+
8
+
9
+ # Not yet testing runners
@@ -0,0 +1,9 @@
1
+ #--
2
+ # Copyright 2009 by Stefan Rusterholz.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #++
6
+
7
+
8
+
9
+ # Not yet testing runners
@@ -0,0 +1,9 @@
1
+ #--
2
+ # Copyright 2009 by Stefan Rusterholz.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #++
6
+
7
+
8
+
9
+ # Not yet testing runners
@@ -0,0 +1,9 @@
1
+ #--
2
+ # Copyright 2009 by Stefan Rusterholz.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #++
6
+
7
+
8
+
9
+ # Not yet testing runners