From 7087b7dbd891f8692fd403a91d595d4f783fcf49 Mon Sep 17 00:00:00 2001 From: Danilo Carolino Date: Fri, 13 Oct 2023 12:11:40 -0300 Subject: [PATCH] Address edge case with table config param (#339) by @krasnoukhov --- lib/confset/options.rb | 6 +++--- spec/fixtures/reserved_keywords.yml | 1 + spec/options_spec.rb | 22 ++++++++++++++++++++++ spec/spec_helper.rb | 1 + 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/confset/options.rb b/lib/confset/options.rb index 806d6b87..23c1d799 100644 --- a/lib/confset/options.rb +++ b/lib/confset/options.rb @@ -114,7 +114,7 @@ def merge!(hash) end # Some keywords that don't play nicely with OpenStruct - SETTINGS_RESERVED_NAMES = %w[select collect test count zip min max exit!].freeze + SETTINGS_RESERVED_NAMES = %w[select collect test count zip min max exit! table].freeze # An alternative mechanism for property access. # This let's you do foo['bar'] along with foo.bar. @@ -134,11 +134,11 @@ def []=(param, value) end def key?(key) - table.key?(key) + @table.key?(key) end def has_key?(key) - table.has_key?(key) + @table.has_key?(key) end def method_missing(method_name, *args) diff --git a/spec/fixtures/reserved_keywords.yml b/spec/fixtures/reserved_keywords.yml index dbda5458..74e851bf 100644 --- a/spec/fixtures/reserved_keywords.yml +++ b/spec/fixtures/reserved_keywords.yml @@ -5,3 +5,4 @@ zip: cherry max: kumquat min: fig exit!: taro +table: strawberry diff --git a/spec/options_spec.rb b/spec/options_spec.rb index f3af37f8..65a827ad 100644 --- a/spec/options_spec.rb +++ b/spec/options_spec.rb @@ -20,6 +20,7 @@ expect(config.max).to eq("kumquat") expect(config.min).to eq("fig") expect(config.exit!).to eq("taro") + expect(config.table).to eq("strawberry") end it "should allow to access them using [] operator" do @@ -30,6 +31,7 @@ expect(config["max"]).to eq("kumquat") expect(config["min"]).to eq("fig") expect(config["exit!"]).to eq("taro") + expect(config["table"]).to eq("strawberry") expect(config[:select]).to eq("apple") expect(config[:collect]).to eq("banana") @@ -38,6 +40,26 @@ expect(config[:max]).to eq("kumquat") expect(config[:min]).to eq("fig") expect(config[:exit!]).to eq("taro") + expect(config[:table]).to eq("strawberry") + end + end + + context "when empty" do + let(:config) do + Confset.load_files("#{fixture_path}/empty1.yml") + end + + it "should allow to access them via object member notation" do + expect(config.select).to be_nil + expect(config.table).to be_nil + end + + it "should allow to access them using [] operator" do + expect(config["select"]).to be_nil + expect(config["table"]).to be_nil + + expect(config[:select]).to be_nil + expect(config[:table]).to be_nil end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c29da534..c8dfbf80 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,7 @@ RSpec.configure do |config| # Turn the deprecation warnings into errors, giving you the full backtrace config.raise_errors_for_deprecations! + config.include FixtureHelper config.before(:suite) do Confset.module_eval do