test-unit-ext 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -150,14 +150,14 @@ module Test
150
150
  end
151
151
  end
152
152
 
153
- alias_method :original_run, :run
153
+ alias_method :run_without_priority_support, :run
154
154
  def run(*args, &block)
155
155
  priority_mode = @@priority_mode
156
156
  if priority_mode
157
157
  @original_tests = @tests
158
158
  apply_priority
159
159
  end
160
- original_run(*args, &block)
160
+ run_without_priority_support(*args, &block)
161
161
  ensure
162
162
  @tests = @original_tests if priority_mode
163
163
  end
@@ -1,3 +1,3 @@
1
1
  module TestUnitExt
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,226 @@
1
+ require "erb"
2
+ require "test/unit/failure"
3
+ require "test/unit/error"
4
+ require "test/unit/testresult"
5
+ require "test/unit/testsuite"
6
+ require "test/unit/autorunner"
7
+
8
+ module Test
9
+ module Unit
10
+ module XMLReportable
11
+ include ERB::Util
12
+
13
+ def to_xml
14
+ <<-XML.gsub(/\s*(\n <result>|\n<\/test_log>)/m, "\\1")
15
+ <test_log>
16
+ <test_case>
17
+ <name>#{h(@test.class.name)}</name>
18
+ <description/>
19
+ </test_case>
20
+ <name>#{h(@test.method_name)}</name>
21
+ <description/>
22
+ #{metadata_xml}
23
+ <result>
24
+ <status>#{h(status_name)}</status>
25
+ <detail>#{h(message)}</detail>
26
+ <elapsed>#{h(elapsed_time)}</elapsed>
27
+ </result>
28
+ #{backtrace_xml}
29
+ </test_log>
30
+ XML
31
+ end
32
+
33
+ private
34
+ def metadata_xml
35
+ return "" if @test.metadata.empty?
36
+ item = @test.metadata.collect do |key, value|
37
+ <<-XML
38
+ <item>
39
+ <name>#{h(key)}</name>
40
+ <value>#{h(value)}</value>
41
+ </item>
42
+ XML
43
+ end
44
+ " <metadata>\n#{item.join()} </metadata>"
45
+ end
46
+
47
+ def backtrace_xml
48
+ entries = backtrace_entries_xml
49
+ if entries.empty?
50
+ ""
51
+ else
52
+ <<-XML
53
+ <backtrace>
54
+ #{entries.join.rstrip}
55
+ </backtrace>
56
+ XML
57
+ end
58
+ end
59
+
60
+ def backtrace_entries_xml
61
+ location.collect do |location|
62
+ file, line, info = location.split(/:(\d+):/)
63
+ <<-XML
64
+ <entry>
65
+ <file>#{h(file)}</file>
66
+ <line>#{h(line)}</line>
67
+ <info>#{h(info.to_s.strip)}</info>
68
+ </entry>
69
+ XML
70
+ end
71
+ end
72
+ end
73
+
74
+ class Success
75
+ include XMLReportable
76
+
77
+ attr_reader :test, :elapsed_time
78
+ def initialize(test, elapsed_time)
79
+ @test = test
80
+ @elapsed_time = elapsed_time
81
+ end
82
+
83
+ def message
84
+ nil
85
+ end
86
+
87
+ def location
88
+ []
89
+ end
90
+
91
+ def test_name
92
+ @test.name
93
+ end
94
+
95
+ def test_case_name
96
+ /\((.*)\)\z/ =~ test_name
97
+ $1
98
+ end
99
+
100
+ def status_name
101
+ "success"
102
+ end
103
+ end
104
+
105
+ class Failure
106
+ include XMLReportable
107
+
108
+ attr_accessor :test, :elapsed_time
109
+
110
+ def status_name
111
+ "failure"
112
+ end
113
+ end
114
+
115
+ class Error
116
+ include XMLReportable
117
+
118
+ attr_accessor :test, :elapsed_time
119
+
120
+ def status_name
121
+ "error"
122
+ end
123
+
124
+ def location
125
+ filter_backtrace(@exception.backtrace)
126
+ end
127
+ end
128
+
129
+ class TestCase
130
+ alias_method(:run_without_success_notify, :run)
131
+ def run(result, &block)
132
+ @_start_time = Time.now
133
+ run_result = run_without_success_notify(result, &block)
134
+ result.add_success(Success.new(self, Time.now - @_start_time)) if passed?
135
+ run_result
136
+ end
137
+
138
+ alias_method(:add_failure_without_test_case_set, :add_failure)
139
+ def add_failure(*args)
140
+ add_failure_without_test_case_set(*args)
141
+ failure = @_result.failures.last
142
+ failure.test = self
143
+ failure.elapsed_time = Time.now - @_start_time
144
+ end
145
+
146
+ alias_method(:add_error_without_test_case_set, :add_error)
147
+ def add_error(*args)
148
+ add_error_without_test_case_set(*args)
149
+ error = @_result.errors.last
150
+ error.test = self
151
+ error.elapsed_time = Time.now - @_start_time
152
+ end
153
+ end
154
+
155
+ class TestResult
156
+ attr_reader :failures, :errors
157
+
158
+ alias_method(:initialize_without_successes, :initialize)
159
+ def initialize
160
+ initialize_without_successes
161
+ @successes = []
162
+ @logs = []
163
+ end
164
+
165
+ def add_success(success)
166
+ @logs << success
167
+ @successes << success
168
+ end
169
+
170
+ alias_method(:add_failure_without_logs_store, :add_failure)
171
+ def add_failure(failure)
172
+ @logs << failure
173
+ add_failure_without_logs_store(failure)
174
+ end
175
+
176
+ alias_method(:add_error_without_logs_store, :add_error)
177
+ def add_error(error)
178
+ @logs << error
179
+ add_error_without_logs_store(error)
180
+ end
181
+
182
+ def to_xml
183
+ return "<test_logs/>" if @logs.empty?
184
+ xml = @logs.collect {|log| log.to_xml.gsub(/^/, " ")}.join
185
+ "<test_logs>\n#{xml}</test_logs>\n"
186
+ end
187
+ end
188
+
189
+ class TestSuite
190
+ attr_reader :result
191
+
192
+ alias_method(:run_without_keep_result, :run)
193
+ def run(result, &block)
194
+ @result = result
195
+ run_without_keep_result(result, &block)
196
+ end
197
+ end
198
+
199
+ class AutoRunner
200
+ alias_method(:options_without_xml_report_support, :options)
201
+ def options
202
+ opt = options_without_xml_report_support
203
+ @xml_report_support_option_added ||= false
204
+ unless @xml_report_support_option_added
205
+ @xml_report_file = nil
206
+ opt.on('--xml-report=FILE',
207
+ "Output test report in XML to FILE.") do |file|
208
+ @xml_report_file = file
209
+ end
210
+ end
211
+ opt
212
+ end
213
+
214
+ alias_method(:run_without_xml_report_support, :run)
215
+ def run
216
+ passed = run_without_xml_report_support
217
+ if @xml_report_file
218
+ File.open(@xml_report_file, "w") do |f|
219
+ f.print(@suite.result.to_xml)
220
+ end
221
+ end
222
+ passed
223
+ end
224
+ end
225
+ end
226
+ end
data/test/test_diff.rb CHANGED
@@ -1,9 +1,10 @@
1
- require 'test-unit-ext'
2
-
3
1
  class TestDiff < Test::Unit::TestCase
