diff --git a/lib/rollbar/util.rb b/lib/rollbar/util.rb index 6e721c71..f1457db1 100644 --- a/lib/rollbar/util.rb +++ b/lib/rollbar/util.rb @@ -66,10 +66,13 @@ def self.deep_copy(obj) end def self.deep_merge(hash1, hash2) + hash1 ||= {} + hash2 ||= {} + hash2.each_key do |k| - if hash1[k].is_a? ::Hash and hash2[k].is_a? ::Hash + if hash1[k].is_a?(::Hash) && hash2[k].is_a?(::Hash) hash1[k] = deep_merge(hash1[k], hash2[k]) - elsif hash1[k].is_a? Array and hash2[k].is_a? Array + elsif hash1[k].is_a?(Array) && hash2[k].is_a?(Array) hash1[k] += deep_copy(hash2[k]) elsif hash2[k] hash1[k] = deep_copy(hash2[k]) diff --git a/spec/rollbar/util_spec.rb b/spec/rollbar/util_spec.rb new file mode 100644 index 00000000..108dab3a --- /dev/null +++ b/spec/rollbar/util_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +require 'rollbar/util' + +describe Rollbar::Util do + describe '.deep_merge' do + context 'with nil arguments' do + let(:data) do + { :foo => :bar } + end + + it 'doesnt fail and returns same hash' do + result = Rollbar::Util.deep_merge(nil, data) + + expect(result).to be_eql(data) + end + end + end +end diff --git a/spec/rollbar_spec.rb b/spec/rollbar_spec.rb index 962cd6c6..8c300732 100644 --- a/spec/rollbar_spec.rb +++ b/spec/rollbar_spec.rb @@ -2,7 +2,6 @@ require 'logger' require 'socket' -require 'spec_helper' require 'girl_friday' require 'redis' require 'active_support/core_ext/object' @@ -14,6 +13,8 @@ rescue LoadError end +require 'spec_helper' + describe Rollbar do let(:notifier) { Rollbar.notifier } before do