jsondiff 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ [submodule "spec/json-patch-tests"]
2
+ path = spec/json-patch-tests
3
+ url = https://github.com/json-patch/json-patch-tests.git
4
+ [submodule "spec/hana"]
5
+ path = spec/hana
6
+ url = https://github.com/tenderlove/hana.git
data/README.md CHANGED
@@ -4,12 +4,6 @@ A JSON patch generator in Ruby that is compliant to the JSON Patch specification
4
4
 
5
5
  To apply a patch, you should use [hana](https://github.com/tenderlove/hana).
6
6
 
7
- ## Status
8
-
9
- This is proof of concept only. Only example of the specification works.
10
-
11
- As for now, it generate only patch with *add*, *remove* and *replace* operations.
12
-
13
7
  ## Install
14
8
 
15
9
  gem install jsondiff
@@ -33,6 +27,10 @@ JsonDiff.generate({foo: :bar}, {foo: :plop})
33
27
 
34
28
  ## Changelog
35
29
 
30
+ - 0.0.4:
31
+ - Don't modify input objects
32
+ - Fix the order when removing entry in an array
33
+ - Add round trip tests with Hana and IETF tests
36
34
  - 0.0.3:
37
35
  - Allow to compare root Hash and Array.
38
36
  - Performance improvements
@@ -4,23 +4,23 @@ module JsonDiff
4
4
 
5
5
  def self.generate(result, prefix, array1, array2)
6
6
  if array1.size < array2.size
7
- array2.each_with_index do |value, index|
7
+ array2[(array1.size..array2.size)].each_with_index do |value, index|
8
+ index += array1.size
8
9
  if array1[index] != value
9
10
  result << add_op(prefix, index, value)
10
- array1.insert(index, value)
11
11
  end
12
12
  end
13
13
  elsif array1.size > array2.size
14
- array1.each_with_index do |value, index|
14
+ array1[(array2.size..array1.size)].reverse.each_with_index do |value, index|
15
+ index = array1.size - 1 - index
15
16
  if array2[index] != value
16
17
  result << remove_op(prefix, index)
17
- array1.delete_at(index)
18
18
  end
19
19
  end
20
20
  end
21
21
  array2.each_with_index do |value, index|
22
- if array1[index] != value
23
- JsonDiff.generate(array1.at(index), value, result, "#{prefix}/#{index}")
22
+ if array1.size > index && array1[index] != value
23
+ JsonDiff.generate(array1[index], value, result, "#{prefix}/#{index}")
24
24
  end
25
25
  end
26
26
  end
@@ -1,3 +1,3 @@
1
1
  module JsonDiff
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -2,8 +2,14 @@ require_relative 'spec_helper'
2
2
 
3
3
  describe JsonDiff do
4
4
  describe "#generate" do
5
- it "generate empty patch if nothing has changed" do
6
- subject.generate({}, {}).should == []
5
+ context "empty patch" do
6
+ it "on hash" do
7
+ subject.generate({}, {}).should == []
8
+ end
9
+
10
+ it "on array" do
11
+ subject.generate([], []).should == []
12
+ end
7
13
  end
8
14
 
9
15
  context "add op" do
@@ -15,8 +21,15 @@ describe JsonDiff do
15
21
 
16
22
  it "on array element" do
17
23
  subject.generate({foo: [:bar, :baz]},
24
+ {foo: [:bar, :baz, :qux]})
25
+ .should == [{ op: :add, path: "/foo/2", value: :qux }]
26
+ end
27
+
28
+ it "on multiple add in array elements" do
29
+ subject.generate({foo: [:bar]},
18
30
  {foo: [:bar, :qux, :baz]})
19
- .should == [{ op: :add, path: "/foo/1", value: :qux }]
31
+ .should == [{ op: :add, path: "/foo/1", value: :qux },
32
+ { op: :add, path: "/foo/2", value: :baz }]
20
33
  end
21
34
 
22
35
  it "on nested member object" do
@@ -52,9 +65,16 @@ describe JsonDiff do
52
65
  end
53
66
 
54
67
  it "on array element" do
55
- subject.generate({foo: [:bar, :qux, :baz]},
68
+ subject.generate({foo: [:bar, :baz, :qux]},
56
69
  {foo: [:bar, :baz]})
57
- .should == [{ op: :remove, path: "/foo/1" }]
70
+ .should == [{ op: :remove, path: "/foo/2" }]
71
+ end
72
+
73
+ it "on multiple remove in array elements" do
74
+ subject.generate({foo: [:bar, :qux, :baz]},
75
+ {foo: [:bar]})
76
+ .should == [{ op: :remove, path: "/foo/2" },
77
+ { op: :remove, path: "/foo/1" }]
58
78
  end
59
79
  end
60
80
 
@@ -90,4 +110,33 @@ describe JsonDiff do
90
110
  end
91
111
  end
92
112
  end
113
+
114
+ # Adapted from hana
115
+ # https://github.com/tenderlove/hana/blob/master/test/test_ietf.rb
116
+ # Copyright (c) 2012 Aaron Patterson
117
+ context "from ietf" do
118
+ TESTDIR = File.dirname File.expand_path __FILE__
119
+ json = File.read File.join TESTDIR, 'json-patch-tests', 'tests.json'
120
+ tests = JSON.parse json
121
+ tests.each_with_index do |test, i|
122
+ next unless test['doc']
123
+
124
+ it "#{test['comment'] || i }" do
125
+ pending "disabled" if test['disabled']
126
+
127
+ doc = test['doc']
128
+ expected = test['expected']
129
+
130
+ if test['error']
131
+ pending "cannot run error test case"
132
+ elsif !expected
133
+ pending "cannot run test case without expectation"
134
+ else
135
+ patch = JSON.parse(subject.generate(doc, expected).to_json)
136
+ hana = Hana::Patch.new patch
137
+ hana.apply(doc).should == expected
138
+ end
139
+ end
140
+ end
141
+ end
93
142
  end
@@ -1,4 +1,6 @@
1
1
  require_relative '../lib/jsondiff'
2
+ require 'json'
3
+ require_relative './hana/lib/hana'
2
4
 
3
5
  RSpec.configure do |c|
4
6
  c.filter_run focus: true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsondiff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-04-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &10290680 !ruby/object:Gem::Requirement
16
+ requirement: &13856960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 2.13.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *10290680
24
+ version_requirements: *13856960
25
25
  description: Generate a JSON Patch from 2 ruby hash
26
26
  email:
27
27
  - francois@2metz.fr
@@ -29,6 +29,7 @@ executables: []
29
29
  extensions: []
30
30
  extra_rdoc_files: []
31
31
  files:
32
+ - .gitmodules
32
33
  - .travis.yml
33
34
  - Gemfile
34
35
  - Gemfile.lock