4
2
  def test_to_indexes
5
3
  assert_to_indexes({"abc def" => [0, 2], "abc" => [1]},
6
4
  ["abc def", "abc", "abc def"])
5
+
6
+ assert_to_indexes({?a => [0, 3], ?b => [1], ?c => [2], ?d => [4]},
7
+ "abcad")
7
8
  end
8
9
 
9
10
  def test_longest_match
@@ -19,46 +20,221 @@ class TestDiff < Test::Unit::TestCase
19
20
  assert_longest_match([1, 0, 2],
20
21
  %w(q a b x c d), %w(a b y c d f),
21
22
  0, 5, 0, 5)
23
+ assert_longest_match([4, 3, 2],
24
+ %w(q a b x c d), %w(a b y c d f),
25
+ 3, 5, 2, 5)
26
+
27
+ assert_longest_match([1, 0, 2], "qabxcd", "abycdf", 0, 5, 0, 5)
28
+ assert_longest_match([0, 0, 1], "efg", "eg", 0, 2, 0, 1)
29
+ assert_longest_match([2, 1, 1], "efg", "eg", 1, 2, 1, 1)
22
30
  end
23
31
 
24
- def _test_matching_blocks
25
- assert_matching_blocks([[0, 0, 2],
26
- [3, 2, 2],
27
- [5, 4, 0]],
28
- %w(a b x c d), %w(a b c d))
29
- assert_matching_blocks([[1, 0, 2],
30
- [4, 3, 2],
31
- [6, 6, 0]],
32
- %w(q a b x c d), %q(a b y c d f))
32
+ def test_longest_match_with_junk_filter
33
+ assert_longest_match([0, 4, 5], " abcd", "abcd abcd", 0, 4, 0, 8)
34
+ assert_longest_match([1, 0, 4], " abcd", "abcd abcd", 0, 4, 0, 8) do |x|
35
+ x == ' '[0]
36
+ end
37
+ end
38
+
39
+ def test_blocks
40
+ assert_blocks([[0, 0, 2],
41
+ [3, 2, 2],
42
+ [5, 4, 0]],
43
+ %w(a b x c d), %w(a b c d))
44
+ assert_blocks([[1, 0, 2],
45
+ [4, 3, 2],
46
+ [6, 6, 0]],
47
+ %w(q a b x c d), %w(a b y c d f))
48
+
49
+ assert_blocks([[1, 0, 2],
50
+ [4, 3, 2],
51
+ [6, 6, 0]],
52
+ "qabxcd", "abycdf")
53
+ assert_blocks([[0, 0, 1],
54
+ [2, 1, 1],
55
+ [3, 2, 0]],
56
+ "efg", "eg")
33
57
  end
