Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when replacing placeholders #219

Merged
merged 1 commit into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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