summaryrefslogtreecommitdiff
path: root/spec/hashie
diff options
context:
space:
mode:
authorBobby McDonald <BobbyMcWho@users.noreply.github.com>2019-07-18 13:11:41 -0400
committerMichael Herold <opensource@michaeljherold.com>2019-07-18 12:11:40 -0500
commitf0faaca6acc69689aa208c4420c258506a0596af (patch)
tree84e43dde19e0968584c40c80f4d1d210fab53648 /spec/hashie
parentda9fd39a0e551e09c1441cb7453c969a4afbfd7f (diff)
downloadhashie-f0faaca6acc69689aa208c4420c258506a0596af.tar.gz
Add selective key-conflict warnings for Mash subclasses (#478)
In some cases, you want to be able to ignore Mash warnings for keys that you know you aren't going to access via a method accessor, yet be warned for other keys that you know you might want to access. This change adds the ability to selectively ignore warnings for specific keys instead of globally ignoring the warnings. The change retains the original behavior as well, so if you call `Mash.disable_warnings` without a value it will still globally ignore the warnings.
Diffstat (limited to 'spec/hashie')
-rw-r--r--spec/hashie/mash_spec.rb73
1 files changed, 72 insertions, 1 deletions
diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb
index 59e3183..3bcb478 100644
--- a/spec/hashie/mash_spec.rb
+++ b/spec/hashie/mash_spec.rb
@@ -154,7 +154,6 @@ describe Hashie::Mash do
mash_class = Class.new(Hashie::Mash) do
disable_warnings
end
-
mash_class.new('trust' => { 'two' => 2 })
expect(logger_output).to be_blank
@@ -174,6 +173,78 @@ describe Hashie::Mash do
expect(logger_output).to be_blank
end
+
+ it 'writes to logger when a key is overridden that is not blacklisted' do
+ mash_class = Class.new(Hashie::Mash) do
+ disable_warnings :merge
+ end
+
+ mash_class.new('address' => { 'zip' => '90210' })
+ expect(logger_output).not_to be_blank
+ end
+
+ it 'does not write to logger when a key is overridden that is blacklisted' do
+ mash_class = Class.new(Hashie::Mash) do
+ disable_warnings :zip
+ end
+
+ mash_class.new('address' => { 'zip' => '90210' })
+ expect(logger_output).to be_blank
+ end
+
+ it 'carries over the disabled blacklist for warnings on grandchild classes' do
+ child_class = Class.new(Hashie::Mash) do
+ disable_warnings :zip, :merge
+ end
+ grandchild_class = Class.new(child_class)
+
+ grandchild_class.new('address' => { 'zip' => '90210' }, 'merge' => true)
+
+ expect(grandchild_class.disable_warnings_blacklist).to eq(%i[zip merge])
+ expect(logger_output).to be_blank
+ end
+
+ context 'multiple disable_warnings calls' do
+ context 'calling disable_warnings multiple times with parameters' do
+ it 'appends each new parameter to the blacklist' do
+ child_class = Class.new(Hashie::Mash) do
+ disable_warnings :zip
+ disable_warnings :merge
+ disable_warnings :cycle
+ end
+
+ expect(child_class.disable_warnings_blacklist).to eq(%i[zip merge cycle])
+ end
+ end
+
+ context 'calling disable_warnings without keys after calling with keys' do
+ it 'uses the last call to ignore the blacklist' do
+ child_class = Class.new(Hashie::Mash) do
+ disable_warnings :zip
+ disable_warnings
+ end
+
+ child_class.new('address' => { 'zip' => '90210' }, 'merge' => true, 'cycle' => 'bi')
+
+ expect(child_class.disable_warnings_blacklist).to eq([])
+ expect(logger_output).to be_blank
+ end
+ end
+
+ context 'calling disable_parameters with keys after calling without keys' do
+ it 'only ignores logging for the blacklisted methods' do
+ child_class = Class.new(Hashie::Mash) do
+ disable_warnings
+ disable_warnings :zip
+ end
+
+ child_class.new('address' => { 'zip' => '90210' }, 'merge' => true)
+
+ expect(logger_output).to match(/#{child_class}#merge/)
+ expect(logger_output).not_to match(/#{child_class}#zip/)
+ end
+ end
+ end
end
context 'updating' do