pretty_diff 0.9.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +2 -0
  3. data/Gemfile.lock +41 -6
  4. data/Rakefile +0 -1
  5. data/VERSION +1 -1
  6. data/lib/pretty_diff.rb +5 -6
  7. data/lib/pretty_diff/abstract_generator.rb +51 -0
  8. data/lib/pretty_diff/basic_generator.rb +72 -0
  9. data/lib/pretty_diff/chunk.rb +24 -41
  10. data/lib/pretty_diff/diff.rb +75 -41
  11. data/lib/pretty_diff/encoding.rb +30 -0
  12. data/lib/pretty_diff/line.rb +30 -51
  13. data/lib/pretty_diff/line_numbers.rb +47 -67
  14. data/pretty_diff.gemspec +33 -18
  15. data/test/abstract_generator_test.rb +65 -0
  16. data/test/basic_generator_test.rb +29 -0
  17. data/test/chunk_test.rb +13 -30
  18. data/test/diff_test.rb +50 -19
  19. data/test/encoding_test.rb +35 -0
  20. data/test/fixtures/blank.diff +0 -0
  21. data/test/fixtures/blank.diff.html +1 -0
  22. data/test/fixtures/chinese-gbk-crlf +5 -0
  23. data/test/fixtures/cp1251.diff +13 -0
  24. data/test/fixtures/csharp.diff +12 -0
  25. data/test/fixtures/csharp.diff.html +19 -0
  26. data/test/{data → fixtures}/first.diff +0 -0
  27. data/test/fixtures/first.diff.html +85 -0
  28. data/test/{data → fixtures}/second.diff +0 -0
  29. data/test/fixtures/single_line.diff +7 -0
  30. data/test/fixtures/single_line.diff.html +4 -0
  31. data/test/fixtures/text.diff +8 -0
  32. data/test/fixtures/text.diff.html +13 -0
  33. data/test/fixtures/windows-cp1251-lf +6 -0
  34. data/test/helper.rb +10 -8
  35. data/test/line_numbers_test.rb +25 -0
  36. data/test/line_test.rb +26 -37
  37. metadata +81 -75
  38. data/lib/pretty_diff/html_generators/chunk_generator.rb +0 -41
  39. data/lib/pretty_diff/html_generators/diff_generator.rb +0 -13
  40. data/lib/pretty_diff/html_generators/line_generator.rb +0 -45
  41. data/lib/pretty_diff/html_generators/line_numbers_generator.rb +0 -36
  42. data/test/html_generator_test.rb +0 -39
data/test/diff_test.rb CHANGED
@@ -1,29 +1,60 @@
1
- require File.dirname(__FILE__) + '/helper'
2
- require 'cgi'
1
+ require File.join(File.dirname(__FILE__), 'helper')
3
2
 
4
- class DiffTest < Test::Unit::TestCase
5
- context "Pretty Diff" do
3
+ class DiffTest < MiniTest::Unit::TestCase
4
+ class SuperGenerator < PrettyDiff::AbstractGenerator; def generate;end; end
5
+ class UberGenerator < SuperGenerator; end
6
6
 
7
- setup do
8
- @small_diff = PrettyDiff::Diff.new read_diff('first.diff')
9
- @big_diff = PrettyDiff::Diff.new read_diff('second.diff')
10
- end
7
+ class NotAGenerator; end
11
8
 
12
- should "generate correct amount of chunks for each diff" do
13
- assert_equal 2, @small_diff.send(:chunks).size
14
- assert_equal 4, @big_diff.send(:chunks).size
15
- end
9
+ def setup
10
+ @diff = new_diff(fixture('first.diff'))
11
+ end
16
12
 
17
- should "generate HTML representation without errors" do
18
- assert @small_diff.to_html
19
- assert @big_diff.to_html
13
+ def test_chunks
14
+ assert_equal 2, @diff.chunks.size
15
+ @diff.chunks.each do |c|
16
+ assert c.kind_of?(PrettyDiff::Chunk)
20
17
  end
