ZenTest 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,179 @@
1
+ $TESTING = true
2
+
3
+ require 'stringio'
4
+ require 'test/unit'
5
+
6
+ require 'autotest'
7
+
8
+ class Autotest
9
+
10
+ attr_accessor :system_responses
11
+ attr_reader :files
12
+
13
+ def system(cmd)
14
+ @system_responses ||= []
15
+ raise 'Out of system responses' if @system_responses.empty?
16
+ return @system_responses.shift
17
+ end
18
+
19
+ attr_accessor :backtick_responses
20
+
21
+ def `(cmd) # ` appeases emacs
22
+ @backtick_responses ||= []
23
+ raise 'Out of backtick responses' if @backtick_responses.empty?
24
+ return @backtick_responses.shift
25
+ end
26
+
27
+ end
28
+
29
+ class TestAutotest < Test::Unit::TestCase
30
+
31
+ def setup
32
+ @photo_file = 'test/data/normal/lib/photo.rb'
33
+ @photo_test_file = 'test/data/normal/test/test_photo.rb'
34
+ @route_test_file = 'test/data/normal/test/test_route.rb'
35
+ @user_test_file = 'test/data/normal/test/test_user.rb'
36
+ @camelcase_test_file = 'test/data/normal/test/test_camelcase.rb'
37
+
38
+ util_touch @photo_file, (Time.now - 60)
39
+ util_touch @photo_test_file, (Time.now - 60)
40
+ util_touch @camelcase_test_file, (Time.now - 60)
41
+
42
+ @at = Autotest.new
43
+ end
44
+
45
+ def test_failed_test_files_not_updated
46
+ klass = 'TestPhoto'
47
+ tests = [@user_test_file, @photo_test_file]
48
+
49
+ @at.updated? @photo_test_file
50
+
51
+ failed_files = @at.failed_test_files klass, tests
52
+
53
+ assert_equal [], failed_files
54
+ end
55
+
56
+ def test_failed_test_files_updated
57
+ klass = 'TestPhoto'
58
+ tests = [@user_test_file, @photo_test_file]
59
+
60
+ @at.updated? @photo_test_file
61
+ util_touch @photo_test_file
62
+
63
+ failed_files = @at.failed_test_files klass, tests
64
+
65
+ assert_equal [@photo_test_file], failed_files
66
+ end
67
+
68
+ def test_failed_test_files_updated_camelcase
69
+ klass = 'TestCamelCase'
70
+ tests = [@camelcase_test_file]
71
+
72
+ @at.updated? @camelcase_test_file
73
+ util_touch @camelcase_test_file
74
+
75
+ failed_files = @at.failed_test_files klass, tests
76
+
77
+ assert_equal [@camelcase_test_file], failed_files
78
+ end
79
+
80
+ def test_failed_test_files_updated_implementation
81
+ klass = 'TestPhoto'
82
+ tests = [@user_test_file, @photo_test_file]
83
+
84
+ @at.updated? @photo_file
85
+ util_touch @photo_file
86
+
87
+ failed_files = @at.failed_test_files klass, tests
88
+
89
+ assert_equal [@photo_test_file], failed_files
90
+ end
91
+
92
+ def test_map_file_names
93
+ @at.files['test/test_autotest.rb'] = Time.at 1
94
+ @at.files['lib/autotest.rb'] = Time.at 1
95
+
96
+ file_names = [
97
+ 'lib/autotest.rb',
98
+ 'lib/auto_test.rb',
99
+ 'test/test_autotest.rb',
100
+ ]
101
+
102
+ expected = [
103
+ [['test/test_autotest.rb']],
104
+ [['test/test_autotest.rb']],
105
+ [['test/test_autotest.rb']],
106
+ ]
107
+
108
+ file_names.each_with_index do |name, i|
109
+ assert_equal expected[i], @at.map_file_names([name]), "test #{i}, #{name}"
110
+ end
111
+ end
112
+
113
+ def test_retest_failed_modified
114
+ failed = [['test_route', 'TestPhoto']]
115
+ tests = [@photo_test_file]
116
+
117
+ @at.system_responses = [true]
118
+
119
+ util_touch @photo_test_file
120
+
121
+ out, err = util_capture do
122
+ @at.retest_failed failed, tests
123
+ end
124
+
125
+ out = out.split $/
126
+
127
+ assert_equal "# Rerunning failures: #{@photo_test_file}", out.shift
128
+ assert_equal "+ ruby -Ilib:test -S testrb -n test_route #{@photo_test_file}", out.shift
129
+
130
+ assert_equal true, @at.system_responses.empty?
131
+ end
132
+
133
+ def test_updated_eh
134
+ assert_equal true, @at.updated?(@photo_test_file), 'Not in @files'
135
+ assert_equal false, @at.updated?(@photo_test_file), 'In @files'
136
+ @at.files[@photo_test_file] = Time.at 1
137
+ util_touch @photo_test_file
138
+ assert_equal true, @at.updated?(@photo_test_file), 'Touched'
139
+ end
140
+
141
+ def test_updated_files
142
+ Dir.chdir 'test/data/normal' do
143
+ @at.updated_files
144
+ end
145
+
146
+ expected = {
147
+ 'lib/photo.rb' => File.stat(@photo_file).mtime,
148
+ 'test/test_photo.rb' => File.stat(@photo_test_file).mtime,
149
+ 'test/test_route.rb' => File.stat(@route_test_file).mtime,
150
+ 'test/test_user.rb' => File.stat(@user_test_file).mtime,
151
+ 'test/test_camelcase.rb' => File.stat(@camelcase_test_file).mtime,
152
+ }
153
+
154
+ assert_equal expected, @at.files
155
+
156
+ util_touch @photo_test_file
157
+
158
+ assert_not_equal expected['test_photo.rb'], @at.files
159
+ end
160
+
161
+ def util_capture
162
+ old_stdout = $stdout
163
+ old_stderr = $stderr
164
+ out = StringIO.new
165
+ err = StringIO.new
166
+ $stdout = out
167
+ $stderr = err
168
+ yield
169
+ return out.string, err.string
170
+ ensure
171
+ $stdout = old_stdout
172
+ $stderr = old_stderr
173
+ end
174
+
175
+ def util_touch(file, t = Time.now)
176
+ File.utime(t, t, file)
177
+ end
178
+ end
179
+
@@ -0,0 +1,55 @@
1
+ require 'test_autotest'
2
+ require 'rails_autotest'
3
+
4
+ class TestRailsAutotest < TestAutotest
5
+
6
+ def setup
7
+ super
8
+ @at = RailsAutotest.new
9
+ end
10
+
11
+ def test_map_file_names
12
+ file_names = [
13
+ './app/helpers/application_helper.rb',
14
+ './test/fixtures/routes.yml',
15
+ './test/unit/photo_test.rb',
16
+ './app/models/photo.rb',
17
+ './app/controllers/application.rb',
18
+ './app/controllers/route_controller.rb',
19
+ './app/views/layouts/default.rhtml',
20
+ './app/views/route/index.rhtml',
21
+ './app/helpers/route_helper.rb',
22
+ ]
23
+
24
+ expected = [
25
+ # ApplicationHelper
26
+ [[], ['test/functional/route_controller_test.rb']],
27
+ # fixture
28
+ [['test/unit/route_test.rb'],
29
+ ['test/functional/route_controller_test.rb']],
30
+ # test
31
+ [['test/unit/photo_test.rb'], []],
32
+ # model
33
+ [['test/unit/photo_test.rb'], []],
34
+ # ApplicationController
35
+ [[], ['test/functional/dummy_controller_test.rb']],
36
+ # controller
37
+ [[], ['test/functional/route_controller_test.rb']],
38
+ # layout
39
+ [[], []],
40
+ # view
41
+ [[], ['test/functional/route_controller_test.rb']],
42
+ # helper
43
+ [[], ['test/functional/route_controller_test.rb']],
44
+ ]
45
+
46
+ Dir.chdir 'test/data/rails' do
47
+ file_names.each_with_index do |name, i|
48
+ assert_equal expected[i], @at.map_file_names([name]),
49
+ "test #{i}, #{name}"
50
+ end
51
+ end
52
+ end
53
+
54
+ end
55
+
@@ -0,0 +1,95 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require 'test/unit'
4
+
5
+ $TESTING = true
6
+
7
+ require 'unit_diff'
8
+
9
+ class TestUnitDiff < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @diff = UnitDiff.new
13
+ end
14
+
15
+ def test_input
16
+ input = "Loaded suite ./blah\nStarted\nFF\nFinished in 0.035332 seconds.\n\n 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"
17
+
18
+ # TODO: I think I'd like a separate footer array as well
19
+ expected = [
20
+ ["Loaded suite ./blah\n", "Started\n", "FF\n", "Finished in 0.035332 seconds.\n"],
21
+ [
22
+ [" 1) Failure:\n", "test_test1(TestBlah) [./blah.rb:25]:\n", "<\"line1\\nline2\\nline3\\n\"> expected but was\n", "<\"line4\\nline5\\nline6\\n\">.\n"],
23
+ [" 2) Failure:\n", "test_test2(TestBlah) [./blah.rb:29]:\n", "<\"line1\"> expected but was\n", "<\"line2\\nline3\\n\\n\">.\n"],
24
+ ],
25
+ ["\n", "2 tests, 2 assertions, 2 failures, 0 errors\n"]
26
+ ]
27
+
28
+ assert_equal expected, @diff.input(input)
29
+ end
30
+
31
+ def test_parse_diff1
32
+ input = [" 1) Failure:\n",
33
+ "test_test1(TestBlah) [./blah.rb:25]:\n",
34
+ "<\"line1\\nline2\\nline3\\n\"> expected but was\n",
35
+ "<\"line4\\nline5\\nline6\\n\">.\n"
36
+ ]
37
+
38
+ expected = [[" 1) Failure:\n", "test_test1(TestBlah) [./blah.rb:25]:\n"], ["line1\\nline2\\nline3\\n"], ["line4\\nline5\\nline6\\n"]]
39
+
40
+ assert_equal expected, @diff.parse_diff(input)
41
+ end
42
+
43
+ def test_parse_diff2
44
+ input = [" 2) Failure:\n",
45
+ "test_test2(TestBlah) [./blah.rb:29]:\n",
46
+ "<\"line1\"> expected but was\n",
47
+ "<\"line2\\nline3\\n\\n\">.\n"
48
+ ]
49
+
50
+ expected = [[" 2) Failure:\n",
51
+ "test_test2(TestBlah) [./blah.rb:29]:\n"],
52
+ ["line1"],
53
+ ["line2\\nline3\\n\\n"]
54
+ ]
55
+
56
+ assert_equal expected, @diff.parse_diff(input)
57
+ end
58
+
59
+ def test_parse_diff3
60
+ input = [" 13) Failure:\n",
61
+ "test_case_stmt(TestRubyToRubyC) [./r2ctestcase.rb:1198]:\n",
62
+ "Unknown expected data.\n",
63
+ "<false> is not true.\n"]
64
+
65
+ expected = [[" 13) Failure:\n", "test_case_stmt(TestRubyToRubyC) [./r2ctestcase.rb:1198]:\n", "Unknown expected data.\n"], ["<false> is not true.\n"], nil]
66
+
67
+ assert_equal expected, @diff.parse_diff(input)
68
+ end
69
+
70
+ def test_unit_diff1
71
+ input = "Loaded suite ./blah\nStarted\nF\nFinished in 0.035332 seconds.\n\n 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"
72
+
73
+ expected = "Loaded suite ./blah\nStarted\nF\nFinished in 0.035332 seconds.\n\n1) Failure:\ntest_test1(TestBlah) [./blah.rb:25]:\n1,3c1,3\n< line1\n< line2\n< line3\n---\n> line4\n> line5\n> line6\n\n1 tests, 1 assertions, 1 failures, 0 errors"
74
+
75
+ assert_equal expected, @diff.unit_diff(input)
76
+ end
77
+
78
+ def test_unit_diff2
79
+ input = "Loaded suite ./blah\nStarted\nFF\nFinished in 0.035332 seconds.\n\n 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"
80
+
81
+ expected = "Loaded suite ./blah\nStarted\nFF\nFinished in 0.035332 seconds.\n\n1) Failure:\ntest_test1(TestBlah) [./blah.rb:25]:\n1,3c1,3\n< line1\n< line2\n< line3\n---\n> line4\n> line5\n> line6\n\n2) Failure:\ntest_test2(TestBlah) [./blah.rb:29]:\n1c1,3\n< line1\n---\n> line2\n> line3\n>\n\n2 tests, 2 assertions, 2 failures, 0 errors"
82
+
83
+ assert_equal expected, @diff.unit_diff(input)
84
+ end
85
+
86
+ def test_unit_diff3
87
+ input = " 13) Failure:\ntest_case_stmt(TestRubyToRubyC) [./r2ctestcase.rb:1198]:\nUnknown expected data.\n<false> is not true.\n"
88
+
89
+ expected = "13) Failure:\ntest_case_stmt(TestRubyToRubyC) [./r2ctestcase.rb:1198]:\nUnknown expected data.\n<false> is not true."
90
+
91
+ assert_equal expected, @diff.unit_diff(input)
92
+ end
93
+
94
+ end
95
+
@@ -0,0 +1,670 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
4
+
5
+ $TESTING = true
6
+
7
+ # I do this so I can still run ZenTest against the tests and itself...
8
+ #if __FILE__ == $0 then
9
+ puts "Requiring ZenTest"
10
+ require 'ZenTest'
11
+ #end
12
+
13
+ # These are just classes set up for quick testing.
14
+ # TODO: need to test a compound class name Mod::Cls
15
+
16
+ class Cls1 # ZenTest SKIP
17
+ def meth1; end
18
+ def self.meth2; end
19
+ end
20
+
21
+ class TestCls1 # ZenTest SKIP
22
+ def setup; end
23
+ def teardown; end
24
+ def test_meth1; end
25
+ def test_meth2; assert(true, "something"); end
26
+ end
27
+
28
+ class SuperDuper # ZenTest SKIP
29
+ def self.cls_inherited; end
30
+ def inherited; end
31
+ def overridden; end
32
+ end
33
+
34
+ class LowlyOne < SuperDuper # ZenTest SKIP
35
+ def self.cls_extended; end
36
+ def overridden; end
37
+ def extended; end
38
+ end
39
+
40
+ # This is the good case where there are no missing methods on either side.
41
+
42
+ class Blah0
43
+ def missingtest; end
44
+ def notmissing1; end
45
+ def notmissing2; end
46
+
47
+ # found by zentest on testcase1.rb
48
+ def missingimpl; end
49
+ end
50
+
51
+ class TestBlah0
52
+ def setup; end
53
+ def teardown; end
54
+
55
+ def test_notmissing1
56
+ assert(true, "a test")
57
+ end
58
+ def test_notmissing2_ext1
59
+ assert(true, "a test")
60
+ end
61
+ def test_notmissing2_ext2
62
+ flunk("a failed test")
63
+ end
64
+ def test_missingimpl; end
65
+ def test_missingtest; end
66
+ end
67
+
68
+ class Blah1
69
+ def missingtest; end
70
+ def notmissing1; end
71
+ def notmissing2; end
72
+ end
73
+
74
+ class TestBlah1
75
+ def test_notmissing1; end
76
+ def test_notmissing2_ext1; end
77
+ def test_notmissing2_ext2; end
78
+ def test_missingimpl; Blah1.new.missingimpl; end
79
+ def test_integration_blah1; end
80
+ def test_integration_blah2; end
81
+ def test_integration_blah3; end
82
+ end
83
+
84
+ module Something2
85
+ class Blah2
86
+ def missingtest; end
87
+ def notmissing1; end
88
+ def notmissing2; end
89
+ end
90
+ end
91
+
92
+ module TestSomething2
93
+ class TestBlah2
94
+ def test_notmissing1; end
95
+ def test_notmissing2_ext1; end
96
+ def test_notmissing2_ext2; end
97
+ def test_missingimpl; end
98
+ end
99
+ end
100
+
101
+ # only test classes
102
+ class TestBlah3
103
+ def test_missingimpl; end
104
+ end
105
+ # only regular classes
106
+ class Blah4
107
+ def missingtest1; end
108
+ def missingtest2; end
109
+ end
110
+
111
+ # subclassing a builtin class
112
+ class MyHash5 < Hash
113
+ def []; end
114
+ def missingtest1; end
115
+ end
116
+
117
+ # nested class
118
+ module MyModule6
119
+ class MyClass6
120
+ def []; end
121
+ def missingtest1; end
122
+ end
123
+ end
124
+
125
+ # nested class
126
+ module MyModule7; end # in 1.9+ you'll not need this
127
+ class MyModule7::MyClass7
128
+ def []; end
129
+ def missingtest1; end
130
+ end
131
+
132
+ class MyClass8
133
+ def self.foobar; end
134
+ def MyClass8.foobaz; end
135
+ end
136
+
137
+ class TestTrueClass
138
+
139
+ end
140
+
141
+ class TestZenTest < Test::Unit::TestCase
142
+
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' unless defined? $ZENTEST and $ZENTEST
178
+
179
+ class Something
180
+ def self.method4(*args)
181
+ raise NotImplementedError, 'Need to write self.method4'
182
+ end
183
+
184
+ def method2(*args)
185
+ raise NotImplementedError, 'Need to write method2'
186
+ end
187
+ end
188
+
189
+ class TestSomething < Test::Unit::TestCase
190
+ def test_class_method3
191
+ raise NotImplementedError, 'Need to write test_class_method3'
192
+ end
193
+
194
+ def test_attrib
195
+ raise NotImplementedError, 'Need to write test_attrib'
196
+ end
197
+
198
+ def test_attrib_equals
199
+ raise NotImplementedError, 'Need to write test_attrib_equals'
200
+ end
201
+
202
+ def test_equal_eh
203
+ raise NotImplementedError, 'Need to write test_equal_eh'
204
+ end
205
+
206
+ def test_method1
207
+ raise NotImplementedError, 'Need to write test_method1'
208
+ end
209
+
210
+ def test_method1_bang
211
+ raise NotImplementedError, 'Need to write test_method1_bang'
212
+ end
213
+
214
+ def test_method1_eh
215
+ raise NotImplementedError, 'Need to write test_method1_eh'
216
+ end
217
+
218
+ def test_method1_equals
219
+ raise NotImplementedError, 'Need to write test_method1_equals'
220
+ end
221
+ end
222
+
223
+ # Number of errors detected: 10
224
+ "
225
+ end
226
+
227
+ ############################################################
228
+ # Accessors & Adders:
229
+
230
+ def test_initialize
231
+ assert_not_nil(@tester, "Tester must be initialized")
232
+ # TODO: should do more at this stage
233
+ end
234
+
235
+ ############################################################
236
+ # Converters and Testers:
237
+
238
+ def test_is_test_class
239
+ # classes
240
+ assert(@tester.is_test_class(TestCls1),
241
+ "All test classes must start with Test")
242
+ assert(!@tester.is_test_class(Cls1),
243
+ "Classes not starting with Test must not be test classes")
244
+ # strings
245
+ assert(@tester.is_test_class("TestCls1"),
246
+ "All test classes must start with Test")
247
+ assert(@tester.is_test_class("TestMod::TestCls1"),
248
+ "All test modules must start with test as well")
249
+ assert(!@tester.is_test_class("Cls1"),
250
+ "Classes not starting with Test must not be test classes")
251
+ assert(!@tester.is_test_class("NotTestMod::TestCls1"),
252
+ "Modules not starting with Test must not be test classes")
253
+ assert(!@tester.is_test_class("NotTestMod::NotTestCls1"),
254
+ "All names must start with Test to be test classes")
255
+ end
256
+
257
+ def test_is_test_class_reversed
258
+ old = $r
259
+ $r = true
260
+ assert(@tester.is_test_class("Cls1Test"),
261
+ "Reversed: All test classes must end with Test")
262
+ assert(@tester.is_test_class("ModTest::Cls1Test"),
263
+ "Reversed: All test classes must end with Test")
264
+ assert(!@tester.is_test_class("TestMod::TestCls1"),
265
+ "Reversed: All test classes must end with Test")
266
+ $r = old
267
+ end
268
+
269
+ def test_convert_class_name
270
+
271
+ assert_equal('Cls1', @tester.convert_class_name(TestCls1))
272
+ assert_equal('TestCls1', @tester.convert_class_name(Cls1))
273
+
274
+ assert_equal('Cls1', @tester.convert_class_name('TestCls1'))
275
+ assert_equal('TestCls1', @tester.convert_class_name('Cls1'))
276
+
277
+ assert_equal('TestModule::TestCls1',
278
+ @tester.convert_class_name('Module::Cls1'))
279
+ assert_equal('Module::Cls1',
280
+ @tester.convert_class_name('TestModule::TestCls1'))
281
+ end
282
+
283
+ def test_convert_class_name_reversed
284
+ old = $r
285
+ $r = true
286
+
287
+ assert_equal('Cls1', @tester.convert_class_name("Cls1Test"))
288
+ assert_equal('Cls1Test', @tester.convert_class_name(Cls1))
289
+
290
+ assert_equal('Cls1', @tester.convert_class_name('Cls1Test'))
291
+ assert_equal('Cls1Test', @tester.convert_class_name('Cls1'))
292
+
293
+ assert_equal('ModuleTest::Cls1Test',
294
+ @tester.convert_class_name('Module::Cls1'))
295
+ assert_equal('Module::Cls1',
296
+ @tester.convert_class_name('ModuleTest::Cls1Test'))
297
+ $r = old
298
+ end
299
+
300
+ ############################################################
301
+ # Missing Classes and Methods:
302
+
303
+ def test_missing_methods_empty
304
+ missing = @tester.missing_methods
305
+ assert_equal({}, missing)
306
+ end
307
+
308
+ def test_add_missing_method_normal
309
+ @tester.add_missing_method("SomeClass", "some_method")
310
+ missing = @tester.missing_methods
311
+ assert_equal({"SomeClass" => { "some_method" => true } }, missing)
312
+ end
313
+
314
+ def test_add_missing_method_duplicates
315
+ @tester.add_missing_method("SomeClass", "some_method")
316
+ @tester.add_missing_method("SomeClass", "some_method")
317
+ @tester.add_missing_method("SomeClass", "some_method")
318
+ missing = @tester.missing_methods
319
+ assert_equal({"SomeClass" => { "some_method" => true } }, missing)
320
+ end
321
+
322
+ def test_analyze_simple
323
+ self.util_simple_setup
324
+
325
+ @tester.analyze
326
+ missing = @tester.missing_methods
327
+ expected = {
328
+ "Something" => {
329
+ "method2" => true,
330
+ "self.method4" => true,
331
+ },
332
+ "TestSomething" => {
333
+ "test_class_method3" => true,
334
+ "test_attrib" => true,
335
+ "test_attrib_equals" => true,
336
+ "test_equal_eh" => true,
337
+ "test_method1" => true,
338
+ "test_method1_eh"=>true,
339
+ "test_method1_bang"=>true,
340
+ "test_method1_equals"=>true,
341
+ }
342
+ }
343
+ assert_equal(expected, missing)
344
+ end
345
+
346
+ def test_generate_code_simple
347
+ self.util_simple_setup
348
+
349
+ @tester.analyze
350
+ str = @tester.generate_code[1..-1].join("\n")
351
+ exp = @generated_code
352
+
353
+ assert_equal(exp, str)
354
+ end
355
+
356
+ def test_get_class_good
357
+ assert_equal(Object, @tester.get_class("Object"))
358
+ end
359
+
360
+ def test_get_class_bad
361
+ assert_nil(@tester.get_class("ZZZObject"))
362
+ end
363
+
364
+ def test_get_inherited_methods_for_subclass
365
+ expect = { "inherited" => true, "overridden" => true }
366
+ result = @tester.get_inherited_methods_for("LowlyOne", false)
367
+
368
+ assert_equal(expect, result)
369
+ end
370
+
371
+ def test_get_inherited_methods_for_subclass_full
372
+ expect = Object.instance_methods + %w( inherited overridden )
373
+ result = @tester.get_inherited_methods_for("LowlyOne", true)
374
+
375
+ assert_equal(expect.sort, result.keys.sort)
376
+ end
377
+
378
+ def test_get_inherited_methods_for_superclass
379
+ expect = { }
380
+ result = @tester.get_inherited_methods_for("SuperDuper", false)
381
+
382
+ assert_equal(expect.keys.sort, result.keys.sort)
383
+ end
384
+
385
+ def test_get_inherited_methods_for_superclass_full
386
+ expect = Object.instance_methods
387
+ result = @tester.get_inherited_methods_for("SuperDuper", true)
388
+
389
+ assert_equal(expect.sort, result.keys.sort)
390
+ end
391
+
392
+ def test_get_methods_for_subclass
393
+ expect = {
394
+ "self.cls_extended" => true,
395
+ "overridden" => true,
396
+ "extended" => true
397
+ }
398
+ result = @tester.get_methods_for("LowlyOne")
399
+
400
+ assert_equal(expect, result)
401
+ end
402
+
403
+ def test_get_methods_for_subclass_full
404
+ expect = {
405
+ "self.cls_inherited" => true,
406
+ "self.cls_extended" => true,
407
+ "overridden" => true,
408
+ "extended" => true
409
+ }
410
+ result = @tester.get_methods_for("LowlyOne", true)
411
+
412
+ assert_equal(expect, result)
413
+ end
414
+
415
+ def test_get_methods_for_superclass
416
+ expect = {
417
+ "self.cls_inherited" => true,
418
+ "overridden" => true,
419
+ "inherited" => true }
420
+ result = @tester.get_methods_for("SuperDuper")
421
+
422
+ assert_equal(expect, result)
423
+ end
424
+
425
+ def test_result
426
+ self.util_simple_setup
427
+
428
+ @tester.analyze
429
+ @tester.generate_code
430
+ str = @tester.result.split($/, 2).last
431
+ exp = @generated_code
432
+
433
+ assert_equal(exp, str)
434
+ end
435
+
436
+ def test_load_file
437
+ # HACK raise NotImplementedError, 'Need to write test_load_file'
438
+ end
439
+
440
+ def test_scan_files
441
+ # HACK raise NotImplementedError, 'Need to write test_scan_files'
442
+ end
443
+
444
+ def test_process_class
445
+ assert_equal({}, @tester.klasses)
446
+ assert_equal({}, @tester.test_klasses)
447
+ assert_equal({}, @tester.inherited_methods["SuperDuper"])
448
+ @tester.process_class("SuperDuper")
449
+ assert_equal({"SuperDuper"=> {
450
+ "self.cls_inherited"=>true,
451
+ "inherited"=>true,
452
+ "overridden"=>true}},
453
+ @tester.klasses)
454
+ assert_equal({}, @tester.test_klasses)
455
+ assert_equal({}, @tester.inherited_methods["SuperDuper"])
456
+ end
457
+
458
+ def test_normal_to_test
459
+ self.util_simple_setup
460
+ assert_equal("test_method1", @tester.normal_to_test("method1"))
461
+ assert_equal("test_method1_bang", @tester.normal_to_test("method1!"))
462
+ assert_equal("test_method1_eh", @tester.normal_to_test("method1?"))
463
+ assert_equal("test_method1_equals", @tester.normal_to_test("method1="))
464
+ end
465
+
466
+ def test_normal_to_test_cls
467
+ self.util_simple_setup
468
+ assert_equal("test_class_method1", @tester.normal_to_test("self.method1"))
469
+ assert_equal("test_class_method1_bang", @tester.normal_to_test("self.method1!"))
470
+ assert_equal("test_class_method1_eh", @tester.normal_to_test("self.method1?"))
471
+ assert_equal("test_class_method1_equals", @tester.normal_to_test("self.method1="))
472
+ end
473
+
474
+ def test_normal_to_test_operators
475
+ self.util_simple_setup
476
+ assert_equal("test_and", @tester.normal_to_test("&"))
477
+ assert_equal("test_bang", @tester.normal_to_test("!"))
478
+ assert_equal("test_carat", @tester.normal_to_test("^"))
479
+ assert_equal("test_div", @tester.normal_to_test("/"))
480
+ assert_equal("test_equalstilde", @tester.normal_to_test("=~"))
481
+ assert_equal("test_minus", @tester.normal_to_test("-"))
482
+ assert_equal("test_or", @tester.normal_to_test("|"))
483
+ assert_equal("test_percent", @tester.normal_to_test("%"))
484
+ assert_equal("test_plus", @tester.normal_to_test("+"))
485
+ assert_equal("test_tilde", @tester.normal_to_test("~"))
486
+ end
487
+
488
+ def test_normal_to_test_overlap
489
+ self.util_simple_setup
490
+ assert_equal("test_equals2", @tester.normal_to_test("=="))
491
+ assert_equal("test_equals3", @tester.normal_to_test("==="))
492
+ assert_equal("test_ge", @tester.normal_to_test(">="))
493
+ assert_equal("test_gt", @tester.normal_to_test(">"))
494
+ assert_equal("test_gt2", @tester.normal_to_test(">>"))
495
+ assert_equal("test_index", @tester.normal_to_test("[]"))
496
+ assert_equal("test_index_equals", @tester.normal_to_test("[]="))
497
+ assert_equal("test_lt", @tester.normal_to_test("<"))
498
+ assert_equal("test_lt2", @tester.normal_to_test("<\<"))
499
+ assert_equal("test_lte", @tester.normal_to_test("<="))
500
+ assert_equal("test_method", @tester.normal_to_test("method"))
501
+ assert_equal("test_method_equals", @tester.normal_to_test("method="))
502
+ assert_equal("test_spaceship", @tester.normal_to_test("<=>"))
503
+ assert_equal("test_times", @tester.normal_to_test("*"))
504
+ assert_equal("test_times2", @tester.normal_to_test("**"))
505
+ assert_equal("test_unary_minus", @tester.normal_to_test("@-"))
506
+ assert_equal("test_unary_plus", @tester.normal_to_test("@+"))
507
+ assert_equal("test_class_index", @tester.normal_to_test("self.[]"))
508
+ end
509
+
510
+ def test_test_to_normal
511
+ self.util_simple_setup
512
+ assert_equal("method1!", @tester.test_to_normal("test_method1_bang", "Something"))
513
+ assert_equal("method1", @tester.test_to_normal("test_method1", "Something"))
514
+ assert_equal("method1=", @tester.test_to_normal("test_method1_equals", "Something"))
515
+ assert_equal("method1?", @tester.test_to_normal("test_method1_eh", "Something"))
516
+ end
517
+
518
+ def test_test_to_normal_cls
519
+ self.util_simple_setup
520
+ assert_equal("self.method1", @tester.test_to_normal("test_class_method1"))
521
+ assert_equal("self.method1!", @tester.test_to_normal("test_class_method1_bang"))
522
+ assert_equal("self.method1?", @tester.test_to_normal("test_class_method1_eh"))
523
+ assert_equal("self.method1=", @tester.test_to_normal("test_class_method1_equals"))
524
+ assert_equal("self.[]", @tester.test_to_normal("test_class_index"))
525
+ end
526
+
527
+ def test_test_to_normal_extended
528
+ self.util_simple_setup
529
+ assert_equal("equal?", @tester.test_to_normal("test_equal_eh_extension", "Something"))
530
+ assert_equal("equal?", @tester.test_to_normal("test_equal_eh_extension_again", "Something"))
531
+ assert_equal("method1", @tester.test_to_normal("test_method1_extension", "Something"))
532
+ assert_equal("method1", @tester.test_to_normal("test_method1_extension_again", "Something"))
533
+ end
534
+
535
+ def test_test_to_normal_mapped
536
+ self.util_simple_setup
537
+ assert_equal("*", @tester.test_to_normal("test_times"))
538
+ assert_equal("*", @tester.test_to_normal("test_times_ext"))
539
+ assert_equal("==", @tester.test_to_normal("test_equals2"))
540
+ assert_equal("==", @tester.test_to_normal("test_equals2_ext"))
541
+ assert_equal("===", @tester.test_to_normal("test_equals3"))
542
+ assert_equal("===", @tester.test_to_normal("test_equals3_ext"))
543
+ assert_equal("[]", @tester.test_to_normal("test_index"))
544
+ assert_equal("[]", @tester.test_to_normal("test_index_ext"))
545
+ assert_equal("[]=", @tester.test_to_normal("test_index_equals"))
546
+ assert_equal("[]=", @tester.test_to_normal("test_index_equals_ext"))
547
+ end
548
+
549
+ def test_test_to_normal_operators
550
+ self.util_simple_setup
551
+ assert_equal("&", @tester.test_to_normal("test_and"))
552
+ assert_equal("!", @tester.test_to_normal("test_bang"))
553
+ assert_equal("^", @tester.test_to_normal("test_carat"))
554
+ assert_equal("/", @tester.test_to_normal("test_div"))
555
+ assert_equal("=~", @tester.test_to_normal("test_equalstilde"))
556
+ assert_equal("-", @tester.test_to_normal("test_minus"))
557
+ assert_equal("|", @tester.test_to_normal("test_or"))
558
+ assert_equal("%", @tester.test_to_normal("test_percent"))
559
+ assert_equal("+", @tester.test_to_normal("test_plus"))
560
+ assert_equal("~", @tester.test_to_normal("test_tilde"))
561
+ end
562
+
563
+ def test_test_to_normal_overlap
564
+ self.util_simple_setup
565
+ assert_equal("==", @tester.test_to_normal("test_equals2"))
566
+ assert_equal("===", @tester.test_to_normal("test_equals3"))
567
+ assert_equal(">=", @tester.test_to_normal("test_ge"))
568
+ assert_equal(">", @tester.test_to_normal("test_gt"))
569
+ assert_equal(">>", @tester.test_to_normal("test_gt2"))
570
+ assert_equal("[]", @tester.test_to_normal("test_index"))
571
+ assert_equal("[]=", @tester.test_to_normal("test_index_equals"))
572
+ assert_equal("<", @tester.test_to_normal("test_lt"))
573
+ assert_equal("<\<", @tester.test_to_normal("test_lt2"))
574
+ assert_equal("<=", @tester.test_to_normal("test_lte"))
575
+ assert_equal("<=>", @tester.test_to_normal("test_spaceship"))
576
+ assert_equal("*", @tester.test_to_normal("test_times"))
577
+ assert_equal("**", @tester.test_to_normal("test_times2"))
578
+ assert_equal("@-", @tester.test_to_normal("test_unary_minus"))
579
+ assert_equal("@+", @tester.test_to_normal("test_unary_plus"))
580
+ end
581
+
582
+ def test_klasses_equals
583
+ self.util_simple_setup
584
+ assert_equal({"Something"=> {
585
+ "self.method3"=>true,
586
+ "equal?"=>true,
587
+ "attrib="=>true,
588
+ "self.[]"=>true,
589
+ "method1"=>true,
590
+ "method1="=>true,
591
+ "method1?"=>true,
592
+ "method1!"=>true,
593
+ "method1"=>true,
594
+ "attrib"=>true}}, @tester.klasses)
595
+ @tester.klasses= {"whoopie" => {}}
596
+ assert_equal({"whoopie"=> {}}, @tester.klasses)
597
+ end
598
+
599
+ # REFACTOR: this should probably be cleaned up and on ZenTest side
600
+ def util_testcase(*klasses)
601
+ zentest = ZenTest.new
602
+ klasses.each do |klass|
603
+ zentest.process_class(klass)
604
+ end
605
+ zentest.analyze
606
+ zentest.generate_code
607
+ return zentest.result.split("\n")[1..-1].join("\n")
608
+ end
609
+
610
+ def test_testcase0
611
+ expected = '# Number of errors detected: 0'
612
+ assert_equal expected, util_testcase("Blah0", "TestBlah0")
613
+ end
614
+
615
+ def test_testcase1
616
+ expected = "\nrequire 'test/unit' unless defined? $ZENTEST and $ZENTEST\n\nclass 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"
617
+
618
+ assert_equal expected, util_testcase("Blah1", "TestBlah1")
619
+ end
620
+
621
+ def test_testcase2
622
+ expected = "\nrequire 'test/unit' unless defined? $ZENTEST and $ZENTEST\n\nmodule Something2\n class Blah2\n def missingimpl(*args)\n raise NotImplementedError, 'Need to write missingimpl'\n end\n end\nend\n\nmodule TestSomething2\n class TestBlah2 < Test::Unit::TestCase\n def test_missingtest\n raise NotImplementedError, 'Need to write test_missingtest'\n end\n end\nend\n\n# Number of errors detected: 2"
623
+
624
+ assert_equal expected, util_testcase("Something2::Blah2", "TestSomething2::TestBlah2")
625
+ end
626
+
627
+ def test_testcase3
628
+ expected = "\nrequire 'test/unit' unless defined? $ZENTEST and $ZENTEST\n\nclass Blah3\n def missingimpl(*args)\n raise NotImplementedError, 'Need to write missingimpl'\n end\nend\n\n# Number of errors detected: 1"
629
+
630
+ assert_equal expected, util_testcase("TestBlah3")
631
+ end
632
+
633
+ def test_testcase4
634
+ expected = "\nrequire 'test/unit' unless defined? $ZENTEST and $ZENTEST\n\nclass 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"
635
+
636
+ assert_equal expected, util_testcase("Blah4")
637
+ end
638
+
639
+ def test_testcase5
640
+ expected = "\nrequire 'test/unit' unless defined? $ZENTEST and $ZENTEST\n\nclass 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"
641
+
642
+ assert_equal expected, util_testcase("MyHash5")
643
+ end
644
+
645
+ def test_testcase6
646
+ expected = "\nrequire 'test/unit' unless defined? $ZENTEST and $ZENTEST\n\nmodule TestMyModule6\n class 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\n end\nend\n\n# Number of errors detected: 3"
647
+
648
+ assert_equal expected, util_testcase("MyModule6::MyClass6")
649
+ end
650
+
651
+ def test_testcase7
652
+ expected = "\nrequire 'test/unit' unless defined? $ZENTEST and $ZENTEST\n\nmodule TestMyModule7\n class 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\n end\nend\n\n# Number of errors detected: 3"
653
+
654
+ assert_equal expected, util_testcase("MyModule7::MyClass7")
655
+ end
656
+
657
+ def test_testcase8
658
+ expected = "\nrequire 'test/unit' unless defined? $ZENTEST and $ZENTEST\n\nclass 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"
659
+
660
+ assert_equal expected, util_testcase("MyClass8")
661
+ end
662
+
663
+ def test_testcase9
664
+ expected = "\nrequire 'test/unit' unless defined? $ZENTEST and $ZENTEST\n\nclass 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"
665
+
666
+ assert_equal expected, util_testcase("TestTrueClass")
667
+ end
668
+
669
+ end
670
+