Skip to content

Commit b560c84

Browse files
committed
BARN-77: don't override base context options
1 parent c271a17 commit b560c84

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
parameter_substitution (3.0.0.pre.0)
4+
parameter_substitution (3.0.0.pre.1)
55
activesupport (>= 6.0)
66
builder (~> 3.2)
77
invoca-utils (~> 0.3)

lib/parameter_substitution.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,16 @@ def find_warnings(string_with_tokens, mapping: {}, context_overrides: nil)
8686
# @param [Hash, nil] context_overrides Optional overrides for context attributes
8787
# @return [ParameterSubstitution::Context] The constructed context
8888
def build_context(string_with_tokens, mapping, context_overrides)
89+
override_options = context_overrides || {}
8990
base_options = {
9091
input: string_with_tokens,
9192
mapping: mapping
9293
}
9394

94-
ParameterSubstitution::Context.new(**base_options.merge(context_overrides || {}))
95+
ParameterSubstitution::Context.new(**override_options.merge(base_options))
9596
end
9697

98+
9799
def parse_expression(context)
98100
cst = ParameterSubstitution::Parser.new(
99101
parameter_start: context.parameter_start,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
class ParameterSubstitution
4-
VERSION = "3.0.0-0"
4+
VERSION = "3.0.0-1"
55
end

spec/lib/parameter_substitution_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ def self.find(_key); end
9595
expect(ParameterSubstitution.find_tokens(square_expression, mapping: mapping, context_overrides: context_overrides)).to eq(['call.start_time', 'do_a_barrel_roll'])
9696
end
9797
end
98+
99+
context 'when context_overrides attempts to override base options' do
100+
it "ignores input override and uses method parameter" do
101+
context_overrides = { input: "<different>" }
102+
expect(ParameterSubstitution.find_tokens(expression, context_overrides: context_overrides)).to eq(['call', 'do_a_barrel_roll'])
103+
end
104+
105+
it "ignores mapping override and uses method parameter" do
106+
context_overrides = { mapping: { 'different' => 'value' } }
107+
expect(ParameterSubstitution.find_tokens(expression, mapping: mapping, context_overrides: context_overrides)).to eq(['call.start_time', 'do_a_barrel_roll'])
108+
end
109+
end
98110
end
99111

100112
context '#find_formatters' do
@@ -119,6 +131,18 @@ def self.find(_key); end
119131
expect(ParameterSubstitution.find_formatters(square_expression, mapping: mapping, context_overrides: context_overrides)).to eq(['blank_if_nil', 'downcase'])
120132
end
121133
end
134+
135+
context 'when context_overrides attempts to override base options' do
136+
it "ignores input override and uses method parameter" do
137+
context_overrides = { input: "<different>" }
138+
expect(ParameterSubstitution.find_formatters(expression, context_overrides: context_overrides)).to eq(['start_time', 'blank_if_nil', 'downcase'])
139+
end
140+
141+
it "ignores mapping override and uses method parameter" do
142+
context_overrides = { mapping: { 'different' => 'value' } }
143+
expect(ParameterSubstitution.find_formatters(expression, mapping: mapping, context_overrides: context_overrides)).to eq(['blank_if_nil', 'downcase'])
144+
end
145+
end
122146
end
123147

124148
context '#find_warnings' do
@@ -180,6 +204,20 @@ def self.find(_key); end
180204
"Unknown methods 'test1', 'test2' used on parameter 'black'"])
181205
end
182206
end
207+
208+
context 'when context_overrides attempts to override base options' do
209+
it "ignores input override and uses method parameter" do
210+
context_overrides = { input: "<different>" }
211+
expect(ParameterSubstitution.find_warnings(expression_with_valid_params, mapping: default_mapping, context_overrides: context_overrides))
212+
.to eq([])
213+
end
214+
215+
it "ignores mapping override and uses method parameter" do
216+
context_overrides = { mapping: { 'bobby' => 'value', 'bobby2' => 'value' } }
217+
expect(ParameterSubstitution.find_warnings(expression_with_bad_params, mapping: {}, context_overrides: context_overrides))
218+
.to eq(["Unknown param 'bobby'", "Unknown param 'bobby2'"])
219+
end
220+
end
183221
end
184222
end
185223

0 commit comments

Comments
 (0)