18
+ end
19
+
20
+ def test_metadata
21
+ assert_equal "--- Revision 1945\n+++ Revision 1995\n", @diff.metadata
22
+ end
23
+
24
+ def test_contents
25
+ assert_equal 32, @diff.contents.lines.size
26
+ assert_equal "@@ -8,6 +8,10 @@\n", @diff.contents.lines.first
27
+ assert_equal " }\n", @diff.contents.lines.last
28
+ end
29
+
30
+ def test_default_generator
31
+ assert_equal 'PrettyDiff::BasicGenerator', new_diff('bla').generator.to_s
32
+ end
21
33
 
22
- should "escape HTML from the input string" do
23
- text = "<b>Funky HTML</b>"
24
- diff = PrettyDiff::Diff.new(text)
25
- assert_equal "&lt;b&gt;Funky HTML&lt;/b&gt;", diff.input
34
+ def test_custom_generator
35
+ assert_equal 'DiffTest::SuperGenerator', new_diff('bla', :generator => SuperGenerator).generator.to_s
36
+ end
37
+
38
+ def test_invalid_custom_generator
39
+ assert_raises PrettyDiff::InvalidGenerator do
40
+ new_diff('bla', :generator => NotAGenerator)
26
41
  end
27
42
 
43
+ assert new_diff('bla', :generator => UberGenerator)
28
44
  end
45
+
46
+ def test_default_encoding
47
+ assert_equal 'utf-8', new_diff('bla').out_encoding.downcase
48
+ end
49
+
50
+ def test_custom_encoding
51
+ assert_equal 'cp1251', new_diff('bla', :out_encoding => 'cp1251').out_encoding
52
+ end
53
+
54
+ def test_encoding_cp1251_diff
55
+ diff = new_diff(fixture('cp1251.diff'))
56
+ assert_equal 1, diff.chunks.size
57
+ assert diff.contents.include?('Сенат США')
58
+ end
59
+
29
60
  end
