Skip to content

Commit

Permalink
Warn when placeholders are redefined
Browse files Browse the repository at this point in the history
If a placeholder is replaced a warning is now emitted. As the test suite
relied on placeholder replacements being silent, placeholders are now
cleared to defaults after each test case. Internal calls made from dsl.rb are filtered
out in order to show the real location of the replacement.
  • Loading branch information
graemeboyd committed Jan 20, 2020
1 parent 2928c17 commit 70bb287
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/turnip/placeholder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ class Match < Struct.new(:regexp, :block);end

class << self
def add(name, &block)
if placeholders.key?(name)
location = caller_locations.detect { |l| l.to_s !~ /lib\/turnip\/dsl\.rb/ }
warn "Placeholder :#{name} was replaced at #{location}."
end

placeholders[name] = Placeholder.new(name, &block)
end

Expand Down
17 changes: 17 additions & 0 deletions spec/placeholder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
end
end

after { Turnip::Placeholder.send(:placeholders).clear }

it 'returns a regexp for the given placeholder' do
resolved = described_class.resolve(:test)

Expand Down Expand Up @@ -150,4 +152,19 @@
end
end
end

describe 'replacing placeholders' do
before do
described_class.add(:test) do
match(/foo/)
end
end

it 'issues a warning' do
expect(described_class).to receive(:warn).with(/Placeholder :test was replaced/)
described_class.add(:test) do
match(/bar/)
end
end
end
end
2 changes: 2 additions & 0 deletions spec/step_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
describe Turnip::StepDefinition do
let(:all_steps) { [] }

after { Turnip::Placeholder.send(:placeholders).clear }

describe "#match" do
it "matches a simple step" do
step = Turnip::StepDefinition.new("there are monsters") {}
Expand Down

0 comments on commit 70bb287

Please sign in to comment.