Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix change after copy #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Hoe.plugin :git # `gem install hoe-git`
Hoe.spec 'hana' do
developer('Aaron Patterson', '[email protected]')
license 'MIT'
self.urls = {}
self.readme_file = 'README.md'
self.extra_rdoc_files = FileList['*.rdoc']
extra_dev_deps << ["minitest", "~> 5.0"]
Expand Down
6 changes: 5 additions & 1 deletion lib/hana.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def copy ins, doc

raise(MissingTargetException, "target location '#{ins['path']}' does not exist") unless dest

add_op dest, key, obj
add_op dest, key, deep_copy(obj)
doc
end

Expand Down Expand Up @@ -251,5 +251,9 @@ def rm_op obj, key
end
end
end

def deep_copy(o)
Marshal.load(Marshal.dump(o))
end
end
end
18 changes: 17 additions & 1 deletion test/mine.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,21 @@
{ "comment": "test add with bad number should fail",
"doc": ["foo", "sil"],
"patch": [{"op": "add", "path": "/1e0", "value": "bar"}],
"error": "add op shouldn't add to array with bad number" }
"error": "add op shouldn't add to array with bad number" },

{ "comment": "test copy object then change copied",
"doc": {"foo": {"bar": {"baz": [{"boo": "net"}]}}},
"patch": [
{"op": "copy", "from": "/foo", "path": "/bak"},
{"op": "replace", "path": "/bak/bar/baz/0/boo", "value": "qux"}
],
"expected": {"foo": {"bar": {"baz": [{"boo": "net"}]}}, "bak": {"bar": {"baz": [{"boo":"qux"}]}}}},

{ "comment": "test copy object then change source",
"doc": {"foo": {"bar": {"baz": [{"boo": "net"}]}}},
"patch": [
{"op": "copy", "from": "/foo", "path": "/bak"},
{"op": "replace", "path": "/foo/bar/baz/0/boo", "value": "qux"}
],
"expected": {"foo": {"bar": {"baz": [{"boo": "qux"}]}}, "bak": {"bar": {"baz": [{"boo":"net"}]}}}}
]