34
58
 
35
59
  def test_operations
60
+ assert_operations([], %w(), %w())
61
+
36
62
  assert_operations([[:delete, 0, 1, 0, 0],
37
63
  [:equal, 1, 3, 0, 2],
38
64
  [:replace, 3, 4, 2, 3],
39
65
  [:equal, 4, 6, 3, 5],
40
66
  [:insert, 6, 6, 5, 6]],
41
67
  %w(q a b x c d), %w(a b y c d f))
68
+
69
+ assert_operations([[:delete, 0, 1, 0, 0],
70
+ [:equal, 1, 3, 0, 2],
71
+ [:replace, 3, 4, 2, 3],
72
+ [:equal, 4, 6, 3, 5],
73
+ [:insert, 6, 6, 5, 6]],
74
+ "qabxcd", "abycdf")
75
+ end
76
+
77
+ def test_grouped_operations
78
+ assert_grouped_operations([[[:equal, 0, 0, 0, 0]]],
79
+ %w(),
80
+ %w())
81
+
82
+ assert_grouped_operations([[[:equal, 0, 3, 0, 3]]],
83
+ %w(a b c),
84
+ %w(a b c))
85
+
86
+ assert_grouped_operations([[[:equal, 0, 1, 0, 1],
87
+ [:replace, 1, 2, 1, 2],
88
+ [:equal, 2, 5, 2, 5]],
89
+ [[:equal, 8, 11, 8, 11],
90
+ [:replace, 11, 12, 11, 12],
91
+ [:equal, 12, 13, 12, 13],
92
+ [:delete, 13, 16, 13, 13],
93
+ [:equal, 16, 17, 13, 14],
94
+ [:replace, 17, 18, 14, 15],
95
+ [:equal, 18, 20, 15, 17]]],
96
+ %w(1 2 3 4 5 6 7 8 9 a b c d e f g h i j k),
97
+ %w(1 i 3 4 5 6 7 8 9 a b cX d h iX j k))
98
+ end
99
+
100
+ def test_ratio
101
+ assert_ratio(0.75, "abcd", "bcde")
102
+ assert_ratio(0.80, "efg", "eg")
103
+ end
104
+
105
+ def test_same_contents_readable_diff
106
+ assert_readable_diff(" aaa", ["aaa"], ["aaa"])
107
+ assert_readable_diff(" aaa\n" \
108
+ " bbb",
109
+ ["aaa", "bbb"], ["aaa", "bbb"])
110
+ end
111
+
112
+ def test_deleted_readable_diff
113
+ assert_readable_diff(" aaa\n" \
114
+ "- bbb",
115
+ ["aaa", "bbb"], ["aaa"])
116
+ assert_readable_diff(" aaa\n" \
117
+ "- bbb\n" \
118
+ "- ccc\n" \
119
+ "- ddd",
120
+ ["aaa", "bbb", "ccc", "ddd"], ["aaa"])
121
+ end
122
+
123
+ def test_inserted_readable_diff
124
+ assert_readable_diff(" aaa\n" \
125
+ "+ bbb\n" \
126
+ "+ ccc\n" \
127
+ "+ ddd",
128
+ ["aaa"], ["aaa", "bbb", "ccc", "ddd"])
129
+ end
130
+
131
+ def test_replace_readable_diff
132
+ assert_readable_diff(" aaa\n" \
133
+ "- bbb\n" \
134
+ "+ BbB\n" \
135
+ " ccc\n" \
136
+ "- ddd\n" \
137
+ "- efg\n" \
138
+ "? -\n" \
139
+ "+ eg",
140
+ ["aaa", "bbb", "ccc", "ddd", "efg"],
141
+ ["aaa", "BbB", "ccc", "eg"])
142
+
143
+ assert_readable_diff("- abcd xyz abc\n" \
144
+ "? -\n" \
145
+ "+ abcd abcd xyz abc\n" \
146
+ "? +++++",
147
+ [" abcd xyz abc"],
148
+ ["abcd abcd xyz abc"])
149
+ end
150
+
151
+ def test_complex_readable_diff
152
+ assert_readable_diff(" aaa\n" \
153
+ "- bbb\n" \
154
+ "- ccc\n" \
155
+ "+ \n" \
156
+ "+ # \n" \
157
+ " ddd",
158
+ ["aaa", "bbb", "ccc", "ddd"],
159
+ ["aaa", "", " # ", "ddd"])
160
+
161
+ assert_readable_diff("- one1\n" \
162
+ "? ^\n" \
163
+ "+ ore1\n" \
164
+ "? ^\n" \
165
+ "- two2\n" \
166
+ "- three3\n" \
167
+ "? - -\n" \
168
+ "+ tree\n" \
169
+ "+ emu",
170
+ ["one1", "two2", "three3"],
171
+ ["ore1", "tree", "emu"])
172
+ end
173
+
174
+ def test_unified_diff
175
+ assert_unified_diff("",
176
+ ["one", "two", "three"],
177
+ ["one", "two", "three"],
178
+ "content 1",
179
+ "content 2")
180
+
181
+ assert_unified_diff("--- Original Sat Jan 26 23:30:50 1991\n" \
182
+ "+++ Current Fri Jun 06 10:20:52 2003\n" \
183
+ "@@ -1,4 +1,4 @@\n" \
184
+ "+zero\n" \
185
+ " one\n" \
186
+ "-two\n" \
187
+ "-three\n" \
188
+ "+tree\n" \
189
+ " four",
190
+ ["one", "two", "three", "four"],
191
+ ["zero", "one", "tree", "four"],
192
+ "Original Sat Jan 26 23:30:50 1991",
193
+ "Current Fri Jun 06 10:20:52 2003",
194
+ :show_context => false)
195
+
196
+ from = File.read(__FILE__).split(/\n/)
197
+ to = from.dup
198
+ target_line = __LINE__
199
+ to[target_line - 1, 1] = []
200
+ context = " def test_unified_diff"
201
+ summary = "@@ -#{target_line - 3},7 +#{target_line - 3},6 @@ #{context}"
202
+ assert_unified_diff((["--- revision 10",
203
+ "+++ revision 11",
204
+ summary] +
205
+ from[target_line - 4, 3].collect {|line| " #{line}"} +
206
+ ["-#{from[target_line - 1]}"] +
207
+ from[target_line, 3].collect {|line| " #{line}"}
208
+ ).join("\n"),
209
+ from, to,
210
+ "revision 10",
211
+ "revision 11")
42
212
  end
