docdiff 0.6.4 → 0.6.6

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 (47) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +7 -7
  3. data/Guardfile +4 -4
  4. data/Makefile +6 -7
  5. data/README.md +1 -0
  6. data/README_ja.md +1 -0
  7. data/Rakefile +6 -6
  8. data/bin/docdiff +2 -209
  9. data/devutil/Rakefile +12 -5
  10. data/devutil/char_by_charclass.rb +43 -20
  11. data/devutil/charclass_by_char.rb +40 -19
  12. data/devutil/jis0208.rb +263 -231
  13. data/devutil/jis0208_test.rb +196 -0
  14. data/doc/news.md +17 -0
  15. data/docdiff.gemspec +13 -10
  16. data/lib/doc_diff.rb +63 -98
  17. data/lib/docdiff/charstring.rb +225 -241
  18. data/lib/docdiff/cli.rb +316 -0
  19. data/lib/docdiff/diff/contours.rb +1 -1
  20. data/lib/docdiff/diff/editscript.rb +1 -1
  21. data/lib/docdiff/diff/rcsdiff.rb +1 -1
  22. data/lib/docdiff/diff/shortestpath.rb +1 -1
  23. data/lib/docdiff/diff/speculative.rb +1 -1
  24. data/lib/docdiff/diff/subsequence.rb +1 -1
  25. data/lib/docdiff/diff/unidiff.rb +1 -1
  26. data/lib/docdiff/diff.rb +1 -1
  27. data/lib/docdiff/difference.rb +71 -70
  28. data/lib/docdiff/document.rb +129 -109
  29. data/lib/docdiff/encoding/en_ascii.rb +64 -58
  30. data/lib/docdiff/encoding/ja_eucjp.rb +250 -235
  31. data/lib/docdiff/encoding/ja_sjis.rb +240 -226
  32. data/lib/docdiff/encoding/ja_utf8.rb +6952 -6939
  33. data/lib/docdiff/version.rb +1 -1
  34. data/lib/docdiff/view.rb +523 -427
  35. data/lib/docdiff.rb +2 -2
  36. data/test/charstring_test.rb +475 -351
  37. data/test/cli_test.rb +314 -0
  38. data/test/diff_test.rb +15 -16
  39. data/test/difference_test.rb +40 -31
  40. data/test/docdiff_test.rb +162 -159
  41. data/test/document_test.rb +280 -175
  42. data/test/fixture/format_wdiff.conf +1 -0
  43. data/test/fixture/simple.conf +9 -0
  44. data/test/test_helper.rb +2 -1
  45. data/test/view_test.rb +636 -497
  46. metadata +27 -9
  47. data/devutil/testjis0208.rb +0 -38
