test-unit 2.0.0 → 2.0.1

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.
Files changed (39) hide show
  1. data/History.txt +30 -0
  2. data/Manifest.txt +20 -12
  3. data/README.txt +28 -12
  4. data/Rakefile +5 -0
  5. data/TODO +1 -0
  6. data/html/classic.html +15 -0
  7. data/html/index.html +25 -0
  8. data/html/index.html.ja +27 -0
  9. data/html/test-unit-classic.png +0 -0
  10. data/lib/test/unit.rb +62 -0
  11. data/lib/test/unit/assertions.rb +350 -66
  12. data/lib/test/unit/autorunner.rb +68 -13
  13. data/lib/test/unit/collector/load.rb +1 -1
  14. data/lib/test/unit/color-scheme.rb +85 -0
  15. data/lib/test/unit/color.rb +40 -5
  16. data/lib/test/unit/diff.rb +14 -0
  17. data/lib/test/unit/fixture.rb +2 -2
  18. data/lib/test/unit/runner/console.rb +8 -2
  19. data/lib/test/unit/testcase.rb +86 -2
  20. data/lib/test/unit/ui/console/testrunner.rb +51 -26
  21. data/lib/test/unit/version.rb +1 -1
  22. data/sample/test_user.rb +22 -0
  23. data/test/collector/{test_descendant.rb → test-descendant.rb} +0 -0
  24. data/test/collector/{test_load.rb → test-load.rb} +1 -1
  25. data/test/{test_attribute.rb → test-attribute.rb} +0 -0
  26. data/test/test-color-scheme.rb +55 -0
  27. data/test/{test_color.rb → test-color.rb} +10 -0
  28. data/test/{test_diff.rb → test-diff.rb} +0 -0
  29. data/test/{test_emacs_runner.rb → test-emacs-runner.rb} +0 -0
  30. data/test/test-fixture.rb +287 -0
  31. data/test/{test_notification.rb → test-notification.rb} +4 -4
  32. data/test/{test_omission.rb → test-omission.rb} +6 -6
  33. data/test/{test_pending.rb → test-pending.rb} +6 -6
  34. data/test/{test_priority.rb → test-priority.rb} +0 -0
  35. data/test/test_assertions.rb +366 -63
  36. data/test/test_testcase.rb +48 -0
  37. data/test/{testunit_test_util.rb → testunit-test-util.rb} +1 -1
  38. metadata +27 -29
  39. data/test/test_fixture.rb +0 -275
@@ -4,7 +4,7 @@
4
4
  # Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
5
5
  # License:: Ruby license.
6
6
 
7
- require 'test/unit/color'
7
+ require 'test/unit/color-scheme'
8
8
  require 'test/unit/ui/testrunner'
9
9
  require 'test/unit/ui/testrunnermediator'
10
10
  require 'test/unit/ui/console/outputlevel'
@@ -18,17 +18,6 @@ module Test
18
18
  class TestRunner < UI::TestRunner
19
19
  include OutputLevel
20
20
 
21
- COLOR_SCHEMES = {
22
- :default => {
23
- "success" => Color.new("green", :bold => true),
24
- "failure" => Color.new("red", :bold => true),
25
- "pending" => Color.new("magenta", :bold => true),
26
- "omission" => Color.new("blue", :bold => true),
27
- "notification" => Color.new("cyan", :bold => true),
28
- "error" => Color.new("yellow", :bold => true),
29
- },
30
- }
31
-
32
21
  # Creates a new TestRunner for running the passed
33
22
  # suite. If quiet_mode is true, the output while
34
23
  # running is limited to progress dots, errors and
@@ -41,8 +30,11 @@ module Test
41
30
  @output = @options[:output] || STDOUT
42
31
  @use_color = @options[:use_color]
43
32
  @use_color = guess_color_availability if @use_color.nil?
44
- @color_scheme = COLOR_SCHEMES[:default]
33
+ @color_scheme = @options[:color_scheme] || ColorScheme.default
45
34
  @reset_color = Color.new("reset")
35
+ @progress_row = 0
36
+ @progress_row_max = @options[:progress_row_max]
37
+ @progress_row_max ||= guess_progress_row_max
46
38
  @already_outputted = false
47
39
  @faults = []
48
40
  end
@@ -84,9 +76,7 @@ module Test
84
76
 
85
77
  def add_fault(fault)
86
78
  @faults << fault
