diffing 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a9db88ed0327a83b434f94b73fa9a425d14dbe8
4
- data.tar.gz: 071b087aa7c10b65a685daa44a548c73176fbd88
3
+ metadata.gz: 8f515a9445aa155f3572e497fd3074293485f80b
4
+ data.tar.gz: 7fb8fb8c02e4e94c45c5f4d4a579839cc0819a62
5
5
  SHA512:
6
- metadata.gz: d8e5f2ed5e39eabfa3b035bda196fe1d74d44978579aacda5354d000ebefb905fad14e0b164050990ef6b90e28e95ecb361e87f036afdef96c49c4decc831696
7
- data.tar.gz: 502929c52ffc80ed63cf86b0109aeaea91e15382b224ed3aefc66741532ae0ca70e8b13b37ad6e0389b45c0198e7453bbefb7d10b0eedd427e4e4b5f5a4ea72b
6
+ metadata.gz: bf83c911a89640fcdf1716603d9b60305798b3a670478f5bb49d75cff7aeaa12461f212b20bd685c64d538af406e0fa20ff5cb5be68861646e9768d2c6846dc7
7
+ data.tar.gz: 4a117ea61cd887661511e6a022ab5b22f2e8f17c83fb4d8959fb29206f5df6774655652cca3a7d2cd20d696fc72ff7371074fd2274cb40757235adeed528cb5d
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- from 'https://rubygems.org'
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Diffing
2
2
 
