diffy 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of diffy might be problematic. Click here for more details.

data/README.md CHANGED
@@ -6,9 +6,20 @@ way to generate a diff from two strings. Instead of reimplementing the LCS diff
6
6
  algorithm Diffy uses battle tested Unix diff to generate diffs, and focuses on
7
7
  providing a convenient interface, and getting out of your way.
8
8
 
9
- It provides several built in format options. Pass `:text`, `:color`, or
10
- `:html` to `Diffy::Diff#to_s` to force that format, or set
11
- `Diffy::Diff.default_format`
9
+ Supported Formats
10
+ -----------------
11
+
12
+ It provides several built in format options which can be passed to
13
+ `Diffy::Diff#to_s`.
14
+
15
+ * `:text` - Plain text output
16
+ * `:color` - ANSI colorized text suitable for use in a terminal
17
+ * `:html` - HTML output. Since version 2.0 this format does inline highlighting of the changes between two the changes within lines.
18
+ * `:html_simple` - HTML output without inline highlighting. This may be useful in situations where high performance is required or simpler output is desired.
19
+
20
+ A default format can be set like so:
21
+
22
+ Diffy::Diff.default_format = :html
12
23
 
13
24
  Getting Started
14
25
  ---------------
@@ -39,7 +50,7 @@ Here's an example of using Diffy to diff two strings
39
50
 
40
51
  Outputing the diff as html is easy too.
41
52
 
42
- >> puts Diffy::Diff.new(string1, string2).to_s(:html)
53
+ >> puts Diffy::Diff.new(string1, string2).to_s(:html_simple)
43
54
  <div class="diff">
44
55
  <ul>
45
56
  <li class="del"><del>Hello how are you</del></li>