87
- output_single(fault.single_character_display,
88
- fault_color(fault),
89
- PROGRESS_ONLY)
79
+ output_progress(fault.single_character_display, fault_color(fault))
90
80
  @already_outputted = true
91
81
  end
92
82
 
@@ -124,7 +114,7 @@ module Test
124
114
 
125
115
  def test_finished(name)
126
116
  unless @already_outputted
127
- output_single(".", @color_scheme["success"], PROGRESS_ONLY)
117
+ output_progress(".", color("success"))
128
118
  end
129
119
  nl(VERBOSE)
130
120
  @already_outputted = false
@@ -141,7 +131,7 @@ module Test
141
131
  end
142
132
 
143
133
  def output_single(something, color=nil, level=NORMAL)
144
- return unless output?(level)
134
+ return false unless output?(level)
145
135
  if @use_color and color
146
136
  something = "%s%s%s" % [color.escape_sequence,
147
137
  something,
@@ -149,31 +139,47 @@ module Test
149
139
  end
150
140
  @output.write(something)
151
141
  @output.flush
142
+ true
152
143
  end
153
-
144
+
145
+ def output_progress(mark, color=nil)
146
+ if output_single(mark, color, PROGRESS_ONLY)
147
+ return unless @progress_row_max > 0
148
+ @progress_row += mark.size
149
+ if @progress_row >= @progress_row_max
150
+ nl unless @output_level == VERBOSE
151
+ @progress_row = 0
152
+ end
153
+ end
154
+ end
155
+
154
156
  def output?(level)
155
157
  level <= @output_level
156
158
  end
157
159
 
160
+ def color(name)
161
+ @color_scheme[name] || ColorScheme.default[name]
162
+ end
163
+
158
164
  def fault_color(fault)
159
- @color_scheme[fault.class.name.split(/::/).last.downcase]
165
+ color(fault.class.name.split(/::/).last.downcase)
160
166
  end
161
167
 
162
168
  def result_color
163
169
  if @result.passed?
164
170
  if @result.pending_count > 0
165
- @color_scheme["pending"]
171
+ color("pending")
166
172
  elsif @result.omission_count > 0
167
- @color_scheme["omission"]
173
+ color("omission")
168
174
  elsif @result.notification_count > 0
169
- @color_scheme["notification"]
175
+ color("notification")
170
176
  else
171
- @color_scheme["success"]
177
+ color("success")
172
178
  end
173
179
  elsif @result.error_count > 0
174
- @color_scheme["error"]
180
+ color("error")
175
181
  elsif @result.failure_count > 0
176
- @color_scheme["failure"]
182
+ color("failure")
177
183
  end
178
184
  end
179
185
 
@@ -184,6 +190,25 @@ module Test
184
190
  return true if ENV["EMACS"] == "t"
185
191
  false
186
192
  end
193
+
194
+ def guess_progress_row_max
195
+ term_width = guess_term_width
196
+ if term_width.zero?
197
+ if ENV["EMACS"] == "t"
198
+ -1
199
+ else
200
+ 79
201
+ end
202
+ else
203
+ term_width
204
+ end
205
+ end
206
+
207
+ def guess_term_width
208
+ Integer(ENV["TERM_WIDTH"] || 0)
209
+ rescue ArgumentError
210
+ 0
211
+ end
187
212
  end
188
213
  end
189
214
  end
@@ -2,6 +2,6 @@
2
2
  # HACK: quick and dirty to get integrated into the new project - ryan
3
3
  module Test
4
4
  module Unit
5
- VERSION = '2.0.0'
5
+ VERSION = '2.0.1'
6
6
  end
7
7
  end
@@ -0,0 +1,22 @@
1
+ # nested test case example.
2
+
3
+ class UserTest < Test::Unit::TestCase
4
+ def setup
5
+ @user = "me"
6
+ end
7
+
8
+ def test_full_name
9
+ assert_equal("me", @user)
10
+ end
11
+
12
+ class ProfileTest < UserTest
13
+ setup
14
+ def setup_profile
15
+ @user += ": profile"
16
+ end
17
+
18
+ def test_has_profile
19
+ assert_match(/: profile/, @user)
20
+ end
21
+ end
22
+ end
@@ -268,7 +268,7 @@ EOT
268
268
  [:test, {:name => "test1_1"}],
269
269
  [:test, {:name => "test1_2"}]]]) do |collector|
270
270
  collector.filter = Proc.new do |test|
