How can I add wrappers to a file based on a pattern?
For instance I have the following:
...
find(css_policy_form_stage3).click
with_ajax_wait
expect(css_coverage_type_everquote).to be_visible
end
with_ajax_wait
expect(css_coverage_type_everquote).to be_visible
end
end
it "Stage 3" do
select(coverage_type, from: css_coverage_type_everquote)
find(css_has_auto_insurance).click
...
And I want to 'wrap' those "with_ajax_wait"
blocks with it "waits" do ... end
around them.
i.e. I want to get:
...
find(css_policy_form_stage3).click
it "waits" do
with_ajax_wait
expect(css_coverage_type_everquote).to be_visible
end
end
it "waits" do
with_ajax_wait
expect(css_coverage_type_everquote).to be_visible
end
do
end
it "Stage 3" do
select(coverage_type, from: css_coverage_type_everquote)
find(css_has_auto_insurance).click
...
Notes and Assumptions:
- block to indent code is always 3 lines long (with... expect... and end). It would be nice to allow for more than i inner code line but not required for the simplest case.
- the block itself should have an extra 2 space indent.
- there are other end's that are not related (example shown just before "Stage 3"
- it would be nice to be able to specify an inner pattern also, e.g. only these blocks that have
expect
starting the code line being indented. I think awk is probably the tool due to the ability to read consecutive lines but I am struggling to know how to write this.
I'm imagining this is a generally useful q&a as adding wrapper within files is not uncommon.
Somewhat similar to my previous question:
How to use awk to indent a source file based on simple rules?
However in this case I am adding the wrapper plus the indent.