data/test/cli_test.rb ADDED
@@ -0,0 +1,314 @@
1
+ #!/usr/bin/ruby
2
+ # -*- coding: utf-8; -*-
3
+
4
+ require "test/unit"
5
+ require "nkf"
6
+ require "docdiff/cli"
7
+
8
+ class TestCLI < Test::Unit::TestCase
9
+ def test_parse_options!
10
+ args = [
11
+ "--resolution=line",
12
+ "--char",
13
+ "--encoding=ASCII",
14
+ "--eucjp",
15
+ "--eol=CR",
16
+ "--crlf",
17
+ "--format=manued",
18
+ "--wdiff",
19
+ "--label=old",
20
+ "--label=new",
21
+ "--digest",
22
+ "--display=block",
23
+ "--pager='less --raw-control-chars'",
24
+ "--no-config-file",
25
+ "--config-file=./docdiff.conf",
26
+ "file1",
27
+ "file2",
28
+ ]
29
+ expected = {
30
+ resolution: "char",
31
+ encoding: "EUC-JP",
32
+ eol: "CRLF",
33
+ format: "wdiff",
34
+ digest: true,
35
+ label: ["old", "new"],
36
+ display: "block",
37
+ pager: "'less --raw-control-chars'",
38
+ no_config_file: true,
39
+ config_file: "./docdiff.conf",
40
+ }
41
+ assert_equal(expected, DocDiff::CLI.parse_options!(args, base_options: {}))
42
+ end
43
+
44
+ def test_parse_config_file_content
45
+ content = [
46
+ "# comment line\n",
47
+ " # comment line with leading space\n",
48
+ "foo1 = bar\n",
49
+ "foo2 = bar baz \n",
50
+ " foo3 = 123 # comment\n",
51
+ "foo4 = no \n",
52
+ "foo1 = tRue\n",
53
+ "\n",
54
+ "",
55
+ nil,
56
+ ].join
57
+ expected = { foo1: true, foo2: "bar baz", foo3: 123, foo4: false }
58
+ assert_equal(expected, DocDiff::CLI.parse_config_file_content(content))
59
+ end
60
+
61
+ def test_read_config_from_file
62
+ filename = File.join(File.dirname(__FILE__), "fixture/simple.conf")
63
+ expected = { foo1: true, foo2: "bar baz", foo3: 123, foo4: false }
64
+ config, _message = DocDiff::CLI.read_config_from_file(filename)
65
+ assert_equal(expected, config)
66
+ end
67
+
68
+ def test_read_config_from_file_raises_exception
69
+ assert_raise(Errno::ENOENT) do
70
+ _config, _message = DocDiff::CLI.read_config_from_file("no/such/file")
71
+ end
72
+ end
73
+
74
+ def test_cli_resolution_line
75
+ expected = <<~EOS.chomp
76
+ [-Hello, my name is Watanabe.
77
+ I am just another Ruby porter.
78
+ -]{+Hello, my name is matz.
79
+ It's me who has created Ruby. I am a Ruby hacker.
80
+ +}
81
+ EOS
82
+ cmd = "ruby -I lib bin/docdiff --resolution=line --format=wdiff " \
83
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
84
+ actual = `#{cmd}`
85
+ assert_equal(expected, actual)
86
+ end
87
+
88
+ def test_cli_resolution_word
89
+ expected = <<~EOS
90
+ Hello, my name is [-Watanabe.-]{+matz.+}
91
+ {+It's me who has created Ruby. +}I am [-just another -]{+a +}Ruby [-porter.-]{+hacker.+}
92
+ EOS
93
+ cmd = "ruby -I lib bin/docdiff --resolution=word --format=wdiff " \
94
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
95
+ actual = `#{cmd}`
96
+ assert_equal(expected, actual)
97
+ end
98
+
99
+ def test_cli_resolution_char
100
+ expected = <<~EOS
101
+ Hello, my name is [-W-]{+m+}at[-anabe-]{+z+}.
102
+ {+It's me who has created Ruby. +}I am [-just -]a[-nother-] Ruby [-port-]{+hack+}er.
103
+ EOS
104
+ cmd = "ruby -I lib bin/docdiff --resolution=char --format=wdiff " \
105
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
106
+ actual = `#{cmd}`
107
+ assert_equal(expected, actual)
108
+ end
109
+
110
+ def test_cli_encoding_ascii
111
+ expected = <<~EOS
112
+ Hello, my name is [-Watanabe.-]{+matz.+}
113
+ {+It's me who has created Ruby. +}I am [-just another -]{+a +}Ruby [-porter.-]{+hacker.+}
114
+ EOS
115
+ cmd = "ruby -I lib bin/docdiff --encoding=ASCII --format=wdiff " \
116
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
117
+ actual = `#{cmd}`
118
+ assert_equal(expected, actual)
119
+ end
120
+
121
+ def test_cli_encoding_euc_jp
122
+ expected = NKF.nkf("--ic=UTF-8 --oc=EUC-JP", <<~EOS)
123
+ [-こんにちは-]{+こんばんは+}、私の[-名前はわたなべです-]{+名前はまつもとです+}。
124
+ {+Rubyを作ったのは私です。+}私は[-Just Another -]Ruby [-Porter-]{+Hacker+}です。
125
+ EOS
126
+ cmd = "ruby --external-encoding EUC-JP -I lib bin/docdiff --encoding=EUC-JP --format=wdiff " \
127
+ "test/fixture/01_ja_eucjp_lf.txt test/fixture/02_ja_eucjp_lf.txt"
128
+ actual = `#{cmd}`.force_encoding("EUC-JP")
129
+ assert_equal(expected, actual)
130
+ end
131
+
132
+ def test_cli_encoding_shift_jis
133
+ expected_utf8_cr =
134
+ "[-こんにちは-]{+こんばんは+}、私の[-名前はわたなべです-]{+名前はまつもとです+}。\r" \
135
+ "{+Rubyを作ったのは私です。+}私は[-Just Another -]Ruby [-Porter-]{+Hacker+}です。\r"
136
+ expected = NKF.nkf("--ic=UTF-8 --oc=Shift_JIS", expected_utf8_cr)
137
+ cmd = "ruby --external-encoding Shift_JIS -I lib bin/docdiff --encoding=Shift_JIS --format=wdiff " \
138
+ "test/fixture/01_ja_sjis_cr.txt test/fixture/02_ja_sjis_cr.txt"
139
+ actual = `#{cmd}`.force_encoding("Shift_JIS")
140
+ assert_equal(expected, actual)
141
+ end
142
+
143
+ def test_cli_encoding_utf_8
144
+ expected = <<~EOS
145
+ [-こんにちは-]{+こんばんは+}、私の[-名前はわたなべです-]{+名前はまつもとです+}。
146
+ {+Rubyを作ったのは私です。+}私は[-Just Another -]Ruby [-Porter-]{+Hacker+}です。
147
+ EOS
148
+ cmd = "ruby -I lib bin/docdiff --encoding=UTF-8 --format=wdiff " \
149
+ "test/fixture/01_ja_utf8_lf.txt test/fixture/02_ja_utf8_lf.txt"
150
+ actual = `#{cmd}`.force_encoding("UTF-8")
151
+ assert_equal(expected, actual)
152
+ end
153
+
154
+ def test_cli_eol_cr
155
+ expected =
156
+ "Hello, my name is [-Watanabe.-]{+matz.+}\r" \
157
+ "{+It's me who has created Ruby. +}I am [-just another -]{+a +}Ruby [-porter.-]{+hacker.+}\r"
158
+ cmd = "ruby -I lib bin/docdiff --eol=CR --format=wdiff " \
159
+ "test/fixture/01_en_ascii_cr.txt test/fixture/02_en_ascii_cr.txt"
160
+ actual = `#{cmd}`
161
+ assert_equal(expected, actual)
162
+ end
163
+
164
+ def test_cli_eol_lf
165
+ expected =
166
+ "Hello, my name is [-Watanabe.-]{+matz.+}\n" \
167
+ "{+It's me who has created Ruby. +}I am [-just another -]{+a +}Ruby [-porter.-]{+hacker.+}\n"
168
+ cmd = "ruby -I lib bin/docdiff --eol=LF --format=wdiff " \
169
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
170
+ actual = `#{cmd}`
171
+ assert_equal(expected, actual)
172
+ end
173
+
174
+ def test_cli_eol_crlf
175
+ expected =
176
+ "Hello, my name is [-Watanabe.-]{+matz.+}\r\n" \
177
+ "{+It's me who has created Ruby. +}I am [-just another -]{+a +}Ruby [-porter.-]{+hacker.+}\r\n"
178
+ cmd = "ruby -I lib bin/docdiff --eol=CRLF --format=wdiff " \
179
+ "test/fixture/01_en_ascii_crlf.txt test/fixture/02_en_ascii_crlf.txt"
180
+ actual = `#{cmd}`
181
+ assert_equal(expected, actual)
182
+ end
183
+
184
+ def test_cli_format_html
185
+ expected = <<~EOS
186
+ <span class="common">Hello, my name is </span>\
187
+ <span class="before-change"><del>Watanabe.</del></span>\
188
+ <span class="after-change"><ins>matz.</ins></span>\
189
+ <span class="common"><br />
190
+ EOS
191
+ cmd = "ruby -I lib bin/docdiff --format=html " \
192
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
193
+ actual = `#{cmd}`.scan(/^.*?$\n/m)[-4]
194
+ assert_equal(expected, actual)
195
+ end
196
+
197
+ def test_cli_format_manued
198
+ expected = "Hello, my name is [Watanabe./matz.]\n"
199
+ cmd = "ruby -I lib bin/docdiff --format=manued " \
200
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
201
+ actual = `#{cmd}`.scan(/^.*?$\n/m)[-2]
202
+ assert_equal(expected, actual)
203
+ end
204
+
205
+ def test_cli_format_tty
206
+ expected = "Hello, my name is \e[7;4;33mWatanabe.\e[0m\e[7;1;32mmatz.\e[0m\n"
207
+ cmd = "ruby -I lib bin/docdiff --format=tty " \
208
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
209
+ actual = `#{cmd}`.scan(/^.*?$\n/m).first
210
+ assert_equal(expected, actual)
211
+ end
212
+
213
+ def test_cli_format_wdiff
214
+ expected = "Hello, my name is [-Watanabe.-]{+matz.+}\n"
215
+ cmd = "ruby -I lib bin/docdiff --format=wdiff " \
216
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
217
+ actual = `#{cmd}`.scan(/^.*?$\n/m).first
218
+ assert_equal(expected, actual)
219
+ end
220
+
221
+ def test_cli_digest
222
+ expected = <<~EOS
223
+ ----
224
+ 1,1
225
+ Hello, my name is [-Watanabe.-]{+matz.+}
226
+
227
+ ----
228
+ (2),2
229
+
230
+ {+It's me who has created Ruby. +}I am#{" "}
231
+ ----
232
+ 2,2
233
+ I am [-just another -]{+a +}Ruby#{" "}
234
+ ----
235
+ 2,2
236
+ Ruby [-porter.-]{+hacker.+}
237
+
238
+ ----
239
+ EOS
240
+ cmd = "ruby -I lib bin/docdiff --digest --format=wdiff " \
241
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
242
+ actual = `#{cmd}`.force_encoding("UTF-8")
243
+ assert_equal(expected, actual)
244
+ end
245
+
246
+ def test_cli_display_inline
247
+ expected = <<~EOS
248
+ ----
249
+ 1,1
250
+ Hello, my name is [-Watanabe.-]{+matz.+}
251
+
252
+ ----
253
+ (2),2
254
+
255
+ {+It's me who has created Ruby. +}I am#{" "}
256
+ ----
257
+ 2,2
258
+ I am [-just another -]{+a +}Ruby#{" "}
259
+ ----
260
+ 2,2
261
+ Ruby [-porter.-]{+hacker.+}
262
+
263
+ ----
264
+ EOS
265
+ cmd = "ruby -I lib bin/docdiff --digest --display=inline --format=wdiff " \
266
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
267
+ actual = `#{cmd}`.force_encoding("UTF-8")
268
+ assert_equal(expected, actual)
269
+ end
270
+
271
+ def test_cli_display_block
272
+ expected = <<~EOS
273
+ ----
274
+ 1,1
275
+ Hello, my name is [-Watanabe.-]
276
+
277
+ Hello, my name is {+matz.+}
278
+
279
+ ----
280
+ (2),2
281
+
282
+ I am#{" "}
283
+
284
+ {+It's me who has created Ruby. +}I am#{" "}
285
+ ----
286
+ 2,2
287
+ I am [-just another -]Ruby#{" "}
288
+ I am {+a +}Ruby#{" "}
289
+ ----
290
+ 2,2
291
+ Ruby [-porter.-]
292
+
293
+ Ruby {+hacker.+}
294
+
295
+ ----
296
+ EOS
297
+ cmd = "ruby -I lib bin/docdiff --digest --display=block --format=wdiff " \
298
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
299
+ actual = `#{cmd}`.force_encoding("UTF-8")
300
+ assert_equal(expected, actual)
301
+ end
302
+
303
+ def test_cli_config_file_format_wdiff
304
+ config_file_name = File.join(File.dirname(__FILE__), "fixture/format_wdiff.conf")
305
+ expected = <<~EOS
306
+ Hello, my name is [-Watanabe.-]{+matz.+}
307
+ {+It's me who has created Ruby. +}I am [-just another -]{+a +}Ruby [-porter.-]{+hacker.+}
308
+ EOS
309
+ cmd = "ruby -I lib bin/docdiff --config-file=#{config_file_name} " \
310
+ "test/fixture/01_en_ascii_lf.txt test/fixture/02_en_ascii_lf.txt"
311
+ actual = `#{cmd}`
312
+ assert_equal(expected, actual)
313
+ end
314
+ end
data/test/diff_test.rb CHANGED
@@ -1,37 +1,36 @@
1
1
  #!/usr/bin/ruby
2
- require 'test/unit'
2
+ require "test/unit"
3
3
  require "docdiff/diff"
4
4
 
5
- class TC_DocDiff_Diff < Test::Unit::TestCase
5
+ class TestDiff < Test::Unit::TestCase
6
6
  Diff = DocDiff::Diff
7
7
 
8
- def setup()
9
- #
8
+ def setup
10
9
  end
11
10
 
12
- def test_new_ses()
11
+ def test_new_ses
13
12
  a1 = [:a, :b, :c]
14
13
  a2 = [:a, :x, :c]
15
- expected = [[:common_elt_elt, [:a], [:a]],
16
- [:del_elt, [:b], nil],
17
- [:add_elt, nil, [:x]],
18
- [:common_elt_elt, [:c], [:c]]]
14
+ expected = [
15
+ [:common_elt_elt, [:a], [:a]],
16
+ [:del_elt, [:b], nil],
17
+ [:add_elt, nil, [:x]],
18
+ [:common_elt_elt, [:c], [:c]],
19
+ ]
19
20
  actual = []
20
21
  actual_speculative = []
21
22
  actual_shortestpath = []
22
23
  actual_contours = []
23
- Diff.new(a1, a2).ses .each{|e| actual << e}
24
- Diff.new(a1, a2).ses(:speculative ).each{|e| actual_speculative << e}
25
- Diff.new(a1, a2).ses(:shortestpath).each{|e| actual_shortestpath << e}
26
- Diff.new(a1, a2).ses(:contours ).each{|e| actual_contours << e}
24
+ Diff.new(a1, a2).ses.each { |e| actual << e }
25
+ Diff.new(a1, a2).ses(:speculative).each { |e| actual_speculative << e }
26
+ Diff.new(a1, a2).ses(:shortestpath).each { |e| actual_shortestpath << e }
27
+ Diff.new(a1, a2).ses(:contours).each { |e| actual_contours << e }
27
28
  assert_equal(expected, actual)
28
29
  assert_equal(expected, actual_speculative)
29
30
  assert_equal(expected, actual_shortestpath)
30
31
  assert_equal(expected, actual_contours)
31
32
  end
32
33
 
33
- def teardown()
34
- #
34
+ def teardown
35
35
  end
36
-
37
36
  end
@@ -1,65 +1,74 @@
1
1
  #!/usr/bin/ruby
2
- require 'test/unit'
3
- require 'docdiff/difference'
2
+ require "test/unit"
3
+ require "docdiff/difference"
4
4
 
5
- class TC_DocDiff_Difference < Test::Unit::TestCase
5
+ class TestDifference < Test::Unit::TestCase
6
6
  Difference = DocDiff::Difference
7
7
 
8
- def setup()
9
- #
8
+ def setup
10
9
  end
11
10
 
12
- def test_new()
11
+ def test_new
13
12
  array1 = [:a, :b, :c]
14
13
  array2 = [:a, :x, :c]
15
- expected = [[:common_elt_elt, [:a], [:a]],
16
- [:change_elt, [:b], [:x]],
17
- [:common_elt_elt, [:c], [:c]]]
14
+ expected = [
15
+ [:common_elt_elt, [:a], [:a]],
16
+ [:change_elt, [:b], [:x]],
17
+ [:common_elt_elt, [:c], [:c]],
18
+ ]
18
19
  assert_equal(expected, Difference.new(array1, array2))
19
20
  end
20
21
 
21
- def test_raw_list()
22
+ def test_raw_list
22
23
  array1 = [:a, :b, :c]
23
24
  array2 = [:a, :x, :c]
24
- expected = [[:common_elt_elt, [:a], [:a]],
25
- [:del_elt, [:b], nil],
26
- [:add_elt, nil, [:x]],
27
- [:common_elt_elt, [:c], [:c]]]
25
+ expected = [
26
+ [:common_elt_elt, [:a], [:a]],
27
+ [:del_elt, [:b], nil],
28
+ [:add_elt, nil, [:x]],
29
+ [:common_elt_elt, [:c], [:c]],
30
+ ]
28
31
  assert_equal(expected, Difference.new(array1, array2).raw_list)
29
32
  end
30
33
 
31
- def test_former_only()
34
+ def test_former_only
32
35
  array1 = [:a, :b, :c]
33
36
  array2 = [:a, :x, :c]
34
- expected = [[:common_elt_elt, [:a], [:a]],
35
- [:change_elt, [:b], nil],
36
- [:common_elt_elt, [:c], [:c]]]
37
+ expected = [
38
+ [:common_elt_elt, [:a], [:a]],
39
+ [:change_elt, [:b], nil],
40
+ [:common_elt_elt, [:c], [:c]],
41
+ ]
37
42
  assert_equal(expected, Difference.new(array1, array2).former_only)
38
43
  array1 = [:a, :b, :c]
39
44
  array2 = [:a, :c, :d]
40
- expected = [[:common_elt_elt, [:a], [:a]],
41
- [:del_elt, [:b], nil],
42
- [:common_elt_elt, [:c], [:c]]]
45
+ expected = [
46
+ [:common_elt_elt, [:a], [:a]],
47
+ [:del_elt, [:b], nil],
48
+ [:common_elt_elt, [:c], [:c]],
49
+ ]
43
50
  assert_equal(expected, Difference.new(array1, array2).former_only)
44
51
  end
45
52
 
46
- def test_latter_only()
53
+ def test_latter_only
47
54
  array1 = [:a, :b, :c]
48
55
  array2 = [:a, :x, :c]
49
- expected = [[:common_elt_elt, [:a], [:a]],
50
- [:change_elt, nil, [:x]],
51
- [:common_elt_elt, [:c], [:c]]]
56
+ expected = [
57
+ [:common_elt_elt, [:a], [:a]],
58
+ [:change_elt, nil, [:x]],
59
+ [:common_elt_elt, [:c], [:c]],
60
+ ]
52
61
  assert_equal(expected, Difference.new(array1, array2).latter_only)
53
62
  array1 = [:a, :b, :c]
54
63
  array2 = [:a, :c, :d]
55
- expected = [[:common_elt_elt, [:a], [:a]],
56
- [:common_elt_elt, [:c], [:c]],
57
- [:add_elt, nil, [:d]]]
64
+ expected = [
65
+ [:common_elt_elt, [:a], [:a]],
66
+ [:common_elt_elt, [:c], [:c]],
67
+ [:add_elt, nil, [:d]],
68
+ ]
58
69
  assert_equal(expected, Difference.new(array1, array2).latter_only)
59
70
  end
60
71
 
61
- def teardown()
62
- #
72
+ def teardown
63
73
  end
64
-
65
74
  end