271
- not /\Atest1/.match(test.method_name).nil?
271
+ !/\Atest1/.match(test.method_name).nil?
272
272
  end
273
273
  end
274
274
  end
@@ -0,0 +1,55 @@
1
+ class TestUnitColorScheme < Test::Unit::TestCase
2
+ def test_default
3
+ assert_equal({
4
+ "success" => color("green", :bold => true),
5
+ "failure" => color("red", :bold => true),
6
+ "pending" => color("magenta", :bold => true),
7
+ "omission" => color("blue", :bold => true),
8
+ "notification" => color("cyan", :bold => true),
9
+ "error" => color("yellow", :bold => true),
10
+ },
11
+ Test::Unit::ColorScheme.default.to_hash)
12
+ end
13
+
14
+ def test_register
15
+ inverted_scheme_spec = {
16
+ "success" => {:name => "red"},
17
+ "failure" => {:name => "green"},
18
+ }
19
+ Test::Unit::ColorScheme["inverted"] = inverted_scheme_spec
20
+ assert_equal({
21
+ "success" => color("red"),
22
+ "failure" => color("green"),
23
+ },
24
+ Test::Unit::ColorScheme["inverted"].to_hash)
25
+ end
26
+
27
+ def test_new_with_colors
28
+ scheme = Test::Unit::ColorScheme.new(:success => color("blue"),
29
+ "failure" => color("green",
30
+ :underline => true))
31
+ assert_equal({
32
+ "success" => color("blue"),
33
+ "failure" => color("green", :underline => true),
34
+ },
35
+ scheme.to_hash)
36
+ end
37
+
38
+ def test_new_with_spec
39
+ scheme = Test::Unit::ColorScheme.new(:success => {
40
+ :name => "blue",
41
+ :bold => true
42
+ },
43
+ "failure" => {:name => "green"})
44
+ assert_equal({
45
+ "success" => color("blue", :bold => true),
46
+ "failure" => color("green"),
47
+ },
48
+ scheme.to_hash)
49
+ end
50
+
51
+ private
52
+ def color(name, options={})
53
+ Test::Unit::Color.new(name, options)
54
+ end
55
+ end
@@ -20,6 +20,16 @@ class TestUnitColor < Test::Unit::TestCase
20
20
  color("none", :underline => true))
21
21
  end
22
22
 
23
+ def test_equal
24
+ red = color("red")
25
+ red_bold = color("red", :bold => true)
26
+
27
+ assert_operator(red, :==, red)
28
+ assert_not_equal(red, nil)
29
+ assert_equal(red, color("red"))
30
+ assert_not_equal(red, red_bold)
31
+ end
32
+
23
33
  private
24
34
  def color(name, options={})
25
35
  Test::Unit::Color.new(name, options)
