baretest 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,235 @@
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 "Run" do
11
+ suite "::new" do
12
+ assert "Should return an instance of Run" do
13
+ kind_of ::Test::Run, ::Test::Run.new(::Test::Suite.new)
14
+ end
15
+
16
+ assert "Should accept 1-2 arguments" do
17
+ raises(ArgumentError) do ::Test::Run.new end &&
18
+ raises_nothing do ::Test::Run.new(::Test::Suite.new) end &&
19
+ raises_nothing do ::Test::Run.new(::Test::Suite.new, {}) end &&
20
+ raises(ArgumentError) do ::Test::Run.new(::Test::Suite.new, {}, nil) end
21
+ end
22
+
23
+ assert "Should accept an option ':format'" do
24
+ raises_nothing do ::Test::Run.new(::Test::Suite.new, :format => 'spec') end
25
+ end
26
+
27
+ assert "Should use the formatter specified in the :format option" do
28
+ run = ::Test::Run.new(::Test::Suite.new, :format => 'spec')
29
+ kind_of(::Test::Run::Spec, run)
30
+ end
31
+
32
+ assert "Should accept an option ':interactive' and load irb_mode" do
33
+ run = ::Test::Run.new(::Test::Suite.new, :interactive => true)
34
+ kind_of(::Test::IRBMode, run)
35
+ end
36
+ end
37
+
38
+ suite "#suite" do
39
+ assert "Should return the suite the instance was initialized with" do
40
+ suite = ::Test::Suite.new
41
+ run = ::Test::Run.new(suite)
42
+
43
+ same(suite, run.suite)
44
+ end
45
+ end
46
+
47
+ suite "#inits" do
48
+ setup do
49
+ Test.extender.clear # avoid interference
50
+ @executed = []
51
+ executed = @executed # for closure
52
+ @init_blocks = [
53
+ proc { executed << :block1 },
54
+ proc { executed << :block2 }
55
+ ]
56
+ init_blocks = @init_blocks # for closure
57
+ @extender = Module.new do |m|
58
+ (class <<m;self;end).send(:define_method, :extended) do |by|
59
+ init_blocks.each { |init_block|
60
+ by.init(&init_block)
61
+ }
62
+ end
63
+ end
64
+ $LOADED_FEATURES << 'test/run/test_init.rb' unless $LOADED_FEATURES.include?('test/run/test_init.rb') # suppress require
65
+ ::Test.format['test/run/test_init'] = @extender # provide the module as formatter
66
+ end
67
+
68
+ assert "Should return the array with blocks called at the end of initialize" do
69
+ run = ::Test::Run.new(::Test::Suite.new, :format => 'test_init')
70
+ equal(@init_blocks, run.inits)
71
+ end
72
+
73
+ assert "Should run the blocks at the end of initialize" do
74
+ run = ::Test::Run.new(::Test::Suite.new, :format => 'test_init')
75
+ equal([:block1, :block2], @executed)
76
+ end
77
+ end
78
+
79
+ suite "#run_all" do
80
+ assert "Invokes #run_suite with the Run instance's toplevel suite" do
81
+ invoked_suites = []
82
+ extender = Module.new do |m|
83
+ define_method :run_suite do |suite|
84
+ invoked_suites << suite
85
+ end
86
+ end
87
+ toplevel_suite = ::Test::Suite.new
88
+ $LOADED_FEATURES << 'test/run/test_init.rb' unless $LOADED_FEATURES.include?('test/run/test_init.rb') # suppress require
89
+ ::Test.format['test/run/test_init'] = extender # provide the module as formatter
90
+ run = ::Test::Run.new(toplevel_suite, :format => 'test_init')
91
+ run.run_all
92
+
93
+ equal([toplevel_suite], invoked_suites)
94
+ end
95
+ end
96
+
97
+ suite "#run_suite" do
98
+ assert "Invokes #run_suite with every suite in the given suite" do
99
+ invoked_suites = []
100
+ extender = Module.new do |m|
101
+ define_method :run_suite do |suite|
102
+ invoked_suites << suite
103
+ super(suite)
104
+ end
105
+ end
106
+ suites = [
107
+ ::Test::Suite.new,
108
+ ::Test::Suite.new
109
+ ]
110
+ toplevel_suite = ::Test::Suite.new
111
+ toplevel_suite.suites.concat(suites) # HAX, should have an API for this
112
+ suites.unshift(toplevel_suite)
113
+ $LOADED_FEATURES << 'test/run/test_init.rb' unless $LOADED_FEATURES.include?('test/run/test_init.rb') # suppress require
114
+ ::Test.format['test/run/test_init'] = extender # provide the module as formatter
115
+ run = ::Test::Run.new(toplevel_suite, :format => 'test_init')
116
+ run.run_suite(toplevel_suite)
117
+
118
+ equal_unordered(suites, invoked_suites)
119
+ end
120
+
121
+ assert "Invokes #run_test with every suite in the given suite" do
122
+ invoked_tests = []
123
+ extender = Module.new do |m|
124
+ define_method :run_test do |test|
125
+ invoked_tests << test
126
+ end
127
+ end
128
+ toplevel_suite = ::Test::Suite.new
129
+ assertions = [
130
+ ::Test::Assertion.new(toplevel_suite, "assertion1"),
131
+ ::Test::Assertion.new(toplevel_suite, "assertion2")
132
+ ]
133
+ toplevel_suite.tests.concat(assertions) # HAX, should have an API for this
134
+ $LOADED_FEATURES << 'test/run/test_init.rb' unless $LOADED_FEATURES.include?('test/run/test_init.rb') # suppress require
135
+ ::Test.format['test/run/test_init'] = extender # provide the module as formatter
136
+ run = ::Test::Run.new(toplevel_suite, :format => 'test_init')
137
+ run.run_all
138
+
139
+ equal_unordered(assertions, invoked_tests)
140
+ end
141
+
142
+ assert "Increments the counter ':suite' at the end" do
143
+ toplevel_suite = ::Test::Suite.new
144
+ run = ::Test::Run.new(toplevel_suite)
145
+
146
+ count_before = run.count[:suite]
147
+ run.run_suite(toplevel_suite)
148
+ count_after = run.count[:suite]
149
+
150
+ equal(count_before+1, count_after)
151
+ end
152
+ end
153
+
154
+ suite "#run_test" do
155
+ assert "Runs the given test" do
156
+ # should implement this with a mock, expecting #execute to be called
157
+ assertion = ::Test::Assertion.new(nil, nil) do true end
158
+ run = ::Test::Run.new(::Test::Suite.new)
159
+ run.run_test(assertion)
160
+
161
+ equal(:success, assertion.status)
162
+ end
163
+
164
+ assert "Increments the counter ':test' at the end" do
165
+ assertion = ::Test::Assertion.new(nil, "") do true end
166
+ run = ::Test::Run.new(::Test::Suite.new)
167
+ count_before = run.count[:test]
168
+ run.run_test(assertion)
169
+ count_after = run.count[:test]
170
+
171
+ equal(count_before+1, count_after)
172
+ end
173
+
174
+ suite "The given test was a success" do
175
+ assert "Increments the counter ':success' at the end" do
176
+ assertion = ::Test::Assertion.new(nil, "") do true end
177
+ run = ::Test::Run.new(::Test::Suite.new)
178
+ count_before = run.count[:success]
179
+ run.run_test(assertion)
180
+ count_after = run.count[:success]
181
+
182
+ equal(count_before+1, count_after)
183
+ end
184
+ end
185
+
186
+ suite "The given test was pending" do
187
+ assert "Increments the counter ':pending' at the end" do
188
+ assertion = ::Test::Assertion.new(nil, "")
189
+ run = ::Test::Run.new(::Test::Suite.new)
190
+ count_before = run.count[:pending]
191
+ run.run_test(assertion)
192
+ count_after = run.count[:pending]
193
+
194
+ equal(count_before+1, count_after)
195
+ end
196
+ end
197
+
198
+ suite "The given test was skipped" do
199
+ assert "Increments the counter ':skipped' at the end" do
200
+ assertion = ::Test::Skipped::Assertion.new(nil, "")
201
+ run = ::Test::Run.new(::Test::Suite.new)
202
+ count_before = run.count[:skipped]
203
+ run.run_test(assertion)
204
+ count_after = run.count[:skipped]
205
+
206
+ equal(count_before+1, count_after)
207
+ end
208
+ end
209
+
210
+ suite "The given test was failure" do
211
+ assert "Increments the counter ':failure' at the end" do
212
+ assertion = ::Test::Assertion.new(nil, "") do false end
213
+ run = ::Test::Run.new(::Test::Suite.new)
214
+ count_before = run.count[:failure]
215
+ run.run_test(assertion)
216
+ count_after = run.count[:failure]
217
+
218
+ equal(count_before+1, count_after)
219
+ end
220
+ end
221
+
222
+ suite "The given test was error" do
223
+ assert "Increments the counter ':error' at the end" do
224
+ assertion = ::Test::Assertion.new(nil, "") do raise end
225
+ run = ::Test::Run.new(::Test::Suite.new)
226
+ count_before = run.count[:error]
227
+ run.run_test(assertion)
228
+ count_after = run.count[:error]
229
+
230
+ equal(count_before+1, count_after)
231
+ end
232
+ end
233
+ end
234
+ end
235
+ end
@@ -0,0 +1,275 @@
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", :requires => 'test/debug' do
10
+ suite "Assertion" do
11
+ suite "::create" do
12
+ assert "Should accept 0-3 arguments" do
13
+ raises_nothing { ::Test::Suite.create() } &&
14
+ raises_nothing { ::Test::Suite.create(nil) } &&
15
+ raises_nothing { ::Test::Suite.create(nil, nil) } &&
16
+ raises_nothing { ::Test::Suite.create(nil, nil, {}) } &&
17
+ raises(ArgumentError) { ::Test::Suite.create(nil, nil, {}, nil) }
18
+ end
19
+
20
+ assert "Should require a single file listed in :requires option." do
21
+ a = self # ruby1.9 fix, no longer yields self with instance_eval
22
+ original_require = Kernel.instance_method(:require)
23
+ file = 'foo/bar'
24
+ Kernel.send(:define_method, :require) do |file, *args| a.touch(file) end
25
+ ::Test::Suite.create(nil, nil, :requires => file)
26
+ Kernel.send(:define_method, :require, original_require)
27
+
28
+ touched file
29
+ end
30
+
31
+ assert "Should require all files listed in :requires option." do
32
+ a = self # ruby1.9 fix, no longer yields self with instance_eval
33
+ original_require = Kernel.instance_method(:require)
34
+ files = %w[moo/bar moo/baz moo/quuz]
35
+ Kernel.send(:define_method, :require) do |file, *args| a.touch(file) end
36
+ ::Test::Suite.create(nil, nil, :requires => files)
37
+ Kernel.send(:define_method, :require, original_require)
38
+
39
+ files.all? { |file| touched file }
40
+ end
41
+
42
+ assert "Should return a ::Test::Suite instance." do
43
+ ::Test::Suite.create {}.class == ::Test::Suite
44
+ end
45
+
46
+ assert "Should return a ::Test::Suite instance without a block." do
47
+ ::Test::Suite.create.class == ::Test::Skipped::Suite
48
+ end
49
+
50
+ assert "Should return a ::Test::Skipped::Suite instance if a required file is not available." do
51
+ original_require = Kernel.instance_method(:require)
52
+ Kernel.send(:define_method, :require) do |*args| raise LoadError end # simulate that the required file was not found
53
+ return_value = ::Test::Suite.create(nil, nil, :requires => 'fake')
54
+ Kernel.send(:define_method, :require, original_require)
55
+
56
+ return_value.class == ::Test::Skipped::Suite
57
+ end
58
+ end
59
+
60
+ suite "::new" do
61
+ assert "Should return a ::Test::Suite instance" do
62
+ ::Test::Suite.new(nil, nil).class == ::Test::Suite
63
+ end
64
+
65
+ assert "Should accept 0-2 arguments" do
66
+ raises_nothing { ::Test::Suite.new() } &&
67
+ raises_nothing { ::Test::Suite.new(nil) } &&
68
+ raises_nothing { ::Test::Suite.new(nil, nil) } &&
69
+ raises(ArgumentError) { ::Test::Suite.new(nil, nil, nil) }
70
+ end
71
+ end
72
+
73
+ suite "#suites" do
74
+ assert "Should return all the suites defined in the block." do
75
+ expected_descriptions = %w[a b c]
76
+ suite = ::Test::Suite.new do
77
+ expected_descriptions.each { |desc|
78
+ suite desc
79
+ }
80
+ end
81
+ actual_descriptions = suite.suites.map { |child| child.description }
82
+
83
+ equal(
84
+ :expected => 3,
85
+ :actual => suite.suites.size,
86
+ :message => "number of defined suites"
87
+ ) &&
88
+ equal_unordered(
89
+ :expected => expected_descriptions,
90
+ :actual => actual_descriptions,
91
+ :message => "the descriptions"
92
+ )
93
+ end
94
+ end
95
+
96
+ suite "#tests" do
97
+ assert "Should return all the suites defined in the block." do
98
+ expected_descriptions = %w[a b c]
99
+ suite = ::Test::Suite.new do
100
+ expected_descriptions.each { |desc|
101
+ assert desc
102
+ }
103
+ end
104
+ actual_descriptions = suite.tests.map { |child| child.description }
105
+
106
+ equal(
107
+ :expected => 3,
108
+ :actual => suite.tests.size,
109
+ :message => "number of defined tests"
110
+ ) &&
111
+ equal_unordered(
112
+ :expected => expected_descriptions,
113
+ :actual => actual_descriptions,
114
+ :message => "the descriptions"
115
+ )
116
+ end
117
+ end
118
+
119
+ suite "#description" do
120
+ assert "A suite should have a description" do
121
+ description = "The suite description"
122
+ suite = ::Test::Suite.new(description)
123
+ equal :expected => description, :actual => suite.description, :message => 'suite description'
124
+ end
125
+ end
126
+
127
+ suite "#parent" do
128
+ assert "A suite can have a parent suite" do
129
+ parent = ::Test::Suite.new
130
+ suite = ::Test::Suite.new("", parent)
131
+ same :expected => suite.parent, :actual => parent, :message => "suite's parent"
132
+ end
133
+ end
134
+
135
+ suite "#ancestors" do
136
+ assert "A suite can have ancestors" do
137
+ grand_parent = ::Test::Suite.new("first")
138
+ parent = ::Test::Suite.new("second", grand_parent)
139
+ suite = ::Test::Suite.new("third", parent)
140
+ equal :expected => suite.ancestors, :actual => [suite, parent, grand_parent], :message => "suite's ancestors"
141
+ end
142
+ end
143
+
144
+ suite "#suite" do
145
+ assert "Should add new suites to a suite." do
146
+ suite = ::Test::Suite.new
147
+ equal(
148
+ :expected => 0,
149
+ :actual => suite.suites.size,
150
+ :message => "number of defined suites before adding any"
151
+ )
152
+
153
+ suite.suite "a"
154
+ equal(
155
+ :expected => 1,
156
+ :actual => suite.suites.size,
157
+ :message => "number of defined suites after adding one"
158
+ )
159
+
160
+ suite.suite "b"
161
+ equal(
162
+ :expected => 2,
163
+ :actual => suite.suites.size,
164
+ :message => "number of defined suites after adding two"
165
+ )
166
+
167
+ equal_unordered(
168
+ :expected => ['a', 'b'],
169
+ :actual => suite.suites.map { |child| child.description },
170
+ :message => "the descriptions"
171
+ )
172
+ end
173
+
174
+ assert "Added suites should have the receiving suite as parent." do
175
+ parent = ::Test::Suite.new
176
+ parent.suite "a"
177
+ child = parent.suites.first
178
+
179
+ same(
180
+ :expected => parent,
181
+ :actual => child.parent,
182
+ :message => "the parent suite"
183
+ )
184
+ end
185
+ end
186
+
187
+ suite "#setup" do
188
+ assert "Called with a block it should add a new setup block." do
189
+ suite = ::Test::Suite.new
190
+ block = proc {}
191
+ before = suite.setup.dup
192
+
193
+ suite.setup(&block)
194
+ after = suite.setup.dup
195
+
196
+ equal(
197
+ :expected => 1,
198
+ :actual => after.size-before.size,
199
+ :message => "number of new setup blocks after adding one"
200
+ ) &&
201
+ same(
202
+ :expected => (after-before).first,
203
+ :actual => block,
204
+ :message => "the new block"
205
+ )
206
+
207
+ end
208
+ end
209
+
210
+ suite "#teardown" do
211
+ assert "Called with a block it should add a new teardown block." do
212
+ suite = ::Test::Suite.new
213
+ block = proc {}
214
+ before = suite.teardown.dup
215
+
216
+ suite.teardown(&block)
217
+ after = suite.teardown.dup
218
+
219
+ equal(
220
+ :expected => 1,
221
+ :actual => after.size-before.size,
222
+ :message => "number of new teardown blocks after adding one"
223
+ ) &&
224
+ same(
225
+ :expected => (after-before).first,
226
+ :actual => block,
227
+ :message => "the new block"
228
+ )
229
+ end
230
+ end
231
+
232
+ suite "#assert" do
233
+ assert "Should add new assertions to a suite." do
234
+ suite = ::Test::Suite.new
235
+ equal(
236
+ :expected => 0,
237
+ :actual => suite.tests.size,
238
+ :message => "number of defined tests before adding any"
239
+ )
240
+
241
+ suite.assert "a"
242
+ equal(
243
+ :expected => 1,
244
+ :actual => suite.tests.size,
245
+ :message => "number of defined tests after adding one"
246
+ )
247
+
248
+ suite.assert "b"
249
+ equal(
250
+ :expected => 2,
251
+ :actual => suite.tests.size,
252
+ :message => "number of defined tests after adding two"
253
+ )
254
+
255
+ equal_unordered(
256
+ :expected => ['a', 'b'],
257
+ :actual => suite.tests.map { |child| child.description },
258
+ :message => "the descriptions"
259
+ )
260
+ end
261
+
262
+ assert "Added tests should have the receiving suite as suite." do
263
+ suite = ::Test::Suite.new
264
+ suite.assert "a"
265
+ assertion = suite.tests.first
266
+
267
+ same(
268
+ :expected => suite,
269
+ :actual => assertion.suite,
270
+ :message => "the suite"
271
+ )
272
+ end
273
+ end
274
+ end
275
+ end