diff options
author | Michael Herold <opensource@michaeljherold.com> | 2020-10-29 19:49:08 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-29 19:49:08 -0500 |
commit | 75410a6657090348de7bff3e5eab8aa684e49c38 (patch) | |
tree | 27e0eab23a209706b738bcfcf5736220fc3ac871 /spec | |
parent | 59d04c851e8ae80d82e93d82059e591eaa7718a7 (diff) | |
parent | f152e25912d0b93ff08b8964eb757200ad4b6f32 (diff) | |
download | hashie-75410a6657090348de7bff3e5eab8aa684e49c38.tar.gz |
Merge pull request #537 from michaelherold/dash-consistency
Fix inconsistencies with Dash defaults
Diffstat (limited to 'spec')
-rw-r--r-- | spec/hashie/dash_spec.rb | 10 | ||||
-rw-r--r-- | spec/hashie/extensions/ignore_undeclared_spec.rb | 12 | ||||
-rw-r--r-- | spec/spec_helper.rb | 2 | ||||
-rw-r--r-- | spec/support/shared_examples.rb | 26 |
4 files changed, 50 insertions, 0 deletions
diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb index 5f3bfe3..3adfc8f 100644 --- a/spec/hashie/dash_spec.rb +++ b/spec/hashie/dash_spec.rb @@ -606,3 +606,13 @@ context 'with method access' do it { is_expected.to eq true } end end + +RSpec.describe Hashie::Dash do + let(:test) do + Class.new(Hashie::Dash) do + property :description, default: '' + end + end + + include_examples 'Dash default handling', :description +end diff --git a/spec/hashie/extensions/ignore_undeclared_spec.rb b/spec/hashie/extensions/ignore_undeclared_spec.rb index 50c4f31..6f2d818 100644 --- a/spec/hashie/extensions/ignore_undeclared_spec.rb +++ b/spec/hashie/extensions/ignore_undeclared_spec.rb @@ -27,6 +27,18 @@ describe Hashie::Extensions::IgnoreUndeclared do hash = subject.new(city: 'Toronto') expect { hash.country = 'Canada' }.to raise_error(NoMethodError) end + + context 'with a default value' do + let(:test) do + Class.new(Hashie::Trash) do + include Hashie::Extensions::IgnoreUndeclared + + property :description, from: :desc, default: '' + end + end + + include_examples 'Dash default handling', :description, :desc + end end context 'combined with DeepMerge' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0bdf972..5ac288a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,6 +13,8 @@ require './spec/support/ruby_version_check' require './spec/support/logger' require './spec/support/matchers' +Dir[File.expand_path(File.join(__dir__, 'support', '**', '*'))].sort.each { |file| require file } + RSpec.configure do |config| config.extend RubyVersionCheck config.expect_with :rspec do |expect| diff --git a/spec/support/shared_examples.rb b/spec/support/shared_examples.rb new file mode 100644 index 0000000..df05592 --- /dev/null +++ b/spec/support/shared_examples.rb @@ -0,0 +1,26 @@ +RSpec.shared_examples 'Dash default handling' do |property, name = property| + it 'uses the default when initializing' do + expect(test.new(name => nil).public_send(property)).to eq '' + end + + it 'allows you to set the value to nil with the hash writer' do + trash = test.new(name => 'foo') + trash[name] = nil + + expect(trash.public_send(property)).to be_nil + end + + it 'allows you to set the value to nil with the method writer' do + trash = test.new(name => 'foo') + trash[name] = nil + + expect(trash.public_send(property)).to be_nil + end + + it 'uses the default when updating with defaults' do + trash = test.new(name => 'foo') + trash.update_attributes!(name => nil) + + expect(trash.public_send(property)).to eq '' + end +end |