@@ -0,0 +1,35 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class EncodingTest < MiniTest::Unit::TestCase
4
+
5
+ def test_detect
6
+ assert_equal 'windows-1251', enc.detect(fixture('windows-cp1251-lf'))
7
+ end
8
+
9
+ def test_convert_cp1251_to_utf8
10
+ text = enc.convert(fixture('windows-cp1251-lf'), 'windows-1251', 'utf-8')
11
+ assert text.include?('Сенат США')
12
+ end
13
+
14
+ def test_keep_utf8
15
+ text = enc.convert('Вася молодец', 'utf-8', 'utf-8')
16
+ assert_equal 'Вася молодец', text
17
+ end
18
+
19
+ if RUBY_VERSION >= '2.0.0'
20
+ def test_force_encoding_on_ruby2
21
+ text = enc.enforce('utf-8', fixture('windows-cp1251-lf'))
22
+ assert_equal 'utf-8', text.encoding.to_s.downcase
23
+
24
+ text = enc.enforce('windows-1251', fixture('windows-cp1251-lf'))
25
+ assert_equal 'windows-1251', text.encoding.to_s.downcase
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def enc
32
+ PrettyDiff::Encoding
33
+ end
34
+
35
+ end
File without changes
@@ -0,0 +1 @@
1
+ <div class="diff"></div>
@@ -0,0 +1,5 @@
1
+ GBK �����ּ��Ϥ� 1993��� GB 2312-80 �Β����Ȥ��ƶ��x���줿����GB 2312
2
+ ��δʹ�äΥ��`�ɥݥ���Ȥ����ä��� GB 13000.1-93 �����֤⺬��Ǥ��������Τ���
3
+ GBK �� GB 2312 �ˌ�������λ���Q�Ǥ��롣
4
+
5
+ Chinese GBK LF
@@ -0,0 +1,13 @@
1
+ diff --git a/windows-cp1251-lf b/windows-cp1251-lf
2
+ deleted file mode 100644
3
+ index 9604f79..0000000
4
+ --- a/windows-cp1251-lf
5
+ +++ /dev/null
6
+ @@ -1,6 +0,0 @@
7
+ -����� ��� 10 ������ �������� �� ���� �������� ���������� ��� �����������
8
+ -����� ������, ���������� � ������������ ���������� REI, ������� ����������
9
+ -�������� ���������� �������. �� ����� �������� ���������� ��� ����� ������
10
+ -������� ���� ��������, ������� ���������� ������������ � 2009 ����.
11
+ -
12
+ -Windows CP1251 LF
13
+
@@ -0,0 +1,12 @@
1
+ diff --git a/src/Data/ServiceMail/ServiceEmailBase.cs b/src/Data/ServiceMail/ServiceEmailBase.cs
2
+ index a93dedb..5cb1240 100644
3
+ --- a/src/Data/ServiceMail/ServiceEmailBase.cs
4
+ +++ b/src/Data/ServiceMail/ServiceEmailBase.cs
5
+ @@ -21,7 +21,7 @@ namespace PostalService.Data.ServiceMail
6
+ public abstract string GenerateSubject();
7
+ public abstract string GenerateTextBody();
8
+ public abstract string GenerateHtmlBody();
9
+ - public abstract string GenerateTag();
10
+ + public abstract string GenerateTag(); change on line 24
11
+
12
+ private readonly SystemSettings _systemSettings;
@@ -0,0 +1,19 @@
1
+ <div class="diff"><div class="diff-chunk"><div class="diff-line-nums"><pre>21
2
+ 22
3
+ 23
4
+ 24
5
+ &nbsp;
6
+ 25
7
+ 26</pre></div><div class="diff-line-nums"><pre>21
8
+ 22
9
+ 23
10
+ &nbsp;
11
+ 24
12
+ 25
13
+ 26</pre></div><div class="diff-code"><pre> public abstract string GenerateSubject();
14
+ public abstract string GenerateTextBody();
15
+ public abstract string GenerateHtmlBody();
16
+ <span class="diff-line-del">- public abstract string GenerateTag();</span>
17
+ <span class="diff-line-add">+ public abstract string GenerateTag(); change on line 24</span>
18
+
19
+ private readonly SystemSettings _systemSettings;</pre></div></div></div>
File without changes
@@ -0,0 +1,85 @@
1
+ <div class="diff"><div class="diff-chunk"><div class="diff-line-nums"><pre>8
2
+ 9
3
+ 10
4
+ &nbsp;
5
+ &nbsp;
6
+ &nbsp;
7
+ &nbsp;
8
+ 11
9
+ 12
10
+ 13</pre></div><div class="diff-line-nums"><pre>8
11
+ 9
12
+ 10
13
+ 11
14
+ 12
15
+ 13
16
+ 14
17
+ 15
18
+ 16
19
+ 17</pre></div><div class="diff-code"><pre> border: 0;
20
+ }
21
+
22
+ <span class="diff-line-add">+.sep {</span>
23
+ <span class="diff-line-add">+ color: #AAA;</span>
24
+ <span class="diff-line-add">+}</span>
25
+ <span class="diff-line-add">+</span>
26
+ /* Short statistic */
27
+
28
+ .short-stat dl {</pre></div></div><div class="diff-chunk"><div class="diff-line-nums"><pre>59
29
+ 60
30
+ 61
31
+ 62
32
+ &nbsp;
33
+ &nbsp;
34
+ &nbsp;
35
+ &nbsp;
36
+ &nbsp;
37
+ &nbsp;
38
+ 63
39
+ 64
40
+ 65
41
+ 66
42
+ 67
43
+ &nbsp;
44
+ &nbsp;
45
+ 68
46
+ 69
47
+ 70</pre></div><div class="diff-line-nums"><pre>63
48
+ 64
49
+ 65
50
+ &nbsp;
51
+ 66
52
+ 67
53
+ 68
54
+ 69
55
+ 70
56
+ 71
57
+ 72
58
+ 73
59
+ 74
60
+ 75
61
+ &nbsp;
62
+ 76
63
+ 77
64
+ 78
65
+ 79
66
+ 80</pre></div><div class="diff-code"><pre> color: #999;
67
+ }
68
+
69
+ <span class="diff-line-del">- table.account-overview td .status {</span>
70
+ <span class="diff-line-add">+ table.account-overview td.label.top {</span>
71
+ <span class="diff-line-add">+ vertical-align: top;</span>
72
+ <span class="diff-line-add">+ }</span>
73
+ <span class="diff-line-add">+ </span>
74
+ <span class="diff-line-add">+ table.account-overview td .status,</span>
75
+ <span class="diff-line-add">+ table.repos-table td .status {</span>
76
+ padding-left: 1.75em;
77
+ background: url(../images/i_error_light.gif) 0 0 no-repeat;
78
+ }
79
+
80
+ <span class="diff-line-del">- table.account-overview td .status.active {</span>
81
+ <span class="diff-line-add">+ table.account-overview td .status.active,</span>
82
+ <span class="diff-line-add">+ table.repos-table td .status.success {</span>
83
+ padding-left: 1.75em;
84
+ background: url(../images/i-yes.gif) 0 0 no-repeat;
85
+ }</pre></div></div></div>
File without changes
@@ -0,0 +1,7 @@
1
+ diff --git a/html/exp2.txt b/html/exp2.txt
2
+ index a78cb18..e2274ae 100644
3
+ --- a/html/exp2.txt
4
+ +++ b/html/exp2.txt
5
+ @@ -1 +1 @@
6
+ -assome example text
7
+ +asassome example text
@@ -0,0 +1,4 @@
1
+ <div class="diff"><div class="diff-chunk"><div class="diff-line-nums"><pre>1
2
+ &nbsp;</pre></div><div class="diff-line-nums"><pre>&nbsp;
3
+ 1</pre></div><div class="diff-code"><pre><span class="diff-line-del">-assome example text</span>
4
+ <span class="diff-line-add">+asassome example text</span></pre></div></div></div>
@@ -0,0 +1,8 @@
1
+ --- a/selenium.txt
2
+ +++ b/selenium.txt
3
+ @@ -3,4 +3,4 @@ line2
4
+ line3
5
+ line4 new
6
+ line5
7
+ -line6 change seleniunm
8
+ +line6 change
@@ -0,0 +1,13 @@
1
+ <div class="diff"><div class="diff-chunk"><div class="diff-line-nums"><pre>3
2
+ 4
3
+ 5
4
+ 6
5
+ &nbsp;</pre></div><div class="diff-line-nums"><pre>3
6
+ 4
7
+ 5
8
+ &nbsp;
9
+ 6</pre></div><div class="diff-code"><pre> line3
10
+ line4 new
11
+ line5
12
+ <span class="diff-line-del">-line6 change seleniunm</span>
13
+ <span class="diff-line-add">+line6 change</span></pre></div></div></div>
@@ -0,0 +1,6 @@
1
+ ����� ��� 10 ������ �������� �� ���� �������� ���������� ��� �����������
2
+ ����� ������, ���������� � ������������ ���������� REI, ������� ����������
3
+ �������� ���������� �������. �� ����� �������� ���������� ��� ����� ������
4
+ ������� ���� ��������, ������� ���������� ������������ � 2009 ����.
5
+
6
+ Windows CP1251 LF
data/test/helper.rb CHANGED
@@ -1,15 +1,17 @@
1
1
  require 'rubygems'
2
- require 'test/unit'
3
- require 'shoulda'
2
+ require 'bundler/setup'
3
+ require 'minitest/autorun'
4
4
 
5
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
- $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- require 'pretty_diff'
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'pretty_diff')
8
6
 
9
- class Test::Unit::TestCase
7
+ class MiniTest::Unit::TestCase
10
8
 
11
- def read_diff(name)
12
- File.read(File.join(File.dirname(__FILE__), "data", name))
9
+ def new_diff(*args)
10
+ PrettyDiff::Diff.new(*args)
11
+ end
12
+
13
+ def fixture(name)
14
+ File.read(File.join(File.dirname(__FILE__), "fixtures", name))
13
15
  end
14
16
 
15
17
  end
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ class LineNumbersTest < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @diff = new_diff(fixture('first.diff'))
7
+ @numbers = @diff.chunks.last.line_numbers
8
+ end
9
+
10
+ def test_left_column
11
+ assert_equal [59, 60, 61, 62, nil, nil, nil, nil, nil, nil, 63, 64, 65, 66, 67, nil, nil, 68, 69, 70],
12
+ @numbers.left_column
13
+ end
14
+
15
+ def test_right_column
16
+ assert_equal [63, 64, 65, nil, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, nil, 76, 77, 78, 79, 80],
17
+ @numbers.right_column
18
+ end
19
+
20
+ def test_line_numbers
21
+ @diff = new_diff fixture('single_line.diff')
22
+ assert_equal [1, nil], @diff.chunks.last.line_numbers.left_column
23
+ assert_equal [nil, 1], @diff.chunks.last.line_numbers.right_column
24
+ end
25
+ end
data/test/line_test.rb CHANGED
@@ -1,45 +1,34 @@
1
- require File.dirname(__FILE__) + '/helper'
2
-
3
- class LineTest < Test::Unit::TestCase
4
- context "PrettyDiff's Line" do
5
-
6
- should "indicate :added status correctly" do
7
- added_line = new_diff("+package chiro.methods.new.ones;")
8
- assert_equal :added, added_line.status
9
- end
10
-
11
- should "indicate :deleted status correctly" do
12
- deleted_line = new_diff('-browser.setTimeout("50000");')
13
- assert_equal :deleted, deleted_line.status
14
- end
15
-
16
- should "indicate :not_modified status correctly" do
17
- not_modified_line = new_diff("class User < ActiveRecord::Base")
18
- assert_equal :not_modified, not_modified_line.status
19
- end
20
-
21
- should "ignore trailing 'no newline' text" do
22
- text = ''
23
- line = new_diff(text)
24
- assert line.ignore?
25
- end
26
-
27
- should "replace tabs with spaces" do
28
- text = " hello eptut {1, 2, 3}"
29
- expected_output = " hello eptut {1, 2, 3}"
30
- line = new_diff(text)
31
- assert_equal expected_output, line.rendered
32
- end
1
+ require File.join(File.dirname(__FILE__), 'helper')
33
2
 
3
+ class LineTest < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @diff = new_diff(fixture('first.diff'))
7
+ @lines = @diff.chunks.last.lines
8
+ end
9
+
10
+ def test_contents
11
+ assert_equal " color: #999;", @lines[0].contents
12
+ assert_equal "- table.account-overview td .status {", @lines[3].contents
13
+ assert_equal "+ table.account-overview td.label.top {", @lines[4].contents
14
+ assert_equal " }", @lines.last.contents
34
15
  end
35
16
 
36
- private
17
+ def test_ignored
18
+ line = PrettyDiff::Line.new(@diff, '')
19
+ assert line.ignored?
20
+ end
37
21
 
38
- def new_diff(text)
39
- PrettyDiff::Line.new(text)
22
+ def test_status_added
23
+ assert_equal :added, @lines[4].status
40
24
  end
41
25
 
42
- def line_to_html(text)
43
- new_diff(text).to_html
26
+ def test_status_deleted
27
+ assert_equal :deleted, @lines[3].status
44
28
  end
29
+
30
+ def test_status_not_modified
31
+ assert_equal :not_modified, @lines[0].status
32
+ end
33
+
45
34
  end