3
+ [![Build Status](https://travis-ci.org/4urbanoff/Diffing.svg)](https://travis-ci.org/4urbanoff/Diffing)
4
+ [![Code Climate](https://codeclimate.com/github/4urbanoff/Diffing/badges/gpa.svg)](https://codeclimate.com/github/4urbanoff/Diffing)
5
+ [![Test Coverage](https://codeclimate.com/github/4urbanoff/Diffing/badges/coverage.svg)](https://codeclimate.com/github/4urbanoff/Diffing/coverage)
6
+
3
7
  ## Installation
4
8
 
5
9
  ```
@@ -59,11 +63,11 @@ module CustomFormat
59
63
  "(++#{ value })"
60
64
  end
61
65
 
62
- def delete( value )
66
+ def remove( value )
63
67
  "(--#{ value })"
64
68
  end
65
69
 
66
- def replace( from, to )
70
+ def change( from, to )
67
71
  "(#{ from } => #{ to })"
68
72
  end
69
73
 
@@ -89,11 +93,11 @@ Diffing.by_words( from, to ).parts.map { |part|
89
93
  result = ''
90
94
  result << "<source:#{ part.source }>" if part.source?
91
95
  result << "<insert:#{ part.insert }>" if part.insert?
92
- result << "<delete:#{ part.delete }>" if part.delete?
96
+ result << "<remove:#{ part.remove }>" if part.remove?
93
97
  result
94
98
 
95
99
  }.join
96
- # => <insert:Hi!><delete:Hello!><source: I am><insert: two><source: string for diffing><delete: test>
100
+ # => <insert:Hi!><remove:Hello!><source: I am><insert: two><source: string for diffing><remove: test>
97
101
  ```
98
102
 
99
103
 
data/Rakefile CHANGED
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new( :default )
data/lib/diffing/diff.rb CHANGED
@@ -5,28 +5,28 @@ module Diffing
5
5
 
6
6
  attr_reader :parts
7
7
 
8
- def initialize( from, to, pattern = nil )
9
- @pattern = pattern
10
- @parts = calcucate( split( from.to_s ), split( to.to_s ) ).flatten
8
+ def initialize( from, to, pattern = nil )
9
+ @pattern = pattern
10
+ @parts = calculate( split( from.to_s ), split( to.to_s ) ).flatten
11
11
  end
12
12
 
13
13
  def format( format )
14
14
  result = []
15
15
  @parts.each do |part|
16
- result << format.source( part.source ) if part.source?
17
- result << format.insert( part.insert ) if part.insert? && !part.delete?
18
- result << format.delete( part.delete ) if part.delete? && !part.insert?
19
- result << format.replace( part.delete, part.insert ) if part.insert? && part.delete?
16
+ result << format.source( part.source ) if part.source?
17
+ result << format.insert( part.insert ) if part.insert? && !part.remove?
18
+ result << format.remove( part.remove ) if part.remove? && !part.insert?
19
+ result << format.change( part.remove, part.insert ) if part.insert? && part.remove?
20
20
  end
21
- result.join
21
+ result.join
22
22
  end
23
23
 
24
24
  def as_ascii
25
- format Format::Ascii
25
+ format Formats::Ascii
26
26
  end
27
27
 
28
28
  def as_html
29
- format Format::Html
29
+ format Formats::Html
30
30
  end
31
31
 
32
32
  alias :to_s :as_ascii
@@ -35,12 +35,12 @@ module Diffing
35
35
 
36
36
  private
37
37
 
38
- def calcucate( from, to )
38
+ def calculate( from, to )
39
39
  if found = find_middle( from, to )
40
40
  from_l, to_l, source, from_r, to_r = found
41
- [ calcucate( from_l, to_l ), Part.new( source: join( source ) ), calcucate( from_r, to_r ) ]
41
+ [ calculate( from_l, to_l ), Part.new( source: join( source ) ), calculate( from_r, to_r ) ]
42
42
  else
43
- [ Part.new( insert: join( to ), delete: join( from ) ) ]
43
+ [ Part.new( insert: join( to ), remove: join( from ) ) ]
44
44
  end
45
45
  end
46
46
 
@@ -75,7 +75,7 @@ module Diffing
75
75
  end
76
76
 
77
77
  def find_middle_index( set, search_subset )
78
- return set.index( search_subset ) unless @pattern
78
+ return set.index( search_subset ) if set.is_a?( String ) && search_subset.is_a?( String )
79
79
  subsets_each( set, search_subset.size ){ |subset, first, last| return first if subset == search_subset }
80
80
  nil
81
81
  end
@@ -89,11 +89,11 @@ module Diffing
89
89
  end
90
90
 
91
91
  def split( set )
92
- @pattern ? set.scan( @pattern ) : set
92
+ @pattern ? set.scan( @pattern ) : set
93
93
  end
94
94
 
95
95
  def join( set )
96
- @pattern ? set.join : set
96
+ set.is_a?( Array ) ? set.join : set
97
97
  end
98
98
 
99
99
  end
@@ -1,6 +1,6 @@
1
1
  module Diffing
2
2
 
3
- module Format
3
+ module Formats
4
4
 
5
5
  module Ascii
6
6
 
@@ -12,15 +12,15 @@ module Diffing
12
12
  end
13
13
 
14
14
  def insert( value )
15
- "{+`#{ value }`}"
15
+ "{+`#{ value }`}"
16
16
  end
17
17
 
18
- def delete( value )
19
- "{-`#{ value }`}"
18
+ def remove( value )
19
+ "{-`#{ value }`}"
20
20
  end
21
21
 
22
- def replace( from, to )
23
- "{`#{ from }`>>`#{ to }`}"
22
+ def change( from, to )
23
+ "{`#{ from }`>>`#{ to }`}"
24
24
  end
25
25
 
26
26
 
@@ -1,33 +1,33 @@
1
- module Diffing
2
-
3
- module Format
4
-
5
- module Html
6
-
7
- class << self
8
-
9
-
10
- def source( value )
11
- value
12
- end
13
-
14
- def insert( value )
15
- "<ins>#{ value }</ins>"
16
- end
17
-
18
- def delete( value )
19
- "<del>#{ value }</del>"
20
- end
21
-
22
- def replace( from, to )
23
- delete( from ) + insert( to )
24
- end
25
-
26
-
27
- end
28
-
29
- end
30
-
31
- end
32
-
33
- end
1
+ module Diffing
2
+
3
+ module Formats
4
+
5
+ module Html
6
+
7
+ class << self
8
+
9
+
10
+ def source( value )
11
+ value
12
+ end
13
+
14
+ def insert( value )
15
+ "<ins>#{ value }</ins>"
16
+ end
17
+
18
+ def remove( value )
19
+ "<del>#{ value }</del>"
20
+ end
21
+
22
+ def change( from, to )
23
+ remove( from ) + insert( to )
24
+ end
25
+
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
data/lib/diffing/part.rb CHANGED
@@ -3,10 +3,10 @@ module Diffing
3
3
 
4
4
  class Part
5
5
 
6
- attr_reader :source, :insert, :delete
6
+ attr_reader :source, :insert, :remove
7
7
 
8
- def initialize( source: '', insert: '', delete: '' )
9
- @source, @insert, @delete = source, insert, delete
8
+ def initialize( source: '', insert: '', remove: '' )
9
+ @source, @insert, @remove = source, insert, remove
10
10
  end
11
11
 
12
12
  def source?
@@ -17,10 +17,10 @@ module Diffing
17
17
  not @insert.empty?
18
18
  end
19
19
 
20
- def delete?
21
- not @delete.empty?
20
+ def remove?
21
+ not @remove.empty?
22
22
  end
23
-
23
+
24
24
  end
25
25
 
26
26
 
data/lib/diffing.rb CHANGED
@@ -1,7 +1,7 @@
1
- require 'diffing/diff'
2
- require 'diffing/part'
3
- require 'diffing/formats/ascii'
4
- require 'diffing/formats/html'
1
+ require 'diffing/diff'
2
+ require 'diffing/part'
3
+ require 'diffing/formats/ascii'
4
+ require 'diffing/formats/html'
5
5
 
6
6
 
7
7
  module Diffing
@@ -9,16 +9,16 @@ module Diffing
9
9
  class << self
10
10
 
11
11
 
12
- def by_chars( from, to )
13
- Diff.new from, to
12
+ def by_chars( from, to )
13
+ Diff.new from, to
14
14
  end
15
15
 
16
16
  def by_words( from, to )
17
- Diff.new from, to, /\S+|\s+/
17
+ Diff.new from, to, /\S+|\s+/
18
18
  end
19
-
20
- def by_lines( from, to )
21
- Diff.new from, to, /[^\n]+|\n+/
19
+
20
+ def by_lines( from, to )
21
+ Diff.new from, to, /[^\n]+|\n+/
22
22
  end
23
23
 
24
24
 
@@ -0,0 +1,48 @@
1
+ describe Diffing::Diff do
2
+
3
+ it '#initialize' do
4
+ expect( diff ).to an_instance_of( Diffing::Diff )
5
+ end
6
+
7
+ it '#calculate' do
8
+ ex = diff_by_chars.send( :calculate, 'ab', 'bc' ).flatten.map { |part|
9
+ ( part.source? and part.source ) or ( part.insert? and part.insert ) or ( part.remove? and part.remove )
10
+ }.join
11
+ expect( ex ).to eql( 'abc' )
12
+ ex = diff_by_words.send( :calculate, %w(a b), %w(b c) ).flatten.map { |part|
13
+ ( part.source? and part.source ) or ( part.insert? and part.insert ) or ( part.remove? and part.remove )
14
+ }.join
15
+ expect( ex ).to eql( 'abc' )
16
+ end
17
+
18
+ it '#find_middle' do
19
+ expect( diff_by_chars.send( :find_middle, '.mid..', '--mid-' ) ).to eq( ['.', '--', 'mid', '..', '-'] )
20
+ expect( diff_by_chars.send( :find_middle, '.mid..', '__===_' ) ).to eq( nil )
21
+ expect( diff_by_words.send( :find_middle, %w(. m i d . .), %w(- - m i d -) ) ).to eq( [['.'], ['-', '-'], ['m', 'i', 'd'], ['.', '.'], ['-']] )
22
+ expect( diff_by_words.send( :find_middle, %w(. m i d . .), %w(_ _ = = = _) ) ).to eq( nil )
23
+ end
24
+
25
+ it '#find_middle_index' do
26
+ expect( diff_by_chars.send( :find_middle_index, '...mid...', 'mid' ) ).to eq( 3 )
27
+ expect( diff_by_words.send( :find_middle_index, %w(. . . m i d . . .), %w(m i d) ) ).to eq( 3 )
28
+ expect( diff_by_chars.send( :find_middle_index, '...mid...', 'nil' ) ).to eq( nil )
29
+ expect( diff_by_words.send( :find_middle_index, %w(. . . m i d . . .), %w(n i l) ) ).to eq( nil )
30
+ end
31
+
32
+ it '#subsets_each' do
33
+ result = []
34
+ diff.send( :subsets_each, 'abcde', 3 ){ |*args| result << args }
35
+ expect( result ).to eq( [ ['abc', 0, 3], ['bcd', 1, 4], ['cde', 2, 5] ] )
36
+ end
37
+
38
+ it '#split' do
39
+ expect( diff_by_chars.send( :split, 'abc' ) ).to an_instance_of( String )
40
+ expect( diff_by_words.send( :split, 'a b' ) ).to an_instance_of( Array )
41
+ end
42
+
43
+ it '#join' do
44
+ expect( diff.send( :join, 'abc' ) ).to eq( 'abc' )
45
+ expect( diff.send( :join, %w(a b c) ) ).to eq( 'abc' )
46
+ end
47
+
48
+ end
@@ -0,0 +1,29 @@
1
+ describe Diffing::Formats do
2
+
3
+ Diffing::Formats.constants.each do |name|
4
+
5
+ name do
6
+
7
+ subject = Diffing::Formats.const_get name
8
+
9
+ it 'does not have method source' do
10
+ subject.source ''
11
+ end
12
+
13
+ it 'does not have method insert' do
14
+ subject.insert ''
15
+ end
16
+
17
+ it 'does not have method remove' do
18
+ subject.remove ''
19
+ end
20
+
21
+ it 'does not have method change' do
22
+ subject.change '', ''
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,33 @@
1
+ describe Diffing::Part do
2
+
3
+ subject { Diffing::Part.new( source: 'a', insert: 'b', remove: 'c' ) }
4
+
5
+ it '#initialize' do
6
+ expect( subject ).to an_instance_of( Diffing::Part )
7
+ end
8
+
9
+ it '#source' do
10
+ expect( subject.source ).to eq( 'a' )
11
+ end
12
+
13
+ it '#insert' do
14
+ expect( subject.insert ).to eq( 'b' )
15
+ end
16
+
17
+ it '#remove' do
18
+ expect( subject.remove ).to eq( 'c' )
19
+ end
20
+
21
+ it '#source?' do
22
+ expect( subject.source? ).to eq( true )
23
+ end
24
+
25
+ it '#insert?' do
26
+ expect( subject.insert? ).to eq( true )
27
+ end
28
+
29
+ it '#remove?' do
30
+ expect( subject.remove? ).to eq( true )
31
+ end
32
+
33
+ end
@@ -0,0 +1,19 @@
1
+ describe Diffing do
2
+
3
+ it 'does not exists' do
4
+ Diffing
5
+ end
6
+
7
+ it 'does not have method by_chars( from, to )' do
8
+ Diffing.by_chars "", ""
9
+ end
10
+
11
+ it 'does not have method by_words( from, to )' do
12
+ Diffing.by_words "", ""
13
+ end
14
+
15
+ it 'does not have method by_lines( from, to )' do
16
+ Diffing.by_lines "", ""
17
+ end
18
+
19
+ end
@@ -0,0 +1,34 @@
1
+ require 'diffing'
2
+
3
+
4
+ module Diffing
5
+
6
+ module SpecHelper
7
+
8
+ def diff( type = :by_chars, from = "", to = "" )
9
+ Diffing.send type, from, to
10
+ end
11
+
12
+ def diff_by_chars( from = "", to = "" )
13
+ diff :by_chars, from, to
14
+ end
15
+
16
+ def diff_by_words( from = "", to = "" )
17
+ diff :by_words, from, to
18
+ end
19
+
20
+ def diff_by_lines( from = "", to = "" )
21
+ diff :by_lines, from, to
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
28
+
29
+
30
+ RSpec.configure do |config|
31
+
32
+ config.include Diffing::SpecHelper
33
+
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diffing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Churbanov
@@ -9,7 +9,35 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-08-05 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description: Diffing of two strings
14
42
  email: 4urbanoff@gmail.com
15
43
  executables: []
@@ -21,10 +49,16 @@ files:
21
49
  - lib/diffing/formats/html.rb
22
50
  - lib/diffing/part.rb
23
51
  - lib/diffing.rb
52
+ - spec/diffing/diff_spec.rb
53
+ - spec/diffing/formats_spec.rb
54
+ - spec/diffing/part_spec.rb
55
+ - spec/diffing_spec.rb
56
+ - spec/spec_helper.rb
24
57
  - LICENSE.txt
25
58
  - Rakefile
26
59
  - Gemfile
27
60
  - README.md
61
+ - .rspec
28
62
  homepage: https://github.com/4urbanoff/Diffing
29
63
  licenses:
30
64
  - MIT