SlimTest 4.6.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require 'focus'
4
+
5
+ class TestFocus < MiniTest::Unit::TestCase
6
+ def setup
7
+ @x = 1
8
+ end
9
+
10
+ def teardown
11
+ assert_equal 2, @x
12
+ end
13
+
14
+ def test_focus
15
+ @x += 1
16
+ end
17
+
18
+ def test_ignore1
19
+ flunk "ignore me!"
20
+ end
21
+
22
+ def test_ignore2
23
+ flunk "ignore me!"
24
+ end
25
+
26
+ def test_ignore3
27
+ flunk "ignore me!"
28
+ end
29
+
30
+ def test_focus2
31
+ @x += 1
32
+ end
33
+
34
+ focus :test_focus, :test_focus2
35
+ end
@@ -0,0 +1,372 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require 'rubygems'
4
+ require 'minitest/autorun'
5
+
6
+ require 'stringio'
7
+
8
+ $TESTING = true
9
+
10
+ require 'unit_diff'
11
+
12
+ class TestUnitDiff < MiniTest::Unit::TestCase
13
+
14
+ def setup
15
+ @diff = UnitDiff.new
16
+ end
17
+
18
+ def test_input
19
+ header = "Loaded suite ./blah\nStarted\nFF\nFinished in 0.035332 seconds.\n\n"
20
+ input = "#{header} 1) Failure:\ntest_test1(TestBlah) [./blah.rb:25]:\n<\"line1\\nline2\\nline3\\n\"> expected but was\n<\"line4\\nline5\\nline6\\n\">.\n\n 2) Failure:\ntest_test2(TestBlah) [./blah.rb:29]:\n<\"line1\"> expected but was\n<\"line2\\nline3\\n\\n\">.\n\n2 tests, 2 assertions, 2 failures, 0 errors\n"
21
+
22
+ # TODO: I think I'd like a separate footer array as well
23
+ expected = [[[" 1) Failure:\n", "test_test1(TestBlah) [./blah.rb:25]:\n", "<\"line1\\nline2\\nline3\\n\"> expected but was\n", "<\"line4\\nline5\\nline6\\n\">.\n"],
24
+ [" 2) Failure:\n", "test_test2(TestBlah) [./blah.rb:29]:\n", "<\"line1\"> expected but was\n", "<\"line2\\nline3\\n\\n\">.\n"]],
25
+ ["\n", "2 tests, 2 assertions, 2 failures, 0 errors\n"]]
26
+
27
+ util_unit_diff(header, input, expected, :parse_input)
28
+ end
29
+
30
+ def test_input_miniunit
31
+ header = "Loaded suite -e\nStarted\nF\nFinished in 0.035332 seconds.\n\n"
32
+ input = "#{header} 1) Failure:
33
+ test_blah(TestBlah) [./blah.rb:25]:
34
+ Expected ['a', 'b', 'c'], not ['a', 'c', 'b'].
35
+
36
+ 1 tests, 1 assertions, 1 failures, 0 errors
37
+ "
38
+
39
+ expected = [[[" 1) Failure:\n",
40
+ "test_blah(TestBlah) [./blah.rb:25]:\n",
41
+ "Expected ['a', 'b', 'c'], not ['a', 'c', 'b'].\n"]],
42
+ ["\n", "1 tests, 1 assertions, 1 failures, 0 errors\n"]]
43
+
44
+ util_unit_diff(header, input, expected, :parse_input)
45
+ end
46
+
47
+ def test_input_miniunit_multiline
48
+ header = "Loaded suite -e\nStarted\nF\nFinished in 0.035332 seconds.\n\n"
49
+ input = "#{header} 1) Failure:
50
+ test_blah(TestBlah) [./blah.rb:25]:
51
+ Expected ['a',
52
+ 'b',
53
+ 'c'], not ['a',
54
+ 'c',
55
+ 'b'].
56
+
57
+ 1 tests, 1 assertions, 1 failures, 0 errors
58
+ "
59
+
60
+ expected = [[[" 1) Failure:\n",
61
+ "test_blah(TestBlah) [./blah.rb:25]:\n",
62
+ "Expected ['a',\n 'b',\n 'c'], not ['a',\n 'c',\n 'b'].\n"]],
63
+ ["\n", "1 tests, 1 assertions, 1 failures, 0 errors\n"]]
64
+
65
+ util_unit_diff(header, input, expected, :parse_input)
66
+ end
67
+
68
+ def test_input_mspec
69
+ header = <<-HEADER
70
+ Started
71
+ .......F
72
+ Finished in 0.1 seconds
73
+
74
+ HEADER
75
+
76
+ failure = <<-FAILURE
77
+ 1)
78
+ The unless expression should fail FAILED
79
+ Expected nil to equal "baz":
80
+ FAILURE
81
+
82
+ backtrace = <<-BACKTRACE
83
+ PositiveExpectation#== at spec/mspec.rb:217
84
+ main.__script__ {} at spec/language/unless_spec.rb:49
85
+ Proc#call at kernel/core/proc.rb:127
86
+ SpecRunner#it at spec/mspec.rb:368
87
+ main.it at spec/mspec.rb:412
88
+ main.__script__ {} at spec/language/unless_spec.rb:48
89
+ Proc#call at kernel/core/proc.rb:127
90
+ SpecRunner#describe at spec/mspec.rb:378
91
+ main.describe at spec/mspec.rb:408
92
+ main.__script__ at spec/language/unless_spec.rb:3
93
+ CompiledMethod#as_script at kernel/bootstrap/primitives.rb:41
94
+ main.load at kernel/core/compile.rb:150
95
+ main.__script__ {} at last_mspec.rb:11
96
+ Array#each {} at kernel/core/array.rb:545
97
+ Integer(Fixnum)#times at kernel/core/integer.rb:15
98
+ Array#each at kernel/core/array.rb:545
99
+ main.__script__ at last_mspec.rb:16
100
+ CompiledMethod#as_script at kernel/bootstrap/primitives.rb:41
101
+ main.load at kernel/core/compile.rb:150
102
+ main.__script__ at kernel/loader.rb:145
103
+ BACKTRACE
104
+
105
+ footer = "\n8 examples, 1 failures\n"
106
+ input = header + failure + backtrace + footer
107
+
108
+ expected_backtrace = backtrace.split("\n").map {|l| "#{l}\n"}
109
+ expected = [[["1)\n", "The unless expression should fail FAILED\n",
110
+ "Expected nil to equal \"baz\":\n",
111
+ *expected_backtrace]],
112
+ ["\n", "8 examples, 1 failures\n"]]
113
+ util_unit_diff(header, input, expected, :parse_input)
114
+ end
115
+
116
+ def test_input_mspec_multiline
117
+ header = <<-HEADER
118
+ Started
119
+ .......F
120
+ Finished in 0.1 seconds
121
+
122
+ HEADER
123
+
124
+ failure = <<-FAILURE
125
+ 1)
126
+ Compiler compiles a case without an argument FAILED
127
+ Expected #<TestGenerator [[:push, :false], [:gif, #<Label 5>], [:push_literal, "foo"], [:string_dup], [:goto, #<Label 19>], [:set_label, #<Label 5>], [:push, :nil], [:gif, #<Label 10>], [:push_literal, "foo"], [:string_dup], [:goto, #<Label 19>], [:set_label, #<Label 10>], [:push, 2], [:push, 1], [:send, :==, 1, false], [:gif, #<Label 17>], [:push_literal, "bar"], [:string_dup], [:goto, #<Label 19>], [:set_label, #<Label 17>], [:push_literal, "baz"], [:string_dup], [:set_label, #<Label 19>]]
128
+ to equal #<TestGenerator [[:push, false], [:gif, #<Label 5>], [:push, "foo"], [:string_dup], [:goto, #<Label 6>], [:set_label, #<Label 5>], [:push, nil], [:set_label, #<Label 6>], [:pop], [:push, nil], [:gif, #<Label 12>], [:push, "foo"], [:string_dup], [:goto, #<Label 13>], [:set_label, #<Label 12>], [:push, nil], [:set_label, #<Label 13>], [:pop], [:push, 2], [:push, 1], [:send, :==, 1], [:gif, #<Label 21>], [:push, "bar"], [:string_dup], [:goto, #<Label 23>], [:set_label, #<Label 21>], [:push_literal, "baz"], [:string_dup], [:set_label, #<Label 23>], [:sret]]:
129
+ FAILURE
130
+
131
+ backtrace = <<-BACKTRACE
132
+ PositiveExpectation#== at spec/mspec.rb:216
133
+ main.gen at ./compiler2/spec/helper.rb:125
134
+ main.__script__ {} at compiler2/spec/control_spec.rb:448
135
+ BACKTRACE
136
+
137
+ footer = "\n8 examples, 1 failures\n"
138
+ input = header + failure + backtrace + footer
139
+
140
+ expected_backtrace = backtrace.split("\n").map {|l| "#{l}\n"}
141
+ expected_failure = failure.split("\n").map {|l| "#{l}\n"}
142
+ expected = [[[*(expected_failure + expected_backtrace)]],
143
+ ["\n", "8 examples, 1 failures\n"]]
144
+ util_unit_diff(header, input, expected, :parse_input)
145
+ end
146
+
147
+ def test_unit_diff_empty # simulates broken pipe at the least
148
+ util_unit_diff("", "", "")
149
+ end
150
+
151
+ def test_parse_diff_angles
152
+ input = [" 1) Failure:\n",
153
+ "test_test1(TestBlah) [./blah.rb:25]:\n",
154
+ "<\"<html>\"> expected but was\n",
155
+ "<\"<body>\">.\n"
156
+ ]
157
+
158
+ expected = [[" 1) Failure:\n", "test_test1(TestBlah) [./blah.rb:25]:\n"],
159
+ ["<html>"],
160
+ ["<body>"],
161
+ []]
162
+
163
+ assert_equal expected, @diff.parse_diff(input)
164
+ end
165
+
166
+ def test_parse_diff_miniunit
167
+ input = [" 1) Failure:\n",
168
+ "test_blah(TestBlah) [./blah.rb:25]:\n",
169
+ "Expected ['a', 'b', 'c'], not ['a', 'c', 'b'].\n"]
170
+
171
+ expected = [[" 1) Failure:\n", "test_blah(TestBlah) [./blah.rb:25]:\n"],
172
+ ["['a', 'b', 'c']"],
173
+ ["['a', 'c', 'b']"],
174
+ []]
175
+
176
+ assert_equal expected, @diff.parse_diff(input)
177
+ end
178
+
179
+ def test_parse_diff_miniunit_multiline
180
+ input = [" 1) Failure:\n",
181
+ "test_blah(TestBlah) [./blah.rb:25]:\n",
182
+ "Expected ['a',\n'b',\n'c'], not ['a',\n'c',\n'b'].\n"]
183
+
184
+ expected = [[" 1) Failure:\n", "test_blah(TestBlah) [./blah.rb:25]:\n"],
185
+ ["['a',\n'b',\n'c']"],
186
+ ["['a',\n'c',\n'b']"],
187
+ []]
188
+
189
+ assert_equal expected, @diff.parse_diff(input)
190
+ end
191
+
192
+ def test_parse_diff1
193
+ input = [" 1) Failure:\n",
194
+ "test_test1(TestBlah) [./blah.rb:25]:\n",
195
+ "<\"line1\\nline2\\nline3\\n\"> expected but was\n",
196
+ "<\"line4\\nline5\\nline6\\n\">.\n"
197
+ ]
198
+
199
+ expected = [[" 1) Failure:\n", "test_test1(TestBlah) [./blah.rb:25]:\n"], ["line1\\nline2\\nline3\\n"], ["line4\\nline5\\nline6\\n"], []]
200
+
201
+ assert_equal expected, @diff.parse_diff(input)
202
+ end
203
+
204
+ def test_parse_diff2
205
+ input = [" 2) Failure:\n",
206
+ "test_test2(TestBlah) [./blah.rb:29]:\n",
207
+ "<\"line1\"> expected but was\n",
208
+ "<\"line2\\nline3\\n\\n\">.\n"
209
+ ]
210
+
211
+ expected = [[" 2) Failure:\n",
212
+ "test_test2(TestBlah) [./blah.rb:29]:\n"],
213
+ ["line1"],
214
+ ["line2\\nline3\\n\\n"],
215
+ []
216
+ ]
217
+
218
+ assert_equal expected, @diff.parse_diff(input)
219
+ end
220
+
221
+ def test_parse_diff3
222
+ input = [" 13) Failure:\n",
223
+ "test_case_stmt(TestRubyToRubyC) [./r2ctestcase.rb:1198]:\n",
224
+ "Unknown expected data.\n",
225
+ "<false> is not true.\n"]
226
+
227
+ expected = [[" 13) Failure:\n", "test_case_stmt(TestRubyToRubyC) [./r2ctestcase.rb:1198]:\n", "Unknown expected data.\n"], ["<false> is not true.\n"], nil, []]
228
+
229
+ assert_equal expected, @diff.parse_diff(input)
230
+ end
231
+
232
+ def test_parse_diff_suspect_equals
233
+ input = ["1) Failure:\n",
234
+ "test_util_capture(AssertionsTest) [test/test_zentest_assertions.rb:53]:\n",
235
+ "<\"out\"> expected but was\n",
236
+ "<\"out\">.\n"]
237
+ expected = [["1) Failure:\n",
238
+ "test_util_capture(AssertionsTest) [test/test_zentest_assertions.rb:53]:\n"],
239
+ ["out"],
240
+ ["out"], []]
241
+
242
+ assert_equal expected, @diff.parse_diff(input)
243
+ end
244
+
245
+ def test_parse_diff_NOT_suspect_equals
246
+ input = ["1) Failure:\n",
247
+ "test_util_capture(AssertionsTest) [test/test_zentest_assertions.rb:53]:\n",
248
+ "<\"out\"> expected but was\n",
249
+ "<\"out\\n\">.\n"]
250
+ expected = [["1) Failure:\n",
251
+ "test_util_capture(AssertionsTest) [test/test_zentest_assertions.rb:53]:\n"],
252
+ ["out"],
253
+ ["out\\n"], []]
254
+
255
+ assert_equal expected, @diff.parse_diff(input)
256
+ end
257
+
258
+ def test_parse_diff_mspec
259
+ input = ["1)\n", "The unless expression should fail FAILED\n",
260
+ "Expected nil to equal \"baz\":\n",
261
+ " PositiveExpectation#== at spec/mspec.rb:217\n"]
262
+
263
+ expected = [["1)\n", "The unless expression should fail FAILED\n"],
264
+ ["nil"],
265
+ ["\"baz\""],
266
+ [" PositiveExpectation#== at spec/mspec.rb:217"]]
267
+
268
+ assert_equal expected, @diff.parse_diff(input)
269
+ end
270
+
271
+ def test_parse_diff_mspec_multiline
272
+ input = ["1)\n", "The unless expression should fail FAILED\n",
273
+ "Expected #<TestGenerator [[:push, :true],\n", " [:dup]\n", "]\n",
274
+ "to equal #<TestGenerator [[:pop],\n", " [:dup]\n", "]:\n",
275
+ " PositiveExpectation#== at spec/mspec.rb:217\n"]
276
+
277
+ expected = [["1)\n", "The unless expression should fail FAILED\n"],
278
+ ["#<TestGenerator [[:push, :true],\n", " [:dup]\n", "]"],
279
+ ["#<TestGenerator [[:pop],\n", " [:dup]\n", "]"],
280
+ [" PositiveExpectation#== at spec/mspec.rb:217"]]
281
+
282
+ assert_equal expected, @diff.parse_diff(input)
283
+ end
284
+
285
+ def test_unit_diff_angles
286
+ header = "Loaded suite ./blah\nStarted\nF\nFinished in 0.035332 seconds.\n\n"
287
+ input = "#{header} 1) Failure:\ntest_test1(TestBlah) [./blah.rb:25]:\n<\"<html>\"> expected but was\n<\"<body>\">.\n\n1 tests, 1 assertions, 1 failures, 0 errors\n"
288
+ expected = "1) Failure:
289
+ test_test1(TestBlah) [./blah.rb:25]:
290
+ --- expected
291
+ +++ actual
292
+ @@ -1 +1 @@
293
+ -<html>
294
+ +<body>
295
+
296
+ 1 tests, 1 assertions, 1 failures, 0 errors"
297
+
298
+ util_unit_diff(header, input, expected)
299
+ end
300
+
301
+ def test_unit_diff1
302
+ header = "Loaded suite ./blah
303
+ Started
304
+ F
305
+ Finished in 0.035332 seconds.
306
+
307
+ "
308
+ input = "#{header} 1) Failure:\ntest_test1(TestBlah) [./blah.rb:25]:\n<\"line1\\nline2\\nline3\\n\"> expected but was\n<\"line4\\nline5\\nline6\\n\">.\n\n1 tests, 1 assertions, 1 failures, 0 errors\n"
309
+ expected = "1) Failure:\ntest_test1(TestBlah) [./blah.rb:25]:\n--- expected\n+++ actual\n@@ -1,4 +1,4 @@\n-line1\n-line2\n-line3\n+line4\n+line5\n+line6\n \n\n1 tests, 1 assertions, 1 failures, 0 errors"
310
+
311
+ util_unit_diff(header, input, expected)
312
+ end
313
+
314
+ def test_unit_diff2
315
+ header = "Loaded suite ./blah\nStarted\nFF\nFinished in 0.035332 seconds.\n\n"
316
+ input = "#{header} 1) Failure:\ntest_test1(TestBlah) [./blah.rb:25]:\n<\"line1\\nline2\\nline3\\n\"> expected but was\n<\"line4\\nline5\\nline6\\n\">.\n\n 2) Failure:\ntest_test2(TestBlah) [./blah.rb:29]:\n<\"line1\"> expected but was\n<\"line2\\nline3\\n\\n\">.\n\n2 tests, 2 assertions, 2 failures, 0 errors\n"
317
+ expected = "1) Failure:\ntest_test1(TestBlah) [./blah.rb:25]:\n--- expected\n+++ actual\n@@ -1,4 +1,4 @@\n-line1\n-line2\n-line3\n+line4\n+line5\n+line6\n \n\n2) Failure:\ntest_test2(TestBlah) [./blah.rb:29]:\n--- expected\n+++ actual\n@@ -1 +1,4 @@\n-line1\n+line2\n+line3\n+\n+\n\n2 tests, 2 assertions, 2 failures, 0 errors"
318
+
319
+ util_unit_diff(header, input, expected)
320
+ end
321
+
322
+ def test_unit_diff3
323
+ header = ""
324
+ input = " 13) Failure:\ntest_case_stmt(TestRubyToRubyC) [./r2ctestcase.rb:1198]:\nUnknown expected data.\n<false> is not true.\n"
325
+ expected = "13) Failure:\ntest_case_stmt(TestRubyToRubyC) [./r2ctestcase.rb:1198]:\nUnknown expected data.\n<false> is not true."
326
+
327
+ util_unit_diff(header, input, expected)
328
+ end
329
+
330
+ def test_unit_diff_suspect_equals
331
+ header = "Loaded suite ./blah\nStarted\n.............................................F............................................\nFinished in 0.834671 seconds.\n\n"
332
+ footer = "90 tests, 241 assertions, 1 failures, 0 errors"
333
+ input = "#{header} 1) Failure:\ntest_unit_diff_suspect_equals(TestUnitDiff) [./test/test_unit_diff.rb:122]:\n<\"out\"> expected but was\n<\"out\">.\n\n#{footer}"
334
+ expected = "1) Failure:\ntest_unit_diff_suspect_equals(TestUnitDiff) [./test/test_unit_diff.rb:122]:\n[no difference--suspect ==]\n\n#{footer}"
335
+
336
+ util_unit_diff(header, input, expected)
337
+ end
338
+
339
+ def test_unit_diff_NOT_suspect_equals
340
+ header = "Loaded suite ./blah
341
+ Started
342
+ .
343
+ Finished in 0.0 seconds.
344
+
345
+ "
346
+ input = "#{header} 1) Failure:
347
+ test_blah(TestBlah)
348
+ <\"out\"> expected but was
349
+ <\"out\\n\">.
350
+
351
+ 1 tests, 1 assertions, 1 failures, 0 errors"
352
+ expected = "1) Failure:
353
+ test_blah(TestBlah)
354
+ --- expected
355
+ +++ actual
356
+ @@ -1 +1,2 @@
357
+ out
358
+ +
359
+
360
+ 1 tests, 1 assertions, 1 failures, 0 errors"
361
+
362
+ util_unit_diff(header, input, expected)
363
+ end
364
+
365
+ def util_unit_diff(header, input, expected, msg=:unit_diff)
366
+ output = StringIO.new("")
367
+ actual = @diff.send(msg, StringIO.new(input), output)
368
+ assert_equal header, output.string, "header output"
369
+ assert_equal expected, actual
370
+ end
371
+ end
372
+
@@ -0,0 +1,566 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ abort "rubinius does not support features required by zentest" if
4
+ defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/
5
+
6
+ $TESTING = true
7
+
8
+ require 'rubygems'
9
+ require 'minitest/autorun'
10
+
11
+ # I do this so I can still run ZenTest against the tests and itself...
12
+ require 'zentest' unless defined? $ZENTEST
13
+
14
+ # These are just classes set up for quick testing.
15
+ # TODO: need to test a compound class name Mod::Cls
16
+
17
+ class Cls1 # ZenTest SKIP
18
+ def meth1; end
19
+ def self.meth2; end
20
+ end
21
+
22
+ class TestCls1 # ZenTest SKIP
23
+ def setup; end
24
+ def teardown; end
25
+ def test_meth1; end
26
+ def test_meth2; assert(true, "something"); end
27
+ end
28
+
29
+ class SuperDuper # ZenTest SKIP
30
+ def self.cls_inherited; end
31
+ def inherited; end
32
+ def overridden; end
33
+ end
34
+
35
+ class LowlyOne < SuperDuper # ZenTest SKIP
36
+ def self.cls_extended; end
37
+ def overridden; end
38
+ def extended; end
39
+ def pretty_print; end
40
+ def pretty_print_cycle; end
41
+ end
42
+
43
+ # This is the good case where there are no missing methods on either side.
44
+
45
+ class Blah0
46
+ def missingtest; end
47
+ def notmissing1; end
48
+ def notmissing2; end
49
+
50
+ # found by zentest on testcase1.rb
51
+ def missingimpl; end
52
+ end
53
+
54
+ class TestBlah0
55
+ def setup; end
56
+ def teardown; end
57
+
58
+ def test_notmissing1
59
+ assert(true, "a test")
60
+ end
61
+ def test_notmissing2_ext1
62
+ assert(true, "a test")
63
+ end
64
+ def test_notmissing2_ext2
65
+ flunk("a failed test")
66
+ end
67
+ def test_missingimpl; end
68
+ def test_missingtest; end
69
+ end
70
+
71
+ class Blah1
72
+ def missingtest; end
73
+ def notmissing1; end
74
+ def notmissing2; end
75
+ end
76
+
77
+ class TestBlah1
78
+ def test_notmissing1; end
79
+ def test_notmissing2_ext1; end
80
+ def test_notmissing2_ext2; end
81
+ def test_missingimpl; Blah1.new.missingimpl; end
82
+ def test_integration_blah1; end
83
+ def test_integration_blah2; end
84
+ def test_integration_blah3; end
85
+ end
86
+
87
+ module Something2
88
+ class Blah2
89
+ def missingtest; end
90
+ def notmissing1; end
91
+ def notmissing2; end
92
+ end
93
+ end
94
+
95
+ module TestSomething2
96
+ class TestBlah2
97
+ def test_notmissing1; end
98
+ def test_notmissing2_ext1; end
99
+ def test_notmissing2_ext2; end
100
+ def test_missingimpl; end
101
+ end
102
+ end
103
+
104
+ # only test classes
105
+ class TestBlah3
106
+ def test_missingimpl; end
107
+ end
108
+ # only regular classes
109
+ class Blah4
110
+ def missingtest1; end
111
+ def missingtest2; end
112
+ end
113
+
114
+ # subclassing a builtin class
115
+ class MyHash5 < Hash
116
+ def []; end
117
+ def missingtest1; end
118
+ end
119
+
120
+ # nested class
121
+ module MyModule6
122
+ class MyClass6
123
+ def []; end
124
+ def missingtest1; end
125
+ end
126
+ end
127
+
128
+ # nested class
129
+ module MyModule7; end # in 1.9+ you'll not need this
130
+ class MyModule7::MyClass7
131
+ def []; end
132
+ def missingtest1; end
133
+ end
134
+
135
+ class MyClass8
136
+ def self.foobar; end
137
+ def MyClass8.foobaz; end
138
+ end
139
+
140
+ class TestTrueClass; end
141
+
142
+ class TestZenTest < MiniTest::Unit::TestCase
143
+ def setup
144
+ @tester = ZenTest.new()
145
+ end
146
+
147
+ ############################################################
148
+ # Utility Methods
149
+
150
+ def util_simple_setup
151
+ @tester.klasses = {
152
+ "Something" =>
153
+ {
154
+ "method1" => true,
155
+ "method1!" => true,
156
+ "method1=" => true,
157
+ "method1?" => true,
158
+ "attrib" => true,
159
+ "attrib=" => true,
160
+ "equal?" => true,
161
+ "self.method3" => true,
162
+ "self.[]" => true,
163
+ },
164
+ }
165
+ @tester.test_klasses = {
166
+ "TestSomething" =>
167
+ {
168
+ "test_class_method4" => true,
169
+ "test_method2" => true,
170
+ "setup" => true,
171
+ "teardown" => true,
172
+ "test_class_index" => true,
173
+ },
174
+ }
175
+ @tester.inherited_methods = @tester.test_klasses.merge(@tester.klasses)
176
+ @generated_code = "
177
+ require 'test/unit/testcase'
178
+ require 'test/unit' if $0 == __FILE__
179
+
180
+ class Something
181
+ def self.method4(*args)
182
+ raise NotImplementedError, 'Need to write self.method4'
183
+ end
184
+
185
+ def method2(*args)
186
+ raise NotImplementedError, 'Need to write method2'
187
+ end
188
+ end
189
+
190
+ class TestSomething < Test::Unit::TestCase
191
+ def test_class_method3
192
+ raise NotImplementedError, 'Need to write test_class_method3'
193
+ end
194
+
195
+ def test_attrib
196
+ raise NotImplementedError, 'Need to write test_attrib'
197
+ end
198
+
199
+ def test_attrib_equals
200
+ raise NotImplementedError, 'Need to write test_attrib_equals'
201
+ end
202
+
203
+ def test_equal_eh
204
+ raise NotImplementedError, 'Need to write test_equal_eh'
205
+ end
206
+
207
+ def test_method1
208
+ raise NotImplementedError, 'Need to write test_method1'
209
+ end
210
+
211
+ def test_method1_bang
212
+ raise NotImplementedError, 'Need to write test_method1_bang'
213
+ end
214
+
215
+ def test_method1_eh
216
+ raise NotImplementedError, 'Need to write test_method1_eh'
217
+ end
218
+
219
+ def test_method1_equals
220
+ raise NotImplementedError, 'Need to write test_method1_equals'
221
+ end
222
+ end
223
+
224
+ # Number of errors detected: 10
225
+ "
226
+ end
227
+
228
+ ############################################################
229
+ # Accessors & Adders:
230
+
231
+ def test_initialize
232
+ refute_nil(@tester, "Tester must be initialized")
233
+ # TODO: should do more at this stage
234
+ end
235
+
236
+ ############################################################
237
+ # Converters and Testers:
238
+
239
+ def test_is_test_class
240
+ # classes
241
+ assert(@tester.is_test_class(TestCls1),
242
+ "All test classes must start with Test")
243
+ assert(!@tester.is_test_class(Cls1),
244
+ "Classes not starting with Test must not be test classes")
245
+ # strings
246
+ assert(@tester.is_test_class("TestCls1"),
247
+ "All test classes must start with Test")
248
+ assert(@tester.is_test_class("TestMod::TestCls1"),
249
+ "All test modules must start with test as well")
250
+ assert(!@tester.is_test_class("Cls1"),
251
+ "Classes not starting with Test must not be test classes")
252
+ assert(!@tester.is_test_class("NotTestMod::TestCls1"),
253
+ "Modules not starting with Test must not be test classes")
254
+ assert(!@tester.is_test_class("NotTestMod::NotTestCls1"),
255
+ "All names must start with Test to be test classes")
256
+ end
257
+
258
+ def test_is_test_class_reversed
259
+ old = $r
260
+ $r = true
261
+ assert(@tester.is_test_class("Cls1Test"),
262
+ "Reversed: All test classes must end with Test")
263
+ assert(@tester.is_test_class("ModTest::Cls1Test"),
264
+ "Reversed: All test classes must end with Test")
265
+ assert(!@tester.is_test_class("TestMod::TestCls1"),
266
+ "Reversed: All test classes must end with Test")
267
+ $r = old
268
+ end
269
+
270
+ def test_convert_class_name
271
+
272
+ assert_equal('Cls1', @tester.convert_class_name(TestCls1))
273
+ assert_equal('TestCls1', @tester.convert_class_name(Cls1))
274
+
275
+ assert_equal('Cls1', @tester.convert_class_name('TestCls1'))
276
+ assert_equal('TestCls1', @tester.convert_class_name('Cls1'))
277
+
278
+ assert_equal('TestModule::TestCls1',
279
+ @tester.convert_class_name('Module::Cls1'))
280
+ assert_equal('Module::Cls1',
281
+ @tester.convert_class_name('TestModule::TestCls1'))
282
+ end
283
+
284
+ def test_convert_class_name_reversed
285
+ old = $r
286
+ $r = true
287
+
288
+ assert_equal('Cls1', @tester.convert_class_name("Cls1Test"))
289
+ assert_equal('Cls1Test', @tester.convert_class_name(Cls1))
290
+
291
+ assert_equal('Cls1', @tester.convert_class_name('Cls1Test'))
292
+ assert_equal('Cls1Test', @tester.convert_class_name('Cls1'))
293
+
294
+ assert_equal('ModuleTest::Cls1Test',
295
+ @tester.convert_class_name('Module::Cls1'))
296
+ assert_equal('Module::Cls1',
297
+ @tester.convert_class_name('ModuleTest::Cls1Test'))
298
+ $r = old
299
+ end
300
+
301
+ ############################################################
302
+ # Missing Classes and Methods:
303
+
304
+ def test_missing_methods_empty
305
+ missing = @tester.missing_methods
306
+ assert_equal({}, missing)
307
+ end
308
+
309
+ def test_add_missing_method_normal
310
+ @tester.add_missing_method("SomeClass", "some_method")
311
+ missing = @tester.missing_methods
312
+ assert_equal({"SomeClass" => { "some_method" => true } }, missing)
313
+ end
314
+
315
+ def test_add_missing_method_duplicates
316
+ @tester.add_missing_method("SomeClass", "some_method")
317
+ @tester.add_missing_method("SomeClass", "some_method")
318
+ @tester.add_missing_method("SomeClass", "some_method")
319
+ missing = @tester.missing_methods
320
+ assert_equal({"SomeClass" => { "some_method" => true } }, missing)
321
+ end
322
+
323
+ def test_analyze_simple
324
+ self.util_simple_setup
325
+
326
+ @tester.analyze
327
+ missing = @tester.missing_methods
328
+ expected = {
329
+ "Something" => {
330
+ "method2" => true,
331
+ "self.method4" => true,
332
+ },
333
+ "TestSomething" => {
334
+ "test_class_method3" => true,
335
+ "test_attrib" => true,
336
+ "test_attrib_equals" => true,
337
+ "test_equal_eh" => true,
338
+ "test_method1" => true,
339
+ "test_method1_eh"=>true,
340
+ "test_method1_bang"=>true,
341
+ "test_method1_equals"=>true,
342
+ }
343
+ }
344
+ assert_equal(expected, missing)
345
+ end
346
+
347
+ def test_create_method
348
+ list = @tester.create_method(" ", 1, "wobble")
349
+ assert_equal([" def wobble(*args)",
350
+ " raise NotImplementedError, 'Need to write wobble'",
351
+ " end"],list)
352
+ end
353
+
354
+ def test_methods_and_tests
355
+ @tester.process_class("ZenTest")
356
+ @tester.process_class("TestZenTest")
357
+ m,t = @tester.methods_and_tests("ZenTest", "TestZenTest")
358
+ assert(m.include?("methods_and_tests"))
359
+ assert(t.include?("test_methods_and_tests"))
360
+ end
361
+
362
+ def test_generate_code_simple
363
+ self.util_simple_setup
364
+
365
+ @tester.analyze
366
+ str = @tester.generate_code[1..-1].join("\n")
367
+ exp = @generated_code
368
+
369
+ assert_equal(exp, str)
370
+ end
371
+
372
+ def test_get_class_good
373
+ assert_equal(Object, @tester.get_class("Object"))
374
+ end
375
+
376
+ def test_get_class_bad
377
+ assert_nil(@tester.get_class("ZZZObject"))
378
+ end
379
+
380
+ def test_get_inherited_methods_for_subclass
381
+ expect = { "inherited" => true, "overridden" => true }
382
+ result = @tester.get_inherited_methods_for("LowlyOne", false)
383
+
384
+ assert_equal(expect, result)
385
+ end
386
+
387
+ def test_get_inherited_methods_for_subclass_full
388
+ expect = Object.instance_methods + %w( inherited overridden )
389
+ expect.map! { |m| m.to_s }
390
+ result = @tester.get_inherited_methods_for("LowlyOne", true)
391
+
392
+ assert_equal(expect.sort, result.keys.sort)
393
+ end
394
+
395
+ def test_get_inherited_methods_for_superclass
396
+ expect = { }
397
+ result = @tester.get_inherited_methods_for("SuperDuper", false)
398
+
399
+ assert_equal(expect.keys.sort, result.keys.sort)
400
+ end
401
+
402
+ def test_get_inherited_methods_for_superclass_full
403
+ expect = Object.instance_methods.map { |m| m.to_s }
404
+ result = @tester.get_inherited_methods_for("SuperDuper", true)
405
+
406
+ assert_equal(expect.sort, result.keys.sort)
407
+ end
408
+
409
+ def test_get_methods_for_subclass
410
+ expect = {
411
+ "self.cls_extended" => true,
412
+ "overridden" => true,
413
+ "extended" => true
414
+ }
415
+ result = @tester.get_methods_for("LowlyOne")
416
+
417
+ assert_equal(expect, result)
418
+ end
419
+
420
+ def test_get_methods_for_subclass_full
421
+ expect = {
422
+ "self.cls_inherited" => true,
423
+ "self.cls_extended" => true,
424
+ "overridden" => true,
425
+ "extended" => true
426
+ }
427
+ result = @tester.get_methods_for("LowlyOne", true)
428
+
429
+ assert_equal(expect, result)
430
+ end
431
+
432
+ def test_get_methods_for_superclass
433
+ expect = {
434
+ "self.cls_inherited" => true,
435
+ "overridden" => true,
436
+ "inherited" => true }
437
+ result = @tester.get_methods_for("SuperDuper")
438
+
439
+ assert_equal(expect, result)
440
+ end
441
+
442
+ def test_result
443
+ self.util_simple_setup
444
+
445
+ @tester.analyze
446
+ @tester.generate_code
447
+ str = @tester.result.split($/, 2).last
448
+ exp = @generated_code
449
+
450
+ assert_equal(exp, str)
451
+ end
452
+
453
+ def test_load_file
454
+ # HACK raise NotImplementedError, 'Need to write test_load_file'
455
+ end
456
+
457
+ def test_scan_files
458
+ # HACK raise NotImplementedError, 'Need to write test_scan_files'
459
+ end
460
+
461
+ def test_process_class
462
+ assert_equal({}, @tester.klasses)
463
+ assert_equal({}, @tester.test_klasses)
464
+ assert_equal({}, @tester.inherited_methods["SuperDuper"])
465
+ @tester.process_class("SuperDuper")
466
+ assert_equal({"SuperDuper"=> {
467
+ "self.cls_inherited"=>true,
468
+ "inherited"=>true,
469
+ "overridden"=>true}},
470
+ @tester.klasses)
471
+ assert_equal({}, @tester.test_klasses)
472
+ assert_equal({}, @tester.inherited_methods["SuperDuper"])
473
+ end
474
+
475
+ def test_klasses_equals
476
+ self.util_simple_setup
477
+ assert_equal({"Something"=> {
478
+ "self.method3"=>true,
479
+ "equal?"=>true,
480
+ "attrib="=>true,
481
+ "self.[]"=>true,
482
+ "method1"=>true,
483
+ "method1="=>true,
484
+ "method1?"=>true,
485
+ "method1!"=>true,
486
+ "method1"=>true,
487
+ "attrib"=>true}}, @tester.klasses)
488
+ @tester.klasses= {"whoopie" => {}}
489
+ assert_equal({"whoopie"=> {}}, @tester.klasses)
490
+ end
491
+
492
+ # REFACTOR: this should probably be cleaned up and on ZenTest side
493
+ def util_testcase(*klasses)
494
+ zentest = ZenTest.new
495
+ klasses.each do |klass|
496
+ zentest.process_class(klass)
497
+ end
498
+ zentest.analyze
499
+ zentest.generate_code
500
+ return zentest.result.split("\n")[1..-1].join("\n")
501
+ end
502
+
503
+ def test_testcase0
504
+ expected = '# Number of errors detected: 0'
505
+ assert_equal expected, util_testcase("Blah0", "TestBlah0")
506
+ end
507
+
508
+ HEADER = "\nrequire 'test/unit/testcase'\nrequire 'test/unit' if $0 == __FILE__\n\n"
509
+
510
+ def test_testcase1
511
+ expected = "#{HEADER}class Blah1\n def missingimpl(*args)\n raise NotImplementedError, 'Need to write missingimpl'\n end\nend\n\nclass TestBlah1 < Test::Unit::TestCase\n def test_missingtest\n raise NotImplementedError, 'Need to write test_missingtest'\n end\nend\n\n# Number of errors detected: 2"
512
+
513
+ assert_equal expected, util_testcase("Blah1", "TestBlah1")
514
+ end
515
+
516
+ def test_testcase2
517
+ expected = "#{HEADER}class Something2::Blah2\n def missingimpl(*args)\n raise NotImplementedError, 'Need to write missingimpl'\n end\nend\n\nclass TestSomething2::TestBlah2 < Test::Unit::TestCase\n def test_missingtest\n raise NotImplementedError, 'Need to write test_missingtest'\n end\nend\n\n# Number of errors detected: 2"
518
+
519
+ assert_equal expected, util_testcase("Something2::Blah2", "TestSomething2::TestBlah2")
520
+ end
521
+
522
+ def test_testcase3
523
+ expected = "#{HEADER}class Blah3\n def missingimpl(*args)\n raise NotImplementedError, 'Need to write missingimpl'\n end\nend\n\n# Number of errors detected: 1"
524
+
525
+ assert_equal expected, util_testcase("TestBlah3")
526
+ end
527
+
528
+ def test_testcase4
529
+ expected = "#{HEADER}class TestBlah4 < Test::Unit::TestCase\n def test_missingtest1\n raise NotImplementedError, 'Need to write test_missingtest1'\n end\n\n def test_missingtest2\n raise NotImplementedError, 'Need to write test_missingtest2'\n end\nend\n\n# Number of errors detected: 3"
530
+
531
+ assert_equal expected, util_testcase("Blah4")
532
+ end
533
+
534
+ def test_testcase5
535
+ expected = "#{HEADER}class TestMyHash5 < Test::Unit::TestCase\n def test_index\n raise NotImplementedError, 'Need to write test_index'\n end\n\n def test_missingtest1\n raise NotImplementedError, 'Need to write test_missingtest1'\n end\nend\n\n# Number of errors detected: 3"
536
+
537
+ assert_equal expected, util_testcase("MyHash5")
538
+ end
539
+
540
+ def test_testcase6
541
+ expected = "#{HEADER}class TestMyModule6::TestMyClass6 < Test::Unit::TestCase\n def test_index\n raise NotImplementedError, 'Need to write test_index'\n end\n\n def test_missingtest1\n raise NotImplementedError, 'Need to write test_missingtest1'\n end\nend\n\n# Number of errors detected: 3"
542
+
543
+ assert_equal expected, util_testcase("MyModule6::MyClass6")
544
+ end
545
+
546
+ def test_testcase7
547
+ expected = "#{HEADER}class TestMyModule7::TestMyClass7 < Test::Unit::TestCase\n def test_index\n raise NotImplementedError, 'Need to write test_index'\n end\n\n def test_missingtest1\n raise NotImplementedError, 'Need to write test_missingtest1'\n end\nend\n\n# Number of errors detected: 3"
548
+
549
+ assert_equal expected, util_testcase("MyModule7::MyClass7")
550
+ end
551
+
552
+ def test_testcase8
553
+ expected = "#{HEADER}class TestMyClass8 < Test::Unit::TestCase\n def test_class_foobar\n raise NotImplementedError, 'Need to write test_class_foobar'\n end\n\n def test_class_foobaz\n raise NotImplementedError, 'Need to write test_class_foobaz'\n end\nend\n\n# Number of errors detected: 3"
554
+
555
+ assert_equal expected, util_testcase("MyClass8")
556
+ end
557
+
558
+ def test_testcase9
559
+ # stupid YAML is breaking my tests. Enters via Test::Rails. order dependent.
560
+ TrueClass.send :remove_method, :taguri, :taguri=, :to_yaml if defined? YAML
561
+
562
+ expected = "#{HEADER}class TestTrueClass < Test::Unit::TestCase\n def test_and\n raise NotImplementedError, 'Need to write test_and'\n end\n\n def test_carat\n raise NotImplementedError, 'Need to write test_carat'\n end\n\n def test_or\n raise NotImplementedError, 'Need to write test_or'\n end\n\n def test_to_s\n raise NotImplementedError, 'Need to write test_to_s'\n end\nend\n\n# Number of errors detected: 4"
563
+
564
+ assert_equal expected, util_testcase("TestTrueClass")
565
+ end
566
+ end