@@ -56,26 +67,23 @@ Then try adding this css to your stylesheets:
56
67
  .diff ul{background:#fff;overflow:auto;font-size:13px;list-style:none;margin:0;padding:0;display:table;width:100%;}
57
68
  .diff del, .diff ins{display:block;text-decoration:none;}
58
69
  .diff li{padding:0; display:table-row;margin: 0;}
70
+ /* try 'whitespace:pre;' if you don't want lines to wrap */
71
+ .diff del, .diff ins, .diff span{white-space:pre-wrap;font-family:courier;}
59
72
  .diff li.ins{background:#9f9;}
60
- .diff li.del{background:#ccf;}
61
- .diff li:hover{background:#ffc}
62
- .diff del, .diff ins, .diff span{white-space:pre;font-family:courier;}
73
+ .diff li.del{background:#fcc;}
74
+ .diff li.ins strong{font-weight:normal; background: #6f6 }
75
+ .diff li.del strong{font-weight:normal; background: #f99 }
63
76
 
64
- `Diffy::Diff` also alows you to set a default format. Here we set the default to
65
- use ANSI termnial color escape sequences.
77
+ You can also diff files instead of strings
66
78
 
67
- >> Diffy::Diff.default_format = :color
68
- => :color
69
- >> puts Diffy::Diff.new(string1, string2) # prints color in the terminal
70
- -Hello how are you
71
- +Hello how are you?
72
- I'm fine
73
- -That's great
74
- +That's swell
79
+ >> puts Diffy::Diff.new('/tmp/foo', '/tmp/bar', :source => 'files')
75
80
 
81
+ Custom Formats
82
+ --------------
76
83
 
77
- Creating custom formatted output is easy too. `Diffy::Diff` provides an
78
- enumberable interface which lets you iterate over lines in the diff.
84
+ Diffy tries to make generating your own custom formatted output easy.
85
+ `Diffy::Diff` provides an enumberable interface which lets you iterate over
86
+ lines in the diff.
79
87
 
80
88
  >> Diffy::Diff.new("foo\nbar\n", "foo\nbar\nbaz\n").each do |line|
81
89
  >* case line
@@ -86,7 +94,25 @@ enumberable interface which lets you iterate over lines in the diff.
86
94
  line +baz added
87
95
  => [" foo\n", " bar\n", "+baz\n"]
88
96
 
97
+ You can also use `Diffy::Diff#each_chunk` to iterate each grouping of additions,
98
+ deletions, and unchanged in a diff.
99
+
100
+ >> Diffy::Diff.new("foo\nbar\nbang\nbaz\n", "foo\nbar\nbing\nbong\n").each_chunk.to_a
101
+ => [" foo\n bar\n", "-bang\n-baz\n", "+bing\n+bong\n"]
102
+
89
103
  Use `#map`, `#inject`, or any of Enumerable's methods. Go crazy.
90
104
 
91
- Report bugs or request features at http://github.com/samg/Diffy/issues
105
+ Ruby Version Compatibility
106
+ -------------------------
107
+
108
+ Support for Ruby 1.8.6 was dropped beggining at version 2.0 in order to support
109
+ the chainable enumerators available in 1.8.7 and 1.9.
110
+
111
+ If you want to use Diffy and Ruby 1.8.6 then:
112
+
113
+ $ gem install diffy -v1.1.0
114
+
115
+ ---------------------------------------------------------------------
116
+
117
+ Report bugs or request features at http://github.com/samg/diffy/issues
92
118
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 2.0.0
data/dirb.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{diffy}
8
- s.version = "1.1.0"
8
+ s.version = "2.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Sam Goldstein"]
12
- s.date = %q{2010-10-15}
12
+ s.date = %q{2010-11-20}
13
13
  s.description = %q{Convenient diffing in ruby}
14
14
  s.email = %q{sgrock@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -25,6 +25,9 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "diffy.gemspec",
27
27
  "lib/diffy.rb",
28
+ "lib/diffy/diff.rb",
29
+ "lib/diffy/format.rb",
30
+ "lib/diffy/html_formatter.rb",
28
31
  "spec/diffy_spec.rb"
29
32
  ]
30
33
  s.homepage = %q{http://github.com/samg/diffy/tree/master}
data/lib/diffy.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'tempfile'
2
+ require 'open3'
3
+ require 'erb'
4
+ # 1.9 compatibility
5
+ if defined? Enumerator and ! defined? Enumerable::Enumerator
6
+ Enumerable::Enumerator = Enumerator
7
+ end
8
+
9
+ module Diffy; end
10
+ require File.join(File.dirname(__FILE__), 'diffy', 'format')
11
+ require File.join(File.dirname(__FILE__), 'diffy', 'html_formatter')
12
+ require File.join(File.dirname(__FILE__), 'diffy', 'diff')
data/lib/diffy/diff.rb ADDED
@@ -0,0 +1,105 @@
1
+ module Diffy
2
+ class Diff
3
+ class << self
4
+ attr_writer :default_format
5
+ def default_format
6
+ @default_format || :text
7
+ end
8
+ end
9
+ include Enumerable
10
+ attr_reader :string1, :string2, :options, :diff
11
+
12
+ # supported options
13
+ # +:diff+:: A cli options string passed to diff
14
+ # +:source+:: Either _strings_ or _files_. Determines whether string1
15
+ # and string2 should be interpreted as strings or file paths.
16
+ def initialize(string1, string2, options = {})
17
+ @options = {:diff => '-U 10000', :source => 'strings'}.merge(options)
18
+ if ! ['strings', 'files'].include?(@options[:source])
19
+ raise ArgumentError, "Invalid :source option #{@options[:source].inspect}. Supported options are 'strings' and 'files'."
20
+ end
21
+ @string1, @string2 = string1, string2
22
+ end
23
+
24
+ def diff
25
+ @diff ||= begin
26
+ paths = case options[:source]
27
+ when 'strings'
28
+ [tempfile(string1), tempfile(string2)]
29
+ when 'files'
30
+ [string1, string2]
31
+ end
32
+ diff ||= Open3.popen3(
33
+ *[ diff_bin, options[:diff], *paths ]
34
+ ) { |i, o, e| o.read }
35
+ if diff =~ /\A\s*\Z/
36
+ diff = case options[:source]
37
+ when 'strings' then string1
38
+ when 'files' then File.read(string1)
39
+ end.gsub(/^/, " ")
40
+ end
41
+ diff
42
+ end
43
+ end
44
+
45
+ def each
46
+ lines = diff.split("\n").reject{|x| x =~ /^---|\+\+\+|@@|\\\\/ }.
47
+ map{|line| line + "\n"}
48
+ if block_given?
49
+ lines.each{|line| yield line}
50
+ else
51
+ Enumerable::Enumerator.new(lines)
52
+ end
53
+ end
54
+
55
+ def each_chunk
56
+ old_state = nil
57
+ chunks = inject([]) do |cc, line|
58
+ state = line.each_char.first
59
+ if state == old_state
60
+ cc.last << line
61
+ else
62
+ cc.push line.dup
63
+ end
64
+ old_state = state
65
+ cc
66
+ end
67
+
68
+ if block_given?
69
+ chunks.each{|chunk| yield chunk }
70
+ else
71
+ Enumerable::Enumerator.new(chunks)
72
+ end
73
+ end
74
+
75
+ def tempfile(string)
76
+ t = Tempfile.new('diffy')
77
+ t.print(string)
78
+ t.flush
79
+ t.path
80
+ end
81
+
82
+ def to_s(format = nil)
83
+ format ||= self.class.default_format
84
+ formats = Format.instance_methods(false).map{|x| x.to_s}
85
+ if formats.include? format.to_s
86
+ enum = self
87
+ enum.extend Format
88
+ enum.send format
89
+ else
90
+ raise ArgumentError,
91
+ "Format #{format.inspect} not found in #{formats.inspect}"
92
+ end
93
+ end
94
+ private
95
+
96
+ def diff_bin
97
+ bin = `which diff`.chomp
98
+ if bin.empty?
99
+ raise "Can't find a diff executable in PATH #{ENV['PATH']}"
100
+ end
101
+ bin
102
+ end
103
+
104
+ end
105
+ end
@@ -0,0 +1,33 @@
1
+ module Diffy
2
+ module Format
3
+ # ANSI color output suitable for terminal output
4
+ def color
5
+ map do |line|
6
+ case line
7
+ when /^\+/
8
+ "\033[32m#{line.chomp}\033[0m"
9
+ when /^-/
10
+ "\033[31m#{line.chomp}\033[0m"
11
+ else
12
+ line.chomp
13
+ end
14
+ end.join("\n") + "\n"
15
+ end
16
+
17
+ # Basic text output
18
+ def text
19
+ to_a.join
20
+ end
21
+
22
+ # Basic html output which does not attempt to highlight the changes
23
+ # between lines, and is more performant.
24
+ def html_simple
25
+ HtmlFormatter.new(self).to_s
26
+ end
27
+
28
+ # Html output which does inline highlighting of changes between two lines.
29
+ def html
30
+ HtmlFormatter.new(self, :highlight_words => true).to_s
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,90 @@
1
+ module Diffy
2
+ class HtmlFormatter
3
+ def initialize(diff, options = {})
4
+ @diff = diff
5
+ @options = options
6
+ end
7
+
8
+ def to_s
9
+ if @options[:highlight_words]
10
+ wrap_lines(highlighted_words)
11
+ else
12
+ wrap_lines(@diff.map{|line| wrap_line(ERB::Util.h(line))})
13
+ end
14
+ end
15
+
16
+ private
17
+ def wrap_line(line)
18
+ cleaned = line.gsub(/^./, '').chomp
19
+ case line
20
+ when /^\+/
21
+ ' <li class="ins"><ins>' + cleaned + '</ins></li>'
22
+ when /^-/
23
+ ' <li class="del"><del>' + cleaned + '</del></li>'
24
+ when /^ /
25
+ ' <li class="unchanged"><span>' + cleaned + '</span></li>'
26
+ end
27
+ end
28
+
29
+ def wrap_lines(lines)
30
+ %'<div class="diff">\n <ul>\n#{lines.join("\n")}\n </ul>\n</div>\n'
31
+ end
32
+
33
+ def highlighted_words
34
+ chunks = @diff.each_chunk.to_a
35
+ processed = []
36
+ lines = chunks.each_with_index.map do |chunk1, index|
37
+ next if processed.include? index
38
+ processed << index
39
+ chunk1 = chunk1
40
+ chunk2 = chunks[index + 1]
41
+ if not chunk2
42
+ next chunk1
43
+ end
44
+
45
+ chunk1 = ERB::Util.h(chunk1)
46
+ chunk2 = ERB::Util.h(chunk2)
47
+
48
+ dir1 = chunk1.each_char.first
49
+ dir2 = chunk2.each_char.first
50
+ case [dir1, dir2]
51
+ when ['-', '+']
52
+ line_diff = Diffy::Diff.new(
53
+ split_characters(chunk1),
54
+ split_characters(chunk2)
55
+ )
56
+ hi1 = reconstruct_characters(line_diff, '-')
57
+ hi2 = reconstruct_characters(line_diff, '+')
58
+ processed << (index + 1)
59
+ [hi1, hi2]
60
+ else
61
+ chunk1
62
+ end
63
+ end.flatten
64
+ lines.map{|line| line.each_line.map(&:chomp).to_a if line }.flatten.compact.
65
+ map{|line|wrap_line(line) }.compact
66
+ end
67
+
68
+ def split_characters(chunk)
69
+ chunk.gsub(/^./, '').each_line.map do |line|
70
+ line.chomp.split('') + ['\n']
71
+ end.flatten.join("\n")
72
+ end
73
+
74
+ def reconstruct_characters(line_diff, type)
75
+ line_diff.each_chunk.map do |l|
76
+ re = /(^|\\n)#{Regexp.escape(type)}/
77
+ case l
78
+ when re
79
+ "<strong>" + l.gsub(re, '').gsub("\n", '').
80
+ gsub('\n', "</strong>\n<strong>") + "</strong>"
81
+ when /^ /
82
+ l.gsub(/^./, '').gsub("\n", '').
83
+ gsub('\r', "\r").gsub('\n', "\n")
84
+ end
85
+ end.join('').split("\n").map do |l|
86
+ type + l
87
+ end
88
+ end
89
+ end
90
+ end
data/spec/dirb_spec.rb ADDED
@@ -0,0 +1,277 @@
1
+ require 'spec'
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'diffy'))
3
+
4
+ describe Diffy::Diff do
5
+
6
+ describe "diffing two files" do
7
+ def tempfile(string)
8
+ t = Tempfile.new('diffy-spec')
9
+ t.print(string)
10
+ t.flush
11
+ t.path
12
+ end
13
+
14
+ it "should accept file paths as arguments" do
15
+ string1 = "foo\nbar\nbang\n"
16
+ string2 = "foo\nbang\n"
17
+ path1, path2 = tempfile(string1), tempfile(string2)
18
+ Diffy::Diff.new(path1, path2, :source => 'files').to_s.should == <<-DIFF
19
+ foo
20
+ -bar
21
+ bang
22
+ DIFF
23
+ end
24
+
25
+ describe "with no line different" do
26
+ before do
27
+ string1 = "foo\nbar\nbang\n"
28
+ string2 = "foo\nbar\nbang\n"
29
+ @path1, @path2 = tempfile(string1), tempfile(string2)
30
+ end
31
+
32
+ it "should show everything" do
33
+ Diffy::Diff.new(@path1, @path2, :source => 'files').to_s.should == <<-DIFF
34
+ foo
35
+ bar
36
+ bang
37
+ DIFF
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "#to_s" do
43
+ describe "with no line different" do
44
+ before do
45
+ @string1 = "foo\nbar\nbang\n"
46
+ @string2 = "foo\nbar\nbang\n"
47
+ end
48
+
49
+ it "should show everything" do
50
+ Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF
51
+ foo
52
+ bar
53
+ bang
54
+ DIFF
55
+ end
56
+ end
57
+ describe "with one line different" do
58
+ before do
59
+ @string1 = "foo\nbar\nbang\n"
60
+ @string2 = "foo\nbang\n"
61
+ end
62
+
63
+ it "should show one line removed" do
64
+ Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF
65
+ foo
66
+ -bar
67
+ bang
68
+ DIFF
69
+ end
70
+
71
+ it "to_s should accept a format key" do
72
+ Diffy::Diff.new(@string1, @string2).to_s(:color).
73
+ should == " foo\n\e[31m-bar\e[0m\n bang\n"
74
+ end
75
+
76
+ it "should accept a default format option" do
77
+ old_format = Diffy::Diff.default_format
78
+ Diffy::Diff.default_format = :color
79
+ Diffy::Diff.new(@string1, @string2).to_s.
80
+ should == " foo\n\e[31m-bar\e[0m\n bang\n"
81
+ Diffy::Diff.default_format = old_format
82
+ end
83
+
84
+ it "should show one line added" do
85
+ Diffy::Diff.new(@string2, @string1).to_s.should == <<-DIFF
86
+ foo
87
+ +bar
88
+ bang
89
+ DIFF
90
+ end
91
+ end
92
+
93
+ describe "with one line changed" do
94
+ before do
95
+ @string1 = "foo\nbar\nbang\n"
96
+ @string2 = "foo\nbong\nbang\n"
97
+ end
98
+ it "should show one line added and one removed" do
99
+ Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF
100
+ foo
101
+ -bar
102
+ +bong
103
+ bang
104
+ DIFF
105
+ end
106
+ end
107
+
108
+ describe "with totally different strings" do
109
+ before do
110
+ @string1 = "foo\nbar\nbang\n"
111
+ @string2 = "one\ntwo\nthree\n"
112
+ end
113
+ it "should show one line added and one removed" do
114
+ Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF
115
+ -foo
116
+ -bar
117
+ -bang
118
+ +one
119
+ +two
120
+ +three
121
+ DIFF
122
+ end
123
+ end
124
+
125
+ describe "with a somewhat complicated diff" do
126
+ before do
127
+ @string1 = "foo\nbar\nbang\nwoot\n"
128
+ @string2 = "one\ntwo\nthree\nbar\nbang\nbaz\n"
129
+ @diff = Diffy::Diff.new(@string1, @string2)
130
+ end
131
+ it "should show one line added and one removed" do
132
+ @diff.to_s.should == <<-DIFF
133
+ -foo
134
+ +one
135
+ +two
136
+ +three
137
+ bar
138
+ bang
139
+ -woot
140
+ +baz
141
+ DIFF
142
+ end
143
+
144
+ it "should make an awesome simple html diff" do
145
+ @diff.to_s(:html_simple).should == <<-HTML
146
+ <div class="diff">
147
+ <ul>
148
+ <li class="del"><del>foo</del></li>
149
+ <li class="ins"><ins>one</ins></li>
150
+ <li class="ins"><ins>two</ins></li>
151
+ <li class="ins"><ins>three</ins></li>
152
+ <li class="unchanged"><span>bar</span></li>
153
+ <li class="unchanged"><span>bang</span></li>
154
+ <li class="del"><del>woot</del></li>
155
+ <li class="ins"><ins>baz</ins></li>
156
+ </ul>
157
+ </div>
158
+ HTML
159
+ end
160
+
161
+ it "should accept overrides to diff's options" do
162
+ @diff = Diffy::Diff.new(@string1, @string2, :diff => "--rcs")
163
+ @diff.to_s.should == <<-DIFF
164
+ d1 1
165
+ a1 3
166
+ one
167
+ two
168
+ three
169
+ d4 1
170
+ a4 1
171
+ baz
172
+ DIFF
173
+ end
174
+ end
175
+
176
+ describe "html" do
177
+
178
+ it "should highlight the changes within the line" do
179
+ @string1 = "hahaha\ntime flies like an arrow\nfoo bar\nbang baz\n"
180
+ @string2 = "hahaha\nfruit flies like a banana\nbang baz\n"
181
+ @diff = Diffy::Diff.new(@string1, @string2)
182
+ html = <<-HTML
183
+ <div class="diff">
184
+ <ul>
185
+ <li class="unchanged"><span>hahaha</span></li>
186
+ <li class="del"><del><strong>t</strong>i<strong>me</strong> flies like a<strong>n arrow</strong></del></li>
187
+ <li class="del"><del><strong>foo</strong> ba<strong>r</strong></del></li>
188
+ <li class="ins"><ins><strong>fru</strong>i<strong>t</strong> flies like a ba<strong>nana</strong></ins></li>
189
+ <li class="unchanged"><span>bang baz</span></li>
190
+ </ul>
191
+ </div>
192
+ HTML
193
+ @diff.to_s(:html).should == html
194
+ end
195
+
196
+ it "should not duplicate some lines" do
197
+ @string1 = "hahaha\ntime flies like an arrow\n"
198
+ @string2 = "hahaha\nfruit flies like a banana\nbang baz"
199
+ @diff = Diffy::Diff.new(@string1, @string2)
200
+ html = <<-HTML
201
+ <div class="diff">
202
+ <ul>
203
+ <li class="unchanged"><span>hahaha</span></li>
204
+ <li class="del"><del><strong>t</strong>i<strong>me</strong> flies like an a<strong>rrow</strong></del></li>
205
+ <li class="ins"><ins><strong>fru</strong>i<strong>t</strong> flies like a<strong> ba</strong>n<strong>ana</strong></ins></li>
206
+ <li class="ins"><ins><strong>bang</strong> <strong>b</strong>a<strong>z</strong></ins></li>
207
+ </ul>
208
+ </div>
209
+ HTML
210
+ @diff.to_s(:html).should == html
211
+ end
212
+
213
+ it "should escape html" do
214
+ @string1 = "ha<br>haha\ntime flies like an arrow\n"
215
+ @string2 = "ha<br>haha\nfruit flies like a banana\nbang baz"
216
+ @diff = Diffy::Diff.new(@string1, @string2)
217
+ html = <<-HTML
218
+ <div class="diff">
219
+ <ul>
220
+ <li class="unchanged"><span>ha&lt;br&gt;haha</span></li>
221
+ <li class="del"><del><strong>t</strong>i<strong>me</strong> flies like an a<strong>rrow</strong></del></li>
222
+ <li class="ins"><ins><strong>fru</strong>i<strong>t</strong> flies like a<strong> ba</strong>n<strong>ana</strong></ins></li>
223
+ <li class="ins"><ins><strong>bang</strong> <strong>b</strong>a<strong>z</strong></ins></li>
224
+ </ul>
225
+ </div>
226
+ HTML
227
+ @diff.to_s(:html).should == html
228
+ end
229
+
230
+ it "should highlight the changes within the line with windows style line breaks" do
231
+ @string1 = "hahaha\r\ntime flies like an arrow\r\nfoo bar\r\nbang baz\n"
232
+ @string2 = "hahaha\r\nfruit flies like a banana\r\nbang baz\n"
233
+ @diff = Diffy::Diff.new(@string1, @string2)
234
+ html = <<-HTML
235
+ <div class="diff">
236
+ <ul>
237
+ <li class="unchanged"><span>hahaha</span></li>
238
+ <li class="del"><del><strong>t</strong>i<strong>me</strong> flies like a<strong>n arrow</strong></del></li>
239
+ <li class="del"><del><strong>foo</strong> ba<strong>r</strong></del></li>
240
+ <li class="ins"><ins><strong>fru</strong>i<strong>t</strong> flies like a ba<strong>nana</strong></ins></li>
241
+ <li class="unchanged"><span>bang baz</span></li>
242
+ </ul>
243
+ </div>
244
+ HTML
245
+ @diff.to_s(:html).should == html
246
+ end
247
+ end
248
+
249
+ it "should escape diffed html in html output" do
250
+ diff = Diffy::Diff.new("<script>alert('bar')</script>", "<script>alert('foo')</script>").to_s(:html)
251
+ diff.should include('&lt;script&gt;')
252
+ diff.should_not include('<script>')
253
+ end
254
+
255
+ it "should be easy to generate custom format" do
256
+ Diffy::Diff.new("foo\nbar\n", "foo\nbar\nbaz\n").map do |line|
257
+ case line
258
+ when /^\+/ then "line #{line.chomp} added"
259
+ when /^-/ then "line #{line.chomp} removed"
260
+ end
261
+ end.compact.join.should == "line +baz added"
262
+ end
263
+
264
+ it "should let you iterate over chunks instead of lines" do
265
+ Diffy::Diff.new("foo\nbar\n", "foo\nbar\nbaz\n").each_chunk.map do |chunk|
266
+ chunk
267
+ end.should == [" foo\n bar\n", "+baz\n"]
268
+ end
269
+
270
+ it "should allow chaining enumerable methods" do
271
+ Diffy::Diff.new("foo\nbar\n", "foo\nbar\nbaz\n").each.map do |line|
272
+ line
273
+ end.should == [" foo\n", " bar\n", "+baz\n"]
274
+ end
275
+ end
276
+ end
277
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diffy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
- - 1
8
- - 1
7
+ - 2
9
8
  - 0
10
- version: 1.1.0
9
+ - 0
10
+ version: 2.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sam Goldstein
@@ -36,7 +36,11 @@ files:
36
36
  - Rakefile
37
37
  - VERSION
38
38
  - dirb.gemspec
39
- - spec/diffy_spec.rb
39
+ - lib/diffy.rb
40
+ - lib/diffy/diff.rb
41
+ - lib/diffy/format.rb
42
+ - lib/diffy/html_formatter.rb
43
+ - spec/dirb_spec.rb
40
44
  has_rdoc: true
41
45
  homepage: http://github.com/samg/diffy/tree/master
42
46
  licenses: []
@@ -73,4 +77,4 @@ signing_key:
73
77
  specification_version: 3
74
78
  summary: A convenient way to diff string in ruby
75
79
  test_files:
76
- - spec/diffy_spec.rb
80
+ - spec/dirb_spec.rb
data/spec/diffy_spec.rb DELETED
@@ -1,155 +0,0 @@
1
- require 'spec'
2
- require File.join(File.dirname(__FILE__), '..', 'lib', 'diffy')
3
-
4
- describe Diffy::Diff do
5
- describe "#to_s" do
6
- describe "with no line different" do
7
- before do
8
- @string1 = "foo\nbar\nbang\n"
9
- @string2 = "foo\nbar\nbang\n"
10
- end
11
-
12
- it "should show everything" do
13
- Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF
14
- foo
15
- bar
16
- bang
17
- DIFF
18
- end
19
- end
20
- describe "with one line different" do
21
- before do
22
- @string1 = "foo\nbar\nbang\n"
23
- @string2 = "foo\nbang\n"
24
- end
25
-
26
- it "should show one line removed" do
27
- Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF
28
- foo
29
- -bar
30
- bang
31
- DIFF
32
- end
33
-
34
- it "to_s should accept a format key" do
35
- Diffy::Diff.new(@string1, @string2).to_s(:color).
36
- should == " foo\n\e[31m-bar\e[0m\n bang\n"
37
- end
38
-
39
- it "should accept a default format option" do
40
- old_format = Diffy::Diff.default_format
41
- Diffy::Diff.default_format = :color
42
- Diffy::Diff.new(@string1, @string2).to_s.
43
- should == " foo\n\e[31m-bar\e[0m\n bang\n"
44
- Diffy::Diff.default_format = old_format
45
- end
46
-
47
- it "should show one line added" do
48
- Diffy::Diff.new(@string2, @string1).to_s.should == <<-DIFF
49
- foo
50
- +bar
51
- bang
52
- DIFF
53
- end
54
- end
55
-
56
- describe "with one line changed" do
57
- before do
58
- @string1 = "foo\nbar\nbang\n"
59
- @string2 = "foo\nbong\nbang\n"
60
- end
61
- it "should show one line added and one removed" do
62
- Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF
63
- foo
64
- -bar
65
- +bong
66
- bang
67
- DIFF
68
- end
69
- end
70
-
71
- describe "with totally different strings" do
72
- before do
73
- @string1 = "foo\nbar\nbang\n"
74
- @string2 = "one\ntwo\nthree\n"
75
- end
76
- it "should show one line added and one removed" do
77
- Diffy::Diff.new(@string1, @string2).to_s.should == <<-DIFF
78
- -foo
79
- -bar
80
- -bang
81
- +one
82
- +two
83
- +three
84
- DIFF
85
- end
86
- end
87
-
88
- describe "with a somewhat complicated diff" do
89
- before do
90
- @string1 = "foo\nbar\nbang\nwoot\n"
91
- @string2 = "one\ntwo\nthree\nbar\nbang\nbaz\n"
92
- @diff = Diffy::Diff.new(@string1, @string2)
93
- end
94
- it "should show one line added and one removed" do
95
- @diff.to_s.should == <<-DIFF
96
- -foo
97
- +one
98
- +two
99
- +three
100
- bar
101
- bang
102
- -woot
103
- +baz
104
- DIFF
105
- end
106
-
107
- it "should make an awesome html diff" do
108
- @diff.to_s(:html).should == <<-HTML
109
- <div class="diff">
110
- <ul>
111
- <li class="del"><del>foo</del></li>
112
- <li class="ins"><ins>one</ins></li>
113
- <li class="ins"><ins>two</ins></li>
114
- <li class="ins"><ins>three</ins></li>
115
- <li class="unchanged"><span>bar</span></li>
116
- <li class="unchanged"><span>bang</span></li>
117
- <li class="del"><del>woot</del></li>
118
- <li class="ins"><ins>baz</ins></li>
119
- </ul>
120
- </div>
121
- HTML
122
- end
123
-
124
- it "should accept overrides to diff's options" do
125
- @diff = Diffy::Diff.new(@string1, @string2, "--rcs")
126
- @diff.to_s.should == <<-DIFF
127
- d1 1
128
- a1 3
129
- one
130
- two
131
- three
132
- d4 1
133
- a4 1
134
- baz
135
- DIFF
136
- end
137
- end
138
-
139
- it "should escape diffed html in html output" do
140
- diff = Diffy::Diff.new("<script>alert('bar')</script>", "<script>alert('foo')</script>").to_s(:html)
141
- diff.should include('&lt;script&gt;')
142
- diff.should_not include('<script>')
143
- end
144
-
145
- it "should be easy to generate custom format" do
146
- Diffy::Diff.new("foo\nbar\n", "foo\nbar\nbaz\n").map do |line|
147
- case line
148
- when /^\+/ then "line #{line.chomp} added"
149
- when /^-/ then "line #{line.chomp} removed"
150
- end
151
- end.compact.join.should == "line +baz added"
152
- end
153
- end
154
- end
155
-