File without changes
@@ -0,0 +1,287 @@
1
+ class TestUnitFixture < Test::Unit::TestCase
2
+ module EmptyModule
3
+ end
4
+
5
+ def test_setup_without_option
6
+ expected_setup_calls = [:setup,
7
+ :custom_setup_method0,
8
+ :custom_setup_method1,
9
+ :custom_setup_method3]
10
+ test_case = assert_setup(expected_setup_calls, [])
11
+ assert_inherited_setup(expected_setup_calls, test_case)
12
+
13
+ assert_inherited_setup([:setup], nil)
14
+ assert_called_fixtures(expected_setup_calls, test_case)
15
+ end
16
+
17
+ def test_setup_with_before_option
18
+ expected_setup_calls = [:custom_setup_method3,
19
+ :custom_setup_method0,
20
+ :custom_setup_method1,
21
+ :setup]
22
+ test_case = assert_setup(expected_setup_calls,
23
+ [[{:before => :append}],
24
+ [{:before => :append}],
25
+ [{:before => :prepend}],
26
+ [{:before => :prepend}]])
27
+ assert_inherited_setup(expected_setup_calls, test_case)
28
+
29
+ assert_inherited_setup([:setup], nil)
30
+ assert_called_fixtures(expected_setup_calls, test_case)
31
+ end
32
+
33
+ def test_setup_with_after_option
34
+ expected_setup_calls = [:setup,
35
+ :custom_setup_method3,
36
+ :custom_setup_method0,
37
+ :custom_setup_method1]
38
+ test_case = assert_setup(expected_setup_calls,
39
+ [[{:after => :append}],
40
+ [{:after => :append}],
41
+ [{:after => :prepend}],
42
+ [{:after => :prepend}]])
43
+ assert_inherited_setup(expected_setup_calls, test_case)
44
+
45
+ assert_inherited_setup([:setup], nil)
46
+ assert_called_fixtures(expected_setup_calls, test_case)
47
+ end
48
+
49
+ def test_setup_with_invalid_option
50
+ assert_invalid_setup_option(:unknown => true)
51
+ assert_invalid_setup_option(:before => :unknown)
52
+ assert_invalid_setup_option(:after => :unknown)
53
+ end
54
+
55
+ def test_setup_with_option_to_inherited
56
+ expected_setup_calls = [:setup]
57
+ test_case = assert_setup(expected_setup_calls, nil)
58
+ assert_inherited_setup([:setup,
59
+ :custom_setup_method0,
60
+ :custom_setup_method1,
61
+ :custom_setup_method3],
62
+ test_case,
63
+ [])
64
+
65
+ assert_inherited_setup([:setup], nil)
66
+ assert_called_fixtures(expected_setup_calls, test_case)
67
+ end
68
+
69
+ def test_teardown_without_option
70
+ expected_teardown_calls = [:custom_teardown_method3,
71
+ :custom_teardown_method1,
72
+ :custom_teardown_method0,
73
+ :teardown]
74
+ test_case = assert_teardown(expected_teardown_calls, [])
75
+ assert_inherited_teardown(expected_teardown_calls, test_case)
76
+
77
+ assert_inherited_teardown([:teardown], nil)
78
+ assert_called_fixtures(expected_teardown_calls, test_case)
79
+ end
80
+
81
+ def test_teardown_with_before_option
82
+ expected_teardown_calls = [:custom_teardown_method3,
83
+ :custom_teardown_method0,
84
+ :custom_teardown_method1,
85
+ :teardown]
86
+ test_case = assert_teardown(expected_teardown_calls,
87
+ [[{:before => :append}],
88
+ [{:before => :append}],
89
+ [{:before => :prepend}],
90
+ [{:before => :prepend}]])
91
+ assert_inherited_teardown(expected_teardown_calls, test_case)
92
+
93
+ assert_inherited_teardown([:teardown], nil)
94
+ assert_called_fixtures(expected_teardown_calls, test_case)
95
+ end
96
+
97
+ def test_teardown_with_after_option
98
+ expected_teardown_calls = [:teardown,
99
+ :custom_teardown_method3,
100
+ :custom_teardown_method0,
101
+ :custom_teardown_method1]
102
+ test_case = assert_teardown(expected_teardown_calls,
103
+ [[{:after => :append}],
104
+ [{:after => :append}],
105
+ [{:after => :prepend}],
106
+ [{:after => :prepend}]])
107
+ assert_inherited_teardown(expected_teardown_calls, test_case)
108
+
109
+ assert_inherited_teardown([:teardown], nil)
110
+ assert_called_fixtures(expected_teardown_calls, test_case)
111
+ end
112
+
113
+ def test_teardown_with_invalid_option
114
+ assert_invalid_teardown_option(:unknown => true)
115
+ assert_invalid_teardown_option(:before => :unknown)
116
+ assert_invalid_teardown_option(:after => :unknown)
117
+ end
118
+
119
+ def test_teardown_with_option_to_inherited
120
+ expected_teardown_calls = [:teardown]
121
+ test_case = assert_teardown(expected_teardown_calls, nil)
122
+ assert_inherited_teardown([:custom_teardown_method3,
123
+ :custom_teardown_method1,
124
+ :custom_teardown_method0,
125
+ :teardown],
126
+ test_case, [])
127
+
128
+ assert_inherited_teardown([:teardown], nil)
129
+ assert_called_fixtures(expected_teardown_calls, test_case)
130
+ end
131
+
132
+ private
133
+ def assert_called_fixtures(expected, test_case)
134
+ test = test_case.new("test_nothing")
135
+ test.run(Test::Unit::TestResult.new) {}
136
+ assert_equal(expected, test.called_ids)
137
+ end
138
+
139
+ def assert_setup_customizable(expected, parent, options)
140
+ test_case = Class.new(parent || Test::Unit::TestCase) do
141
+ yield(self, :before) if block_given?
142
+
143
+ def called_ids
144
+ @called_ids ||= []
145
+ end
146
+
147
+ def called(id)
148
+ called_ids << id
149
+ end
150
+
151
+ def setup
152
+ called(:setup)
153
+ end
154
+
155
+ setup(*(options[0] || [])) if options
156
+ def custom_setup_method0
157
+ called(:custom_setup_method0)
158
+ end
159
+
160
+ def custom_setup_method1
161
+ called(:custom_setup_method1)
162
+ end
163
+ setup(*[:custom_setup_method1, *(options[1] || [])]) if options
164
+
165
+ setup(*(options[2] || [])) if options
166
+ def custom_setup_method2
167
+ called(:custom_setup_method2)
168
+ end
169
+ unregister_setup(:custom_setup_method2) if options
170
+
171
+ setup(*(options[3] || [])) if options
172
+ def custom_setup_method3
173
+ called(:custom_setup_method3)
174
+ end
175
+
176
+ def test_nothing
177
+ end
178
+
179
+ yield(self, :after) if block_given?
180
+ end
181
+
182
+ assert_called_fixtures(expected, test_case)
183
+ test_case
184
+ end
185
+
186
+ def assert_setup(expected, options)
187
+ _test_case = assert_setup_customizable(expected, nil, options)
188
+ assert_setup_customizable(expected, nil, options) do |test_case, tag|
189
+ test_case.send(:include, EmptyModule) if tag == :before
190
+ end
191
+ _test_case
192
+ end
193
+
194
+ def assert_inherited_setup(expected, parent, options=nil)
195
+ _test_case = assert_setup_customizable(expected, parent, options)
196
+ assert_setup_customizable(expected, parent, options) do |test_case, tag|
197
+ test_case.send(:include, EmptyModule) if tag == :before
198
+ end
199
+ _test_case
200
+ end
201
+
202
+ def assert_teardown_customizable(expected, parent, options)
203
+ test_case = Class.new(parent || Test::Unit::TestCase) do
204
+ yield(self, :before) if block_given?
205
+
206
+ def called_ids
207
+ @called_ids ||= []
208
+ end
209
+
210
+ def called(id)
211
+ called_ids << id
212
+ end
213
+
214
+ def teardown
215
+ called(:teardown)
216
+ end
217
+
218
+ teardown(*(options[0] || [])) if options
219
+ def custom_teardown_method0
220
+ called(:custom_teardown_method0)
221
+ end
222
+
223
+ def custom_teardown_method1
224
+ called(:custom_teardown_method1)
225
+ end
226
+ teardown(*[:custom_teardown_method1, *(options[1] || [])]) if options
227
+
228
+ teardown(*(options[2] || [])) if options
229
+ def custom_teardown_method2
230
+ called(:custom_teardown_method2)
231
+ end
232
+ unregister_teardown(:custom_teardown_method2) if options
233
+
234
+ teardown(*(options[3] || [])) if options
235
+ def custom_teardown_method3
236
+ called(:custom_teardown_method3)
237
+ end
238
+
239
+ def test_nothing
240
+ end
241
+
242
+ yield(self, :after) if block_given?
243
+ end
244
+
245
+ assert_called_fixtures(expected, test_case)
246
+ test_case
247
+ end
248
+
249
+ def assert_teardown(expected, options)
250
+ assert_teardown_customizable(expected, nil, options)
251
+ assert_teardown_customizable(expected, nil, options) do |test_case, tag|
252
+ test_case.send(:include, EmptyModule) if tag == :before
253
+ end
254
+ end
255
+
256
+ def assert_inherited_teardown(expected, parent, options=nil)
257
+ assert_teardown_customizable(expected, parent, options)
258
+ assert_teardown_customizable(expected, parent, options) do |test_case, tag|
259
+ test_case.send(:include, EmptyModule) if tag == :before
260
+ end
261
+ end
262
+
263
+ def assert_invalid_option(fixture_type, option)
264
+ exception = assert_raise(ArgumentError) do
265
+ Class.new(Test::Unit::TestCase) do
266
+ def test_nothing
267
+ end
268
+
269
+ send(fixture_type, option)
270
+ def fixture
271
+ end
272
+ end
273
+ end
274
+ assert_equal("must be {:before => :prepend}, {:before => :append}, " +
275
+ "{:after => :prepend} or {:after => :append}" +
276
+ ": #{option.inspect}",
277
+ exception.message)
278
+ end
279
+
280
+ def assert_invalid_setup_option(option)
281
+ assert_invalid_option(:setup, option)
282
+ end
283
+
284
+ def assert_invalid_teardown_option(option)
285
+ assert_invalid_option(:teardown, option)
286
+ end
287
+ end