diff --git a/.travis.yml b/.travis.yml index dd36cee..0a59ef7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: ruby rvm: - - 1.9.2 - - 1.9.3 - - 2.0.0 - - jruby-19mode - - rbx-19mode + - 2.4 + - 2.5 + - 2.6 + - 2.7 diff --git a/Rakefile b/Rakefile index abd0206..7a58f1b 100644 --- a/Rakefile +++ b/Rakefile @@ -1,11 +1,13 @@ -require "bundler/gem_tasks" +# frozen_string_literal: true + +require 'bundler/gem_tasks' require 'rake/testtask' Rake::TestTask.new do |t| - t.libs << "test" + t.libs << 'test' t.test_files = FileList['test/*_test.rb'] t.verbose = true end -task :default => :test +task default: :test diff --git a/json-merge_patch.gemspec b/json-merge_patch.gemspec index 4138816..62cf7f2 100644 --- a/json-merge_patch.gemspec +++ b/json-merge_patch.gemspec @@ -1,24 +1,26 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'json/merge_patch/version' Gem::Specification.new do |spec| - spec.name = "json-merge_patch" + spec.name = 'json-merge_patch' spec.version = JSON::MergePatch::VERSION - spec.authors = ["Steve Klabnik"] - spec.email = ["steve@steveklabnik.com"] - spec.description = %q{An implementation of the json-merge-patch draft.} - spec.summary = %q{An implementation of the json-merge-patch draft.} - spec.homepage = "http://json-merge-patch.herokuapp.com/" - spec.license = "MIT" + spec.authors = ['Steve Klabnik'] + spec.email = ['steve@steveklabnik.com'] + spec.description = 'An implementation of the json-merge-patch draft.' + spec.summary = 'An implementation of the json-merge-patch draft.' + spec.homepage = 'http://json-merge-patch.herokuapp.com/' + spec.license = 'MIT' - spec.files = `git ls-files`.split($/) + spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ["lib"] + spec.require_paths = ['lib'] - spec.add_development_dependency "bundler", "~> 1.3" - spec.add_development_dependency "rake" - spec.add_development_dependency "minitest" + spec.required_ruby_version = '>= 2.4' + spec.add_development_dependency 'bundler' + spec.add_development_dependency 'minitest' + spec.add_development_dependency 'rake' end diff --git a/lib/json/merge_patch.rb b/lib/json/merge_patch.rb index 3f4e3d7..2eb21d1 100644 --- a/lib/json/merge_patch.rb +++ b/lib/json/merge_patch.rb @@ -58,7 +58,7 @@ def is_array_or_primitive?(obj) end def is_primitive?(val) - [ String, Fixnum, + [ String, Integer, Float, TrueClass, FalseClass, NilClass ].include?(val.class) diff --git a/lib/json/merge_patch/version.rb b/lib/json/merge_patch/version.rb index d1656a9..40f9a8a 100644 --- a/lib/json/merge_patch/version.rb +++ b/lib/json/merge_patch/version.rb @@ -3,6 +3,6 @@ module JSON class MergePatch # The current version of json-merge_patch - VERSION = "1.1.0" + VERSION = "2.0.0" end end diff --git a/test/merge_patch_test.rb b/test/merge_patch_test.rb index 60a3da3..758f288 100644 --- a/test/merge_patch_test.rb +++ b/test/merge_patch_test.rb @@ -1,214 +1,200 @@ +# frozen_string_literal: true + require 'test_helper' require 'json/merge_patch' -describe "Section 1" do -=begin -1. If either the root of the JSON data provided in the payload or - the root of the target resource are JSON Arrays, the target - resource is to be replaced, in whole, by the provided data. Any - object member contained in the provided data whose value is - explicitly null is to be treated as if the member was undefined. -=end +describe 'Section 1' do + # 1. If either the root of the JSON data provided in the payload or + # the root of the target resource are JSON Arrays, the target + # resource is to be replaced, in whole, by the provided data. Any + # object member contained in the provided data whose value is + # explicitly null is to be treated as if the member was undefined. - describe "if the root of the data provided is an array" do - let(:merge_patch) { %q'["foo"]' } + describe 'if the root of the data provided is an array' do + let(:merge_patch) { '["foo"]' } - it "replaces the root whole" do - document = %q'{"foo":"bar"}' + it 'replaces the root whole' do + document = '{"foo":"bar"}' - expected = %q'["foo"]' + expected = '["foo"]' assert_equal expected, JSON.merge(document, merge_patch) end end - describe "if the root of the target resource is an array" do - let(:document) { %q'["foo"]' } + describe 'if the root of the target resource is an array' do + let(:document) { '["foo"]' } - it "replaces the root whole" do - merge_patch = %q'{"foo":"bar","bar":null}' + it 'replaces the root whole' do + merge_patch = '{"foo":"bar","bar":null}' - expected = %q'{"foo":"bar"}' + expected = '{"foo":"bar"}' assert_equal expected, JSON.merge(document, merge_patch) end end end -describe "section 2" do -=begin -2. If the root of the JSON data provided in the payload is an - Object, for each distinct member specified within that object: -=end +describe 'section 2' do + # 2. If the root of the JSON data provided in the payload is an + # Object, for each distinct member specified within that object: - describe "part 1" do -=begin -* If the member is currently undefined within the target - resource, and the given value is not null, the member and the - value are to be added to the target. Any object member - contained in the provided data whose value is explicitly null - is to be treated as if the member were undefined. -=end - it "adds members" do - document = %q'{"foo":"bar"}' + describe 'part 1' do + # * If the member is currently undefined within the target + # resource, and the given value is not null, the member and the + # value are to be added to the target. Any object member + # contained in the provided data whose value is explicitly null + # is to be treated as if the member were undefined. + it 'adds members' do + document = '{"foo":"bar"}' - merge_patch = %q'{"foo":"bar","baz":"quxx"}' + merge_patch = '{"foo":"bar","baz":"quxx"}' - expected = %q'{"foo":"bar","baz":"quxx"}' + expected = '{"foo":"bar","baz":"quxx"}' assert_equal expected, JSON.merge(document, merge_patch) end end - describe "part 2" do -=begin -* If the value is explicitly set to null and that member is - currently defined within the target resource, the existing - member is removed. -=end - it "removes members" do - document = %q'{"foo":"bar"}' + describe 'part 2' do + # * If the value is explicitly set to null and that member is + # currently defined within the target resource, the existing + # member is removed. + it 'removes members' do + document = '{"foo":"bar"}' - merge_patch = %q'{"foo":null}' + merge_patch = '{"foo":null}' - expected = %q'{}' + expected = '{}' assert_equal expected, JSON.merge(document, merge_patch) end end - describe "part 3" do -=begin -* If the value is either a non-null JSON primitive or an Array - and that member is currently defined within the target - resource, the existing value for that member is to be replaced - with that provided. Any object member contained in the - provided data whose value is explicitly null is to be treated - as if the member were undefined. -=end - describe "object" do - it "replaces members" do - document = %q'{"foo":"bar"}' + describe 'part 3' do + # * If the value is either a non-null JSON primitive or an Array + # and that member is currently defined within the target + # resource, the existing value for that member is to be replaced + # with that provided. Any object member contained in the + # provided data whose value is explicitly null is to be treated + # as if the member were undefined. + describe 'object' do + it 'replaces members' do + document = '{"foo":"bar"}' - merge_patch = %q'{"foo":"baz"}' + merge_patch = '{"foo":"baz"}' - expected = %q'{"foo":"baz"}' + expected = '{"foo":"baz"}' assert_equal expected, JSON.merge(document, merge_patch) end end end - describe "part 4" do -=begin -* If the value is a JSON object and that member is currently - defined for the target resource and the existing value is a - JSON primitive or Array, the existing value is to be replaced - in whole by the object provided. Any object member contained - in the provided data whose value is explicitly null is to be - treated as if the member was undefined. -=end - describe "patch has a primitive" do - it "replaces members" do - document = %q'{"foo":{"bar":"baz"}}' + describe 'part 4' do + # * If the value is a JSON object and that member is currently + # defined for the target resource and the existing value is a + # JSON primitive or Array, the existing value is to be replaced + # in whole by the object provided. Any object member contained + # in the provided data whose value is explicitly null is to be + # treated as if the member was undefined. + describe 'patch has a primitive' do + it 'replaces members' do + document = '{"foo":{"bar":"baz"}}' - merge_patch = %q'{"foo":2}' + merge_patch = '{"foo":2}' - expected = %q'{"foo":2}' + expected = '{"foo":2}' assert_equal expected, JSON.merge(document, merge_patch) end end - describe "patch has an array" do - it "replaces members" do - document = %q'{"foo":{"bar":"baz"}}' + describe 'patch has an array' do + it 'replaces members' do + document = '{"foo":{"bar":"baz"}}' - merge_patch = %q'{"foo":["bar"]}' + merge_patch = '{"foo":["bar"]}' - expected = %q'{"foo":["bar"]}' + expected = '{"foo":["bar"]}' assert_equal expected, JSON.merge(document, merge_patch) end end end - describe "part 5" do -=begin -* If the value is a JSON object and that member is currently - defined within the target resource and the existing value is - also a JSON object, then recursively apply Rule #2 to each - object. -=end - describe "nested objects" do - it "recursively " do - document = %q'{"foo":{"bar":"baz"}}' + describe 'part 5' do + # * If the value is a JSON object and that member is currently + # defined within the target resource and the existing value is + # also a JSON object, then recursively apply Rule #2 to each + # object. + describe 'nested objects' do + it 'recursively ' do + document = '{"foo":{"bar":"baz"}}' - merge_patch = %q'{"foo":{"bar":{"baz":"qux","dev":null}}}' + merge_patch = '{"foo":{"bar":{"baz":"qux","dev":null}}}' - expected = %q'{"foo":{"bar":{"baz":"qux"}}}' + expected = '{"foo":{"bar":{"baz":"qux"}}}' assert_equal expected, JSON.merge(document, merge_patch) end end - it "recursive removal" do - document = %q'{"foo":{"bar":"baz"}}' + it 'recursive removal' do + document = '{"foo":{"bar":"baz"}}' - merge_patch = %q'{"foo":{"bar":null}}' + merge_patch = '{"foo":{"bar":null}}' - expected = %q'{"foo":{}}' + expected = '{"foo":{}}' assert_equal expected, JSON.merge(document, merge_patch) end end - describe "part 6" do -=begin -* Any member currently defined within the target resource that - does not explicitly appear within the patch is to remain - untouched and unmodified. -=end - it "leaves well enough alone!" do - document = %q'{"content": "This will be unchanged"}' + describe 'part 6' do + # * Any member currently defined within the target resource that + # does not explicitly appear within the patch is to remain + # untouched and unmodified. + it 'leaves well enough alone!' do + document = '{"content": "This will be unchanged"}' - merge_patch = %q'{}' + merge_patch = '{}' - expected = %q'{"content":"This will be unchanged"}' + expected = '{"content":"This will be unchanged"}' assert_equal expected, JSON.merge(document, merge_patch) end end end -describe "missing coverage" do - it "deals with a nil patch" do - document = %q'{"foo":"bar"}' - expected = %q'{"foo":"bar"}' +describe 'missing coverage' do + it 'deals with a nil patch' do + document = '{"foo":"bar"}' + expected = '{"foo":"bar"}' assert_equal expected, JSON::MergePatch.new(document, nil).call end - it "handles primitive true" do - document = %q'{"foo":"bar"}' - patch = %q'{"foo":true}' - expected = %q'{"foo":true}' + it 'handles primitive true' do + document = '{"foo":"bar"}' + patch = '{"foo":true}' + expected = '{"foo":true}' assert_equal expected, JSON.merge(document, patch) end - it "handles primitive false" do - document = %q'{"foo":"bar"}' - patch = %q'{"foo":false}' - expected = %q'{"foo":false}' + it 'handles primitive false' do + document = '{"foo":"bar"}' + patch = '{"foo":false}' + expected = '{"foo":false}' assert_equal expected, JSON.merge(document, patch) end end -describe "README example" do - it "works properly" do +describe 'README example' do + it 'works properly' do document = <<-JSON.strip_heredoc.chomp { "title": "Goodbye!", @@ -232,14 +218,14 @@ } JSON - expected = %q'{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}' + expected = '{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}' assert_equal expected, JSON.merge(document, merge_patch) end end -describe "errors" do - it "throws an error when stuff goes wrong" do +describe 'errors' do + it 'throws an error when stuff goes wrong' do assert_raises(JSON::MergeError) do JSON.merge(nil, nil) end diff --git a/test/test_helper.rb b/test/test_helper.rb index 062bef7..1c8c304 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'simplecov' SimpleCov.start do add_filter do |source_file| @@ -11,12 +13,10 @@ require 'minitest/autorun' def pending - begin - yield - fail "OMG pending test passed." - rescue MiniTest::Assertion - skip "Still pending" - end + yield + raise 'OMG pending test passed.' +rescue MiniTest::Assertion + skip 'Still pending' end class String