diff options
author | Johan Sørensen <johan@johansorensen.com> | 2009-03-05 14:31:24 +0100 |
---|---|---|
committer | Johan Sørensen <johan@johansorensen.com> | 2009-04-22 15:15:50 +0200 |
commit | c630fb4c676e147d1134a9faa2a76098f3ba955d (patch) | |
tree | 8f698ee1a74f05860c5207e3d87598352948eac2 /vendor/diff-display | |
parent | 243989c502ffaede9a651ca67c9d132931a36d85 (diff) | |
download | gitorious-mainline-outdated-c630fb4c676e147d1134a9faa2a76098f3ba955d.zip gitorious-mainline-outdated-c630fb4c676e147d1134a9faa2a76098f3ba955d.tar.gz gitorious-mainline-outdated-c630fb4c676e147d1134a9faa2a76098f3ba955d.tar.bz2 |
Updated Diff::Display with support for inline change support
Diffstat (limited to 'vendor/diff-display')
29 files changed, 484 insertions, 433 deletions
diff --git a/vendor/diff-display/.gitignore b/vendor/diff-display/.gitignore new file mode 100644 index 0000000..496ee2c --- /dev/null +++ b/vendor/diff-display/.gitignore @@ -0,0 +1 @@ +.DS_Store
\ No newline at end of file diff --git a/vendor/diff-display/README.txt b/vendor/diff-display/README.txt index fffd306..d56f429 100644 --- a/vendor/diff-display/README.txt +++ b/vendor/diff-display/README.txt @@ -49,7 +49,7 @@ See Diff::Renderer::Base for what methods your callback needs to implement Git Repository =============== -http://GitoriousConfig['gitorious_host']/projects/diff-display/ +http://gitorious.org/projects/diff-display/ License diff --git a/vendor/diff-display/Rakefile b/vendor/diff-display/Rakefile index e469154..98e85c3 100644 --- a/vendor/diff-display/Rakefile +++ b/vendor/diff-display/Rakefile @@ -1,4 +1,10 @@ -require 'config/requirements'
-require 'config/hoe' # setup Hoe + all gem configuration
+# encoding: utf-8
-Dir['tasks/**/*.rake'].each { |rake| load rake }
\ No newline at end of file +require 'rubygems'
+require 'hoe'
+require './lib/diff-display.rb'
+
+Hoe.new('diff-display', Diff::Display::VERSION::STRING) do |p|
+ p.rubyforge_name = 'hoe_testx'
+ p.developer('Johan Sørensen', 'johan@johansorensen.com')
+end
diff --git a/vendor/diff-display/config/hoe.rb b/vendor/diff-display/config/hoe.rb deleted file mode 100644 index bf36c45..0000000 --- a/vendor/diff-display/config/hoe.rb +++ /dev/null @@ -1,71 +0,0 @@ -require 'diff/display/version' - -AUTHOR = ['Johan Sørensen', 'Marcel Molina Jr.'] # can also be an array of Authors -EMAIL = "johan@johansorensen.com" -DESCRIPTION = "Displays a unified diffs in various (user-definable) ways" -GEM_NAME = 'diff-display' # what ppl will type to install your gem -RUBYFORGE_PROJECT = 'diff-display' # The unix name for your project -HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org" -DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}" - -@config_file = "~/.rubyforge/user-config.yml" -@config = nil -RUBYFORGE_USERNAME = "unknown" -def rubyforge_username - unless @config - begin - @config = YAML.load(File.read(File.expand_path(@config_file))) - rescue - puts <<-EOS -ERROR: No rubyforge config file found: #{@config_file} -Run 'rubyforge setup' to prepare your env for access to Rubyforge - - See http://newgem.rubyforge.org/rubyforge.html for more details - EOS - exit - end - end - RUBYFORGE_USERNAME.replace @config["username"] -end - - -REV = nil -# UNCOMMENT IF REQUIRED: -# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil -VERS = Diff::Display::VERSION::STRING + (REV ? ".#{REV}" : "") -RDOC_OPTS = ['--quiet', '--title', 'diff-display documentation', - "--opname", "index.html", - "--line-numbers", - "--main", "README", - "--inline-source"] - -class Hoe - def extra_deps - @extra_deps.reject! { |x| Array(x).first == 'hoe' } - @extra_deps - end -end - -# Generate all the Rake tasks -# Run 'rake -T' to see list of generated tasks (from gem root directory) -hoe = Hoe.new(GEM_NAME, VERS) do |p| - p.author = AUTHOR - p.description = DESCRIPTION - p.email = EMAIL - p.summary = DESCRIPTION - p.url = HOMEPATH - p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT - p.test_globs = ["test/**/test_*.rb"] - p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean. - - # == Optional - p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n") - #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ] - - #p.spec_extras = {} # A hash of extra values to set in the gemspec. - -end - -CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n") -PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}" -hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc') -hoe.rsync_args = '-av --delete --ignore-errors'
\ No newline at end of file diff --git a/vendor/diff-display/config/requirements.rb b/vendor/diff-display/config/requirements.rb deleted file mode 100644 index b6e36b2..0000000 --- a/vendor/diff-display/config/requirements.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'fileutils' -include FileUtils - -require 'rubygems' -%w[rake hoe newgem rubigen].each do |req_gem| - begin - require req_gem - rescue LoadError - puts "This Rakefile requires the '#{req_gem}' RubyGem." - puts "Installation: gem install #{req_gem} -y" - exit - end -end - -$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib])) - -require 'diff-display'
\ No newline at end of file diff --git a/vendor/diff-display/lib/diff/display/data_structure.rb b/vendor/diff-display/lib/diff/display/data_structure.rb index e9696eb..176eaf9 100644 --- a/vendor/diff-display/lib/diff/display/data_structure.rb +++ b/vendor/diff-display/lib/diff/display/data_structure.rb @@ -9,22 +9,41 @@ module Diff diff = "" each do |block| block.each do |line| + line_str = line.expand_inline_changes_with(nil, nil) case line when HeaderLine - diff << "#{line}\n" + diff << "#{line_str}\n" when UnModLine - diff << " #{line}\n" + diff << " #{line_str}\n" when SepLine diff << "\n" when AddLine - diff << "+#{line}\n" + diff << "+#{line_str}\n" when RemLine - diff << "-#{line}\n" + diff << "-#{line_str}\n" + when NonewlineLine + diff << line_str end end end diff.chomp end + + def debug + demodularize = Proc.new {|obj| obj.class.name[/\w+$/]} + each do |diff_block| + print "-" * 40, ' ', demodularize.call(diff_block) + puts + puts diff_block.map {|line| + # "%5d" % line.old_number + + "%8s" % "[#{line.old_number || '.'} #{line.new_number || '.'}]" + + " [#{demodularize.call(line)}#{'(i)' if line.inline_changes?}]" + + line + }.join("\n") + puts "-" * 40, ' ' + end + nil + end end # Every line from the passed in diff gets transformed into an instance of @@ -34,18 +53,22 @@ module Diff # a SepLine class which represents all the lines that aren't part of the diff. class Line < String class << self - def add(line, line_number) - AddLine.new(line, line_number) + def add(line, line_number, inline = false) + AddLine.new(line, line_number, inline) end - def rem(line, line_number) - RemLine.new(line, line_number) + def rem(line, line_number, inline = false) + RemLine.new(line, line_number, inline) end def unmod(line, old_number, new_number) UnModLine.new(line, old_number, new_number) end + def nonewline(line) + NonewlineLine.new(line) + end + def header(line) HeaderLine.new(line) end @@ -54,26 +77,72 @@ module Diff def initialize(line, old_number = nil, new_number = nil) super(line) @old_number, @new_number = old_number, new_number + @inline = false end attr_reader :old_number, :new_number + def identifier + self.class.name[/\w+$/].gsub(/Line$/, "").downcase.to_sym + end + + def inline_changes? + # Is set in the AddLine+RemLine subclasses + @inline + end + + # returns the prefix, middle and postfix parts of a line with inline changes + def segments + return self.dup unless inline_changes? + prefix, changed = self.dup.split('\\0') + changed, postfix = changed.split('\\1') + [prefix, changed, postfix] + end + + # Expand any inline changes with +prefix+ and +postfix+ + def expand_inline_changes_with(prefix, postfix) + return self.dup unless inline_changes? + str = self.dup + str.sub!('\\0', prefix.to_s) + str.sub!('\\1', postfix.to_s) + str + end + def inspect %Q{#<#{self.class.name} [#{old_number.inspect}-#{new_number.inspect}] "#{self}">} end end + # class AddLine < Line + # def initialize(line, line_number) + # super(line, nil, line_number) + # end + # end + # + # class RemLine < Line + # def initialize(line, line_number) + # super(line, line_number, nil) + # end + # end class AddLine < Line - def initialize(line, line_number) + def initialize(line, line_number, inline = false) super(line, nil, line_number) + @inline = inline end end class RemLine < Line - def initialize(line, line_number) + def initialize(line, line_number, inline = false) super(line, line_number, nil) + @inline = inline end end + class NonewlineLine < Line + def initialize(line = '\\ No newline at end of file') + super(line) + end + end + class UnModLine < Line def initialize(line, old_number, new_number) super(line, old_number, new_number) @@ -107,6 +176,7 @@ module Diff def mod; ModBlock.new end def unmod; UnModBlock.new end def header; HeaderBlock.new end + def nonewline; NonewlineBlock.new end end end @@ -117,6 +187,7 @@ module Diff class UnModBlock < Block; end class SepBlock < Block; end class HeaderBlock < Block; end + class NonewlineBlock < Block; end #:startdoc:# end end
\ No newline at end of file diff --git a/vendor/diff-display/lib/diff/display/unified/generator.rb b/vendor/diff-display/lib/diff/display/unified/generator.rb index 1059172..91b1d16 100644 --- a/vendor/diff-display/lib/diff/display/unified/generator.rb +++ b/vendor/diff-display/lib/diff/display/unified/generator.rb @@ -12,19 +12,19 @@ module Diff::Display # Extracts the line number info for a given diff section LINE_NUM_RE = /^@@ [+-]([0-9]+)(?:,([0-9]+))? [+-]([0-9]+)(?:,([0-9]+))? @@/ - LINE_TYPES = {'+' => :add, '-' => :rem, ' ' => :unmod} + LINE_TYPES = {'+' => :add, '-' => :rem, ' ' => :unmod, '\\' => :nonewline} # Runs the generator on a diff and returns a Data object def self.run(udiff) raise ArgumentError, "Object must be enumerable" unless udiff.respond_to?(:each_line) generator = new udiff.each_line {|line| generator.process(line.chomp)} + generator.finish generator.data end def initialize @buffer = [] - @prev_buffer = [] @line_type = nil @prev_line_type = nil @offset = [0, 0] @@ -35,26 +35,25 @@ module Diff::Display # Finishes up with the generation and returns the Data object (could # probably use a better name...maybe just #data?) def data - close @data end - # Operates on a single line from the diff and passes along the - # collected data to the appropriate method for further processing. The - # cycle of processing is in general: - # - # process --> identify_block --> process_block --> process_line - # + # This method is called once the generator is done with the unified + # diff. It is a finalizer of sorts. By the time it is called all data + # has been collected and processed. + def finish + # certain things could be set now that processing is done + #identify_block + end + def process(line) if is_header_line?(line) - identify_block push Block.header current_block << Line.header(line) return end if line =~ LINE_NUM_RE - identify_block push Block.header current_block << Line.header(line) add_separator unless @offset[0].zero? @@ -63,23 +62,19 @@ module Diff::Display return end - new_line_type, line = LINE_TYPES[car(line)], cdr(line) + @line_type, line = LINE_TYPES[car(line)], cdr(line) - # Add line to the buffer if it's the same diff line type - # as the previous line - # - # e.g. - # - # + This is a new line - # + As is this one - # + And yet another one... - # - if new_line_type.eql?(@line_type) - @buffer.push(line) - else - identify_block - @buffer = [line] - @line_type = new_line_type + if @line_type == :add && @prev_line_type == :rem + @offset[0] -= 1 + @buffer.push current_block.pop + @buffer.push line + process_block(:mod, false) + return + end + + if LINE_TYPES.values.include?(@line_type) + @buffer.push(line.to_s) + process_block(@line_type, true) end end @@ -89,32 +84,82 @@ module Diff::Display return true if ['+++ ', '--- '].include?(line[0,4]) return true if line =~ /^(new|delete) file mode [0-9]+$/ return true if line =~ /^diff \-\-git/ - return true if line =~ /^index \w+\.\.\w+ [0-9]+$/ + return true if line =~ /^index \w+\.\.\w+( [0-9]+)?$/i false end + + def process_block(diff_line_type, isnew = false) + @data.pop unless isnew + push Block.send(diff_line_type) + + current_line = @buffer.pop + return unless current_line + + # \\ No newline at end of file + if diff_line_type == :nonewline + current_block << Line.nonewline('\\ No newline at end of file') + return + end + + if isnew + process_line(current_line, diff_line_type) + else + process_lines_with_differences(@buffer.shift, current_line) + raise "buffer exceeded #{@buffer.inspect}" unless @buffer.empty? + end + end - def identify_block - if LINE_TYPES.values.include?(@line_type) - process_block(@line_type) + def process_line(line, type, inline = false) + case type + when :add + @offset[1] += 1 + current_block << Line.send(type, line, @offset[1], inline) + when :rem + @offset[0] += 1 + current_block << Line.send(type, line, @offset[0], inline) + # when :rmod + # @offset[0] += 1 + # @offset[1] += 1 # TODO: is that really correct? + # current_block << Line.send(@prev_line_type, line, @offset[0]) + when :unmod + @offset[0] += 1 + @offset[1] += 1 + current_block << Line.send(type, line, *@offset) end + @prev_line_type = type + end + + # TODO Needs a better name...it does process a line (two in fact) but + # its primary function is to add a Rem and an Add pair which + # potentially have inline changes + def process_lines_with_differences(oldline, newline) + start, ending = get_change_extent(oldline, newline) + + # - + line = inline_diff(oldline, start, ending) + process_line(line, :rem, true) - @prev_line_type = nil + # + + line = inline_diff(newline, start, ending) + process_line(line, :add, true) end - def process_block(diff_line_type) - push Block.send(diff_line_type) - unroll_buffer + # Inserts string formating characters around the section of a string + # that differs internally from another line so that the Line class + # can insert the desired formating + def inline_diff(line, start, ending) + if start != 0 || ending != 0 + last = ending + line.length + str = line[0...start] + '\0' + line[start...last] + '\1' + line[last...line.length] + end + str || line end - + def add_separator push SepBlock.new current_block << SepLine.new end - def extract_change(line, start, ending) - line.size > (start - ending) ? line[start...ending] : '' - end - def car(line) line[0,1] end @@ -133,36 +178,6 @@ module Diff::Display @data.push line end - def prev_buffer - @prev_buffer - end - - def unroll_buffer - return if @buffer.empty? - @buffer.each do |line| - case @line_type - when :add - @offset[1] += 1 - current_block << Line.send(@line_type, line, @offset[1]) - when :rem - @offset[0] += 1 - current_block << Line.send(@line_type, line, @offset[0]) - when :unmod - @offset[0] += 1 - @offset[1] += 1 - current_block << Line.send(@line_type, line, *@offset) - end - end - end - - # This method is called once the generator is done with the unified - # diff. It is a finalizer of sorts. By the time it is called all data - # has been collected and processed. - def close - # certain things could be set now that processing is done - identify_block - end - # Determines the extent of differences between two string. Returns # an array containing the offset at which changes start, and then # negative offset at which the chnages end. If the two strings have diff --git a/vendor/diff-display/spec/api_spec.rb b/vendor/diff-display/spec/api_spec.rb deleted file mode 100644 index ed82038..0000000 --- a/vendor/diff-display/spec/api_spec.rb +++ /dev/null @@ -1,10 +0,0 @@ -require File.dirname(__FILE__) + '/spec_helper.rb' - -describe "API acceptance specs" do - - it "has a simple API" do - diff = Diff::Display::Unified.new(load_diff("simple")) - diff.render(Diff::Renderer::Base.new) - end - -end
\ No newline at end of file diff --git a/vendor/diff-display/spec/data_structure_spec.rb b/vendor/diff-display/spec/data_structure_spec.rb deleted file mode 100644 index d5a726f..0000000 --- a/vendor/diff-display/spec/data_structure_spec.rb +++ /dev/null @@ -1,81 +0,0 @@ -require File.dirname(__FILE__) + '/spec_helper.rb' - -describe "Diff::Display data structures" do - - describe "Data" do - it "behaves like an array" do - data = Diff::Display::Data.new - data << "foo" - data.push "bar" - data.should == ["foo", "bar"] - end - end - - describe "Line" do - it "initializes with an old line number" do - line = Diff::Display::Line.new("foo", 12) - line.old_number.should == 12 - end - - it "initializes with numbers" do - line = Diff::Display::Line.new("foo", 12, 13) - line.old_number.should == 12 - line.new_number.should == 13 - end - - it "has a class method for creating an AddLine" do - line = Diff::Display::Line.add("foo", 7) - line.should be_instance_of(Diff::Display::AddLine) - end - - it "has a class method for creating a RemLine" do - line = Diff::Display::Line.rem("foo", 7) - line.should be_instance_of(Diff::Display::RemLine) - end - - it "has a class method for creating a UnModLine" do - line = Diff::Display::Line.unmod("foo", 7, 8) - line.should be_instance_of(Diff::Display::UnModLine) - end - - it "has a class method for creating a HeaderLine" do - line = Diff::Display::Line.header("foo") - line.should be_instance_of(Diff::Display::HeaderLine) - end - end - - describe "Block" do - it "behaves like an array" do - block = Diff::Display::Block.new - block.push 1,2,3 - block.size.should == 3 - block.should == [1,2,3] - end - - it "has class method for creating an AddBlock" do - block = Diff::Display::Block.add - block.should be_instance_of(Diff::Display::AddBlock) - end - - it "has class method for creating an RemBlock" do - block = Diff::Display::Block.rem - block.should be_instance_of(Diff::Display::RemBlock) - end - - it "has class method for creating an ModBlock" do - block = Diff::Display::Block.mod - block.should be_instance_of(Diff::Display::ModBlock) - end - - it "has class method for creating an UnModBlock" do - block = Diff::Display::Block.unmod - block.should be_instance_of(Diff::Display::UnModBlock) - end - - it "has class method for creating an HeaderBlock" do - block = Diff::Display::Block.header - block.should be_instance_of(Diff::Display::HeaderBlock) - end - end - -end diff --git a/vendor/diff-display/spec/generator_spec.rb b/vendor/diff-display/spec/generator_spec.rb deleted file mode 100644 index bff1024..0000000 --- a/vendor/diff-display/spec/generator_spec.rb +++ /dev/null @@ -1,86 +0,0 @@ -require File.dirname(__FILE__) + '/spec_helper.rb' - -describe Diff::Display::Unified::Generator do - - before(:each) do - @generator = Diff::Display::Unified::Generator.new - end - - it "Generator.run raises if doesn't get a Enumerable object" do - proc { - Diff::Display::Unified::Generator.run(nil) - }.should raise_error(ArgumentError) - end - - it "Generator.run processes each line in the diff" do - Diff::Display::Unified::Generator.expects(:new).returns(@generator) - @generator.expects(:process).with("foo") - @generator.expects(:process).with("bar") - Diff::Display::Unified::Generator.run("foo\nbar") - end - - it "Generator.run returns the data" do - Diff::Display::Unified::Generator.expects(:new).returns(@generator) - generated = Diff::Display::Unified::Generator.run("foo\nbar") - generated.should be_instance_of(Diff::Display::Data) - end - - it "the returned that object is in parity with the diff" do - %w[simple only_add only_rem multiple_adds_after_rem].each do |diff| - data = Diff::Display::Unified::Generator.run(load_diff(diff)) - data.to_diff.should == load_diff(diff).chomp - end - end - - describe "edgecase bugs" do - it "multiple rems and an add is in parity" do - diff_data = load_diff("multiple_rems_then_add") - data = Diff::Display::Unified::Generator.run(diff_data) - data.to_diff.should == diff_data.chomp - end - - #it "doesn't parse linenumbers that isn't part if the diff" do - # line_numbers_for(:pseudo_recursive).compact.should == (1..14).to_a - #end - end - - describe "line numbering" do - it "numbers correctly for multiple_adds_after_rem" do - line_numbers_for(:multiple_adds_after_rem).should == [ - [193, 193], - [194, nil], - [nil, 194], - [nil, 195], - [nil, 196], - [nil, 197], - [nil, 198], - [195, 199] - ] - end - - it "numbers correctly for simple" do - line_numbers_for(:simple).should == [ - [1, 1], - [2, 2], - [3, nil], - [4, nil], - [nil, 3], - [nil, 4], - [nil, 5], - ] - end - end - - def line_numbers_for(diff) - diff_data = load_diff(diff) - data = Diff::Display::Unified::Generator.run(diff_data) - linenos = [] - data.each do |blk| - blk.each do |line| - next if line.class == Diff::Display::HeaderLine - linenos << [line.old_number, line.new_number] - end - end - linenos - end -end diff --git a/vendor/diff-display/spec/renderer/diff_spec.rb b/vendor/diff-display/spec/renderer/diff_spec.rb deleted file mode 100644 index 2c7b862..0000000 --- a/vendor/diff-display/spec/renderer/diff_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -require File.dirname(__FILE__) + '/../spec_helper.rb' - -describe Diff::Renderer::Diff do - it "renders a diff back to its original state" do - data = Diff::Display::Unified::Generator.run(load_diff("simple")) - base_renderer = Diff::Renderer::Diff.new - base_renderer.render(data).should == load_diff("simple") - end -end diff --git a/vendor/diff-display/spec/spec.opts b/vendor/diff-display/spec/spec.opts deleted file mode 100644 index cf6add7..0000000 --- a/vendor/diff-display/spec/spec.opts +++ /dev/null @@ -1 +0,0 @@ ---colour
\ No newline at end of file diff --git a/vendor/diff-display/spec/spec_helper.rb b/vendor/diff-display/spec/spec_helper.rb deleted file mode 100644 index 5c91155..0000000 --- a/vendor/diff-display/spec/spec_helper.rb +++ /dev/null @@ -1,20 +0,0 @@ -begin - require 'spec' -rescue LoadError - require 'rubygems' - gem 'rspec' - require 'spec' -end - -require File.dirname(__FILE__) + "/../lib/diff-display" - -module DiffFixtureHelper - def load_diff(name) - File.read(File.dirname(__FILE__) + "/fixtures/#{name}.diff") - end -end - -Spec::Runner.configure do |config| - config.mock_with :mocha - config.include DiffFixtureHelper -end
\ No newline at end of file diff --git a/vendor/diff-display/tasks/rspec.rake b/vendor/diff-display/tasks/rspec.rake deleted file mode 100644 index b256d1c..0000000 --- a/vendor/diff-display/tasks/rspec.rake +++ /dev/null @@ -1,21 +0,0 @@ -begin - require 'spec' -rescue LoadError - require 'rubygems' - require 'spec' -end -begin - require 'spec/rake/spectask' -rescue LoadError - puts <<-EOS -To use rspec for testing you must install rspec gem: - gem install rspec -EOS - exit(0) -end - -desc "Run the specs under spec/models" -Spec::Rake::SpecTask.new do |t| - t.spec_opts = ['--options', "spec/spec.opts"] - t.spec_files = FileList['spec/*_spec.rb'] -end diff --git a/vendor/diff-display/spec/fixtures/big.diff b/vendor/diff-display/test/fixtures/big.diff index b1918e4..a3c9eb6 100644 --- a/vendor/diff-display/spec/fixtures/big.diff +++ b/vendor/diff-display/test/fixtures/big.diff @@ -1,5 +1,3 @@ -Index: unified.rb -=================================================================== --- unified.rb (revision 620) +++ unified.rb (revision 644) @@ -1,298 +1,390 @@ diff --git a/vendor/diff-display/spec/fixtures/multiple_adds_after_rem.diff b/vendor/diff-display/test/fixtures/multiple_adds_after_rem.diff index 07682d2..07682d2 100644 --- a/vendor/diff-display/spec/fixtures/multiple_adds_after_rem.diff +++ b/vendor/diff-display/test/fixtures/multiple_adds_after_rem.diff diff --git a/vendor/diff-display/spec/fixtures/multiple_rems_then_add.diff b/vendor/diff-display/test/fixtures/multiple_rems_then_add.diff index f5fd3f8..f5fd3f8 100644 --- a/vendor/diff-display/spec/fixtures/multiple_rems_then_add.diff +++ b/vendor/diff-display/test/fixtures/multiple_rems_then_add.diff diff --git a/vendor/diff-display/spec/fixtures/only_add.diff b/vendor/diff-display/test/fixtures/only_add.diff index b95d705..b95d705 100644 --- a/vendor/diff-display/spec/fixtures/only_add.diff +++ b/vendor/diff-display/test/fixtures/only_add.diff diff --git a/vendor/diff-display/spec/fixtures/only_rem.diff b/vendor/diff-display/test/fixtures/only_rem.diff index 42c6d8f..42c6d8f 100644 --- a/vendor/diff-display/spec/fixtures/only_rem.diff +++ b/vendor/diff-display/test/fixtures/only_rem.diff diff --git a/vendor/diff-display/spec/fixtures/pseudo_recursive.diff b/vendor/diff-display/test/fixtures/pseudo_recursive.diff index d9b635d..d9b635d 100644 --- a/vendor/diff-display/spec/fixtures/pseudo_recursive.diff +++ b/vendor/diff-display/test/fixtures/pseudo_recursive.diff diff --git a/vendor/diff-display/spec/fixtures/simple.diff b/vendor/diff-display/test/fixtures/simple.diff index e2b9909..e2b9909 100644 --- a/vendor/diff-display/spec/fixtures/simple.diff +++ b/vendor/diff-display/test/fixtures/simple.diff diff --git a/vendor/diff-display/test/fixtures/simple_oneliner.diff b/vendor/diff-display/test/fixtures/simple_oneliner.diff new file mode 100644 index 0000000..9ae4561 --- /dev/null +++ b/vendor/diff-display/test/fixtures/simple_oneliner.diff @@ -0,0 +1,7 @@ +--- a.txt 2009-03-04 17:20:15.000000000 +0100 ++++ b.txt 2009-03-04 17:20:33.000000000 +0100 +@@ -1,3 +1,3 @@ + foo +-bar ++baz + baz diff --git a/vendor/diff-display/test/test_api.rb b/vendor/diff-display/test/test_api.rb new file mode 100644 index 0000000..49a25cf --- /dev/null +++ b/vendor/diff-display/test/test_api.rb @@ -0,0 +1,12 @@ +# encoding: utf-8 + +require File.dirname(__FILE__) + "/test_helper" + +class TestApi < Test::Unit::TestCase + include DiffFixtureHelper + + def test_it_has_a_simple_API + diff = Diff::Display::Unified.new(load_diff("simple")) + diff.render(Diff::Renderer::Base.new) + end +end
\ No newline at end of file diff --git a/vendor/diff-display/spec/renderer/base_spec.rb b/vendor/diff-display/test/test_base_renderer.rb index f20b32b..274f829 100644 --- a/vendor/diff-display/spec/renderer/base_spec.rb +++ b/vendor/diff-display/test/test_base_renderer.rb @@ -1,98 +1,101 @@ -require File.dirname(__FILE__) + '/../spec_helper.rb' +# encoding: utf-8 -describe Diff::Renderer::Base do +require File.dirname(__FILE__) + "/test_helper" + +class TestBaseRenderer < Test::Unit::TestCase + include DiffFixtureHelper - before(:each) do + def setup @data = Diff::Display::Unified::Generator.run(load_diff("big")) @base_renderer = Diff::Renderer::Base.new end - it "classifies a classname" do - @base_renderer.send(:classify, Diff::Display::RemBlock.new).should == "remblock" + def test_it_classifies_a_classname + assert_equal "remblock", @base_renderer.send(:classify, Diff::Display::RemBlock.new) end - it "calls the before_headerblock" do + def test_it_calls_the_before_headerblock @base_renderer.expects(:before_headerblock).at_least_once @base_renderer.render(@data) end - # it "calls the before_sepblock" do + # def test_it_calls_the_before_sepblock # @base_renderer.expects(:before_sepblock).at_least_once # @base_renderer.render(@data) # end - # it "calls the before_modblock" do + # def test_it_calls_the_before_modblock # @base_renderer.expects(:before_modblock).at_least_once # @base_renderer.render(@data) # end - it "calls the before_unmodblock" do + def test_calls_the_before_unmodblock @base_renderer.expects(:before_unmodblock).at_least_once @base_renderer.render(@data) end - it "calls the before_addblock" do + def test_should_calls_the_before_addblock @base_renderer.expects(:before_addblock).at_least_once @base_renderer.render(@data) end - it "calls the before_remblock" do + def test_calls_the_before_remblock @base_renderer.expects(:before_remblock).at_least_once @base_renderer.render(@data) end - it "calls headerline" do + def test_calls_headerline @base_renderer.expects(:headerline).at_least_once @base_renderer.render(@data) end - it "calls unmodline" do + def test_calls_unmodline @base_renderer.expects(:unmodline).at_least_once @base_renderer.render(@data) end - it "calls addline" do + def test_calls_addline @base_renderer.expects(:addline).at_least_once @base_renderer.render(@data) end - it "calls remline" do + def test_calls_remline @base_renderer.expects(:remline).at_least_once @base_renderer.render(@data) end - it "calls the after_headerblock" do + def test_calls_the_after_headerblock @base_renderer.expects(:after_headerblock).at_least_once @base_renderer.render(@data) end - # it "calls the after_sepblock" do + # def test_calls_the_after_sepblock # @base_renderer.expects(:after_sepblock).at_least_once # @base_renderer.render(@data) # end - # it "calls the after_modblock" do + # def test_calls_the_after_modblock # @base_renderer.expects(:after_modblock).at_least_once # @base_renderer.render(@data) # end - it "calls the after_unmodblock" do + def test_calls_the_after_unmodblock @base_renderer.expects(:after_unmodblock).at_least_once @base_renderer.render(@data) end - it "calls the after_addblock" do + def test_calls_the_after_addblock @base_renderer.expects(:after_addblock).at_least_once @base_renderer.render(@data) end - it "calls the after_remblock" do + def test_calls_the_after_remblock @base_renderer.expects(:after_remblock).at_least_once @base_renderer.render(@data) end - it "renders a basic datastructure" do + def test_renders_a_basic_datastructure output = @base_renderer.render(@data) - output.should_not == nil + assert_not_equal nil, output end -end
\ No newline at end of file +end diff --git a/vendor/diff-display/test/test_datastructure.rb b/vendor/diff-display/test/test_datastructure.rb new file mode 100644 index 0000000..6073010 --- /dev/null +++ b/vendor/diff-display/test/test_datastructure.rb @@ -0,0 +1,89 @@ +# encoding: utf-8 + +require File.dirname(__FILE__) + "/test_helper" + +class TestDatastructure < Test::Unit::TestCase + include DiffFixtureHelper + + # Data + def test_behaves_like_an_array + data = Diff::Display::Data.new + data << "foo" + data.push "bar" + assert_equal ["foo", "bar"], data + end + + # Line + def test_initializes_with_an_old_line_number + line = Diff::Display::Line.new("foo", 12) + assert_equal 12, line.old_number + end + + def test_initializes_with_numbers + line = Diff::Display::Line.new("foo", 12, 13) + assert_equal 12, line.old_number + assert_equal 13, line.new_number + end + + def test_has_a_class_method_for_creating_an_addline + line = Diff::Display::Line.add("foo", 7) + assert_instance_of Diff::Display::AddLine, line + end + + def test_has_a_class_method_for_creating_a_remline + line = Diff::Display::Line.rem("foo", 7) + assert_instance_of Diff::Display::RemLine, line + end + + def test_has_a_class_method_for_creating_a_unmodline + line = Diff::Display::Line.unmod("foo", 7, 8) + assert_instance_of Diff::Display::UnModLine, line + end + + def test_has_a_class_method_for_creating_a_header_line + line = Diff::Display::Line.header("foo") + assert_instance_of Diff::Display::HeaderLine, line + end + + def test_has_an_identifier + assert_equal :add, Diff::Display::Line.add("foo", 7).identifier + assert_equal :rem, Diff::Display::Line.rem("foo", 7).identifier + assert_equal :unmod, Diff::Display::Line.unmod("foo", 7, 8).identifier + assert_equal :header, Diff::Display::Line.header("foo").identifier + assert_equal :nonewline, Diff::Display::Line.nonewline("foo").identifier + end + + # Block + def test_block_behaves_like_an_array + block = Diff::Display::Block.new + block.push 1,2,3 + assert_equal 3, block.size + assert_equal [1,2,3], block + end + + def test_has_class_method_for_creating_an_addblock + block = Diff::Display::Block.add + assert_instance_of Diff::Display::AddBlock, block + end + + def test_has_class_method_for_creating_an_remblock + block = Diff::Display::Block.rem + assert_instance_of Diff::Display::RemBlock, block + end + + def test_has_class_method_for_creating_an_modblock + block = Diff::Display::Block.mod + assert_instance_of Diff::Display::ModBlock, block + end + + def test_has_class_method_for_creating_an_unmodblock + block = Diff::Display::Block.unmod + assert_instance_of Diff::Display::UnModBlock, block + end + + def test_has_class_method_for_creating_an_headerblock + block = Diff::Display::Block.header + assert_instance_of Diff::Display::HeaderBlock, block + end + +end diff --git a/vendor/diff-display/test/test_diff_renderer.rb b/vendor/diff-display/test/test_diff_renderer.rb new file mode 100644 index 0000000..a59cd3d --- /dev/null +++ b/vendor/diff-display/test/test_diff_renderer.rb @@ -0,0 +1,14 @@ +# encoding: utf-8 + +require File.dirname(__FILE__) + "/test_helper" + +class TestDiffRenderer < Test::Unit::TestCase + include DiffFixtureHelper + + def test_it_renders_a_diff_back_to_its_original_state + data = Diff::Display::Unified::Generator.run(load_diff("simple")) + base_renderer = Diff::Renderer::Diff.new + assert_equal load_diff("simple"), base_renderer.render(data) + end +end + diff --git a/vendor/diff-display/test/test_generator.rb b/vendor/diff-display/test/test_generator.rb new file mode 100644 index 0000000..09f6b60 --- /dev/null +++ b/vendor/diff-display/test/test_generator.rb @@ -0,0 +1,125 @@ +# encoding: utf-8 + +require File.dirname(__FILE__) + "/test_helper" + +class TestGenerator < Test::Unit::TestCase + include DiffFixtureHelper + + def setup + @generator = Diff::Display::Unified::Generator.new + end + + def test_generator_run_raises_if_doesnt_get_a_enumerable_object + assert_raises(ArgumentError) do + Diff::Display::Unified::Generator.run(nil) + end + end + + def test_generator_run_processes_each_line_in_the_diff + Diff::Display::Unified::Generator.expects(:new).returns(@generator) + @generator.expects(:process).with("foo") + @generator.expects(:process).with("bar") + Diff::Display::Unified::Generator.run("foo\nbar") + end + + def test_generator_run_returns_the_data + Diff::Display::Unified::Generator.expects(:new).returns(@generator) + generated = Diff::Display::Unified::Generator.run("foo\nbar") + assert_instance_of Diff::Display::Data, generated + end + + def test_the_returned_that_object_is_in_parity_with_the_diff + %w[multiple_rems_then_add only_rem simple multiple_adds_after_rem only_add pseudo_recursive simple_oneliner].each do |diff| + data = Diff::Display::Unified::Generator.run(load_diff(diff)) + assert_equal load_diff(diff).chomp, data.to_diff, "failed on #{diff}" + end + end + + def test_big + diff_data = load_diff("big") + data = Diff::Display::Unified::Generator.run(diff_data) + assert_equal diff_data.chomp, data.to_diff + end + + def test_multiple_rems_and_an_add_is_in_parity + diff_data = load_diff("multiple_rems_then_add") + data = Diff::Display::Unified::Generator.run(diff_data) + assert_equal diff_data.chomp, data.to_diff + end + + def test_doesnt_parse_linenumbers_that_isnt_part_if_the_diff + range = 1..14 + expected_lines = range.to_a.map{|l| [nil, l] } + assert_equal expected_lines, line_numbers_for(:pseudo_recursive).compact + end + + def test_parses_no_newline_at_end_of_file + diff_data = load_diff("pseudo_recursive") + data = Diff::Display::Unified::Generator.run(diff_data) + assert_equal diff_data.chomp, data.to_diff + assert_instance_of Diff::Display::NonewlineBlock, data.last + assert_equal 1, data.last.size + assert_instance_of Diff::Display::NonewlineLine, data.last[0] + assert_equal '\\ No newline at end of file', data.last[0] + end + + def test_a_changed_string_becomes_a_modblock + diff_data = load_diff("simple_oneliner") + data = Diff::Display::Unified::Generator.run(diff_data) + + assert_equal diff_data.chomp, data.to_diff + assert_equal 6, data.size + modblock = data[4] + assert_instance_of Diff::Display::ModBlock, modblock + assert_equal 2, modblock.size, modblock.inspect + + rem = modblock[0] + add = modblock[1] + assert_instance_of Diff::Display::RemLine, rem + assert_instance_of Diff::Display::AddLine, add + assert add.inline_changes? + assert rem.inline_changes? + end + + # line numbering + def test_numbers_correctly_for_multiple_adds_after_rem + expected = [ + [193, 193], + [194, nil], + [nil, 194], + [nil, 195], + [nil, 196], + [nil, 197], + [nil, 198], + [195, 199] + ] + assert_equal expected, line_numbers_for(:multiple_adds_after_rem) + end + + def test_numbers_correctly_for_simple + expected = [ + [1, 1], + [2, 2], + [3, nil], + [4, nil], + [nil, 3], + [nil, 4], + [nil, 5], + ] + assert_equal expected, line_numbers_for(:simple) + end + + def line_numbers_for(diff) + diff_data = load_diff(diff) + data = Diff::Display::Unified::Generator.run(diff_data) + linenos = [] + data.each do |blk| + blk.each do |line| + next if line.class == Diff::Display::HeaderLine + next if line.class == Diff::Display::NonewlineLine + linenos << [line.old_number, line.new_number] + end + end + linenos + end +end diff --git a/vendor/diff-display/test/test_helper.rb b/vendor/diff-display/test/test_helper.rb new file mode 100644 index 0000000..323d323 --- /dev/null +++ b/vendor/diff-display/test/test_helper.rb @@ -0,0 +1,25 @@ +# require "test/unit" +# $:.unshift File.dirname(__FILE__) + "/../lib/" +# require "diff-display" +require File.join(File.dirname(__FILE__), "/../lib/diff-display") + +if RUBY_VERSION > '1.9' + gem 'test-unit', ">=0" + class Test::Unit::TestCase + PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, Interrupt, SystemExit] + end +end +require 'test/unit' +require "rubygems" +gem("mocha", ">=0") +require "mocha" +begin + require "redgreen" +rescue LoadError +end + +module DiffFixtureHelper + def load_diff(name) + File.read(File.dirname(__FILE__) + "/fixtures/#{name}.diff") + end +end
\ No newline at end of file diff --git a/vendor/diff-display/spec/unified_spec.rb b/vendor/diff-display/test/test_unified.rb index 39c5712..3061191 100644 --- a/vendor/diff-display/spec/unified_spec.rb +++ b/vendor/diff-display/test/test_unified.rb @@ -1,21 +1,22 @@ -require File.dirname(__FILE__) + '/spec_helper.rb' +require File.dirname(__FILE__) + "/test_helper" -describe Diff::Display::Unified do +class TestUnified < Test::Unit::TestCase + include DiffFixtureHelper - it "generates its data structure via the Generator" do + def test_generates_its_data_structure_via_the_generator generator_data = mock("Generator mock") Diff::Display::Unified::Generator.expects(:run).returns(generator_data) diff = Diff::Display::Unified.new(load_diff("simple")) - diff.data.should == generator_data + assert_equal generator_data, diff.data end - it "renders a diff via a callback and renders it to a stringlike object" do + def test_renders_a_diff_via_a_callback_and_renders_it_to_a_stringlike_object diff = Diff::Display::Unified.new(load_diff("simple")) callback = mock() callback.expects(:render).returns("foo") output = "" diff.render(callback, output) - output.should == "foo" + assert_equal "foo", output end -end
\ No newline at end of file +end |