43
213
 
44
- def test_same_contents
45
- assert_ndiff(" aaa", ["aaa"], ["aaa"])
46
- assert_ndiff(" aaa\n" \
47
- " bbb",
48
- ["aaa", "bbb"], ["aaa", "bbb"])
214
+ def test_diff_lines
215
+ assert_diff_lines(["- ddd",
216
+ "- efg",
217
+ "? -",
218
+ "+ eg"],
219
+ ["aaa", "bbb", "ccc", "ddd", "efg"],
220
+ ["aaa", "BbB", "ccc", "eg"],
221
+ 3, 5, 3, 4)
49
222
  end
50
223
 
51
- def test_deleted
52
- assert_ndiff(" aaa\n" \
53
- " bbb\n" \
54
- "- bbb",
55
- ["aaa", "bbb"], ["aaa"])
56
- assert_ndiff(" aaa\n" \
57
- " bbb\n" \
58
- "- bbb\n" \
59
- "- ccc\n" \
60
- "- ddd",
61
- ["aaa", "bbb", "ccc", "ddd"], ["aaa"])
224
+ def test_diff_line
225
+ assert_diff_line(["- abcDefghiJkl",
226
+ "? ^ ^ ^",
227
+ "+ abcdefGhijkl",
228
+ "? ^ ^ ^"],
229
+ "abcDefghiJkl",
230
+ "abcdefGhijkl")
231
+
232
+ assert_diff_line(["- bcDefghiJklx",
233
+ "? ^ ^ ^ -",
234
+ "+ abcdefGhijkl",
235
+ "? + ^ ^ ^"],
236
+ "bcDefghiJklx",
237
+ "abcdefGhijkl")
62
238
  end
63
239
 
64
240
  def test_format_diff_point
@@ -70,6 +246,33 @@ class TestDiff < Test::Unit::TestCase
70
246
  "\t\tabcdefGhijkl",
71
247
  " ^ ^ ^ ",
72
248
  "+ ^ ^ ^ ")
249
+ assert_format_diff_point(["- efg",
250
+ "? ^",
251
+ "+ eg"],
252
+ "efg",
253
+ "eg",
254
+ " ^",
255
+ "")
256
+ end
257
+
258
+ def test_interesting_line
259
+ from = ["class X",
260
+ " def find(x=0)",
261
+ " body",
262
+ " end",
263
+ "end"]
264
+ to = ["def xxx",
265
+ " raise 'not call me'",
266
+ "end"]
267
+ assert_interesting_line(" def find(x=0)",
268
+ from, to,
269
+ 2, 1)
270
+ assert_interesting_line("def xxx",
271
+ from, to,
272
+ 2, 0)
273
+ assert_interesting_line("class X",
274
+ from, to,
275
+ 0, 0)
73
276
  end
74
277
 
75
278
  private
@@ -80,15 +283,15 @@ class TestDiff < Test::Unit::TestCase
80
283
 
81
284
  def assert_longest_match(expected, from, to,
82
285
  from_start, from_end,
83
- to_start, to_end)
84
- matcher = Test::Diff::SequenceMatcher.new(from, to)
286
+ to_start, to_end, &junk_predicate)
287
+ matcher = Test::Diff::SequenceMatcher.new(from, to, &junk_predicate)
85
288
  assert_equal(expected, matcher.longest_match(from_start, from_end,
86
289
  to_start, to_end))
87
290
  end
88
291
 
89
- def assert_matching_blocks(expected, from, to)
292
+ def assert_blocks(expected, from, to)
90
293
  matcher = Test::Diff::SequenceMatcher.new(from, to)
91
- assert_equal(expected, matcher.matching_blocks)
294
+ assert_equal(expected, matcher.blocks)
92
295
  end
93
296
 
94
297
  def assert_operations(expected, from, to)
@@ -96,14 +299,52 @@ class TestDiff < Test::Unit::TestCase
96
299
  assert_equal(expected, matcher.operations)
97
300
  end
98
301
 
99
- def assert_ndiff(expected, from, to)
100
- assert_equal(expected, Test::Diff.ndiff(from, to))
302
+ def assert_grouped_operations(expected, from, to)
303
+ matcher = Test::Diff::SequenceMatcher.new(from, to)
304
+ assert_equal(expected, matcher.grouped_operations)
305
+ end
306
+
307
+ def assert_ratio(expected, from, to)
308
+ matcher = Test::Diff::SequenceMatcher.new(from, to)
309
+ assert_in_delta(expected, 0.001, matcher.ratio)
310
+ end
311
+
312
+ def assert_readable_diff(expected, from, to)
313
+ assert_equal(expected, Test::Diff.readable(from.join("\n"), to.join("\n")))
314
+ end
315
+
316
+ def assert_unified_diff(expected, from, to, from_label, to_label, options={})
317
+ options = options.merge(:from_label => from_label,
318
+ :to_label => to_label)
319
+ assert_equal(expected, Test::Diff.unified(from.join("\n"), to.join("\n"),
320
+ options))
321
+ end
322
+
323
+ def assert_diff_lines(expected, from, to,
324
+ from_start, from_end,
325
+ to_start, to_end)
326
+ differ = Test::Diff::ReadableDiffer.new(from, to)
327
+ assert_equal(expected, differ.send(:diff_lines,
328
+ from_start, from_end,
329
+ to_start, to_end))
330
+ end
331
+
332
+ def assert_diff_line(expected, from_line, to_line)
333
+ differ = Test::Diff::ReadableDiffer.new([""], [""])
334
+ assert_equal(expected, differ.send(:diff_line, from_line, to_line))
101
335
  end
102
336
 
103
337
  def assert_format_diff_point(expected, from_line, to_line, from_tags, to_tags)
104
- differ = Test::Diff::Differ.new([""], [""])
338
+ differ = Test::Diff::ReadableDiffer.new([""], [""])
105
339
  assert_equal(expected, differ.send(:format_diff_point,
106
340
  from_line, to_line,
107
341
  from_tags, to_tags))
108
342
  end
343
+
344
+ def assert_interesting_line(expected, from, to, from_start, to_start)
345
+ differ = Test::Diff::UnifiedDiffer.new(from, to)
346
+ assert_equal(expected, differ.send(:find_interesting_line,
347
+ from_start, to_start,
348
+ :define_line?))
349
+ end
109
350
  end