Skip to content

Commit

Permalink
refacotr: Use shared examples
Browse files Browse the repository at this point in the history
  • Loading branch information
buty4649 committed Nov 14, 2023
1 parent 16c5351 commit 5a9d319
Show file tree
Hide file tree
Showing 17 changed files with 371 additions and 517 deletions.
234 changes: 118 additions & 116 deletions spec/method_spec.rb → spec/container/instance_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,34 @@
describe 'Method' do
describe '#gsub' do
let(:input) { 'foo' }
let(:output) do
<<~OUTPUT
bar
foo
OUTPUT
end
describe 'Container internal methods' do
using RSpec::Parameterized::TableSyntax

before { run_rf(%q('puts gsub(/foo/, "bar"); _'), input) }
where(:method, :expect_output) do
'gsub' | %w[barbar foofoo].join("\n")
'gsub!' | %w[barbar barbar].join("\n")
'sub' | %w[barfoo foofoo].join("\n")
'sub!' | %w[barfoo barfoo].join("\n")
end

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
with_them do
let(:input) { 'foofoo' }
let(:args) { %('puts #{method}(/foo/, "bar"); _') }

it_behaves_like 'a successful exec'
end

describe '#gsub!' do
let(:input) { 'foo' }
let(:output) do
<<~OUTPUT
bar
bar
OUTPUT
end
where(:method, :expect_output) do
'tr' | %w[FOOFOO foofoo].join("\n")
'tr!' | %w[FOOFOO FOOFOO].join("\n")
end

before { run_rf(%q('puts gsub!(/foo/, "bar"); _'), input) }
with_them do
let(:input) { 'foofoo' }
let(:args) { %('puts #{method}("a-z", "A-Z"); _') }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
it_behaves_like 'a successful exec'
end

%w[match m].each do |method|
describe "##{method}" do
let(:input) do
<<~INPUT
1 foo bar
2 foo baz
3 foo qux
INPUT
end

describe "Container##{method}" do
where do
{
'String' => {
Expand All @@ -63,7 +53,7 @@
condition: '_1 == "3"',
output: {
without_block: '3 foo qux',
with_block: 3
with_block: '3'
}
},
'FalseClass' => {
Expand Down Expand Up @@ -95,33 +85,33 @@
end

with_them do
let(:input) do
<<~INPUT
1 foo bar
2 foo baz
3 foo qux
INPUT
end

context 'without block' do
before { run_rf("'#{method} #{condition}'", input) }
let(:args) { %('#{method} #{condition}') }
let(:expect_output) { output[:without_block] }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output[:without_block] }
it_behaves_like 'a successful exec'
end

context 'with block' do
before { run_rf("'#{method} #{condition} { _1 }'", input) }
let(:args) { %('#{method} #{condition} { _1 }') }
let(:expect_output) { output[:with_block] }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output[:with_block] }
it_behaves_like 'a successful exec'
end
end
end
end

%w[match? m?].each do |method|
describe "##{method}" do
let(:input) do
<<~INPUT
1 foo bar
2 foo baz
3 foo qux
INPUT
end

where do
{
'String' => {
Expand Down Expand Up @@ -192,87 +182,99 @@
end

with_them do
let(:input) do
<<~INPUT
1 foo bar
2 foo baz
3 foo qux
INPUT
end

context 'without block' do
before { run_rf("'#{method} #{condition}'", input) }
let(:args) { "'#{method} #{condition}'" }
let(:expect_output) { output[:without_block] }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output[:without_block] }
it_behaves_like 'a successful exec'
end

context 'with block' do
before { run_rf("'#{method} #{condition} { _1 }'", input) }
let(:args) { "'#{method} #{condition} { _1 }'" }
let(:expect_output) { output[:with_block] }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output[:with_block] }
it_behaves_like 'a successful exec'
end

describe 'return value' do
before { run_rf("-q 'p #{method} #{condition} { _1 }'", input) }
let(:args) { "-q 'p #{method} #{condition} { _1 }'" }
let(:expect_output) { output[:return_value] }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output[:return_value] }
it_behaves_like 'a successful exec'
end
end
end
end

describe '#sub' do
let(:input) { 'foofoo' }
let(:output) do
<<~OUTPUT
barfoo
foofoo
OUTPUT
end

before { run_rf(%q('puts sub(/foo/, "bar"); _'), input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end

describe '#sub!' do
let(:input) { 'foofoo' }
let(:output) do
<<~OUTPUT
barfoo
barfoo
OUTPUT
end

before { run_rf(%q('puts sub!(/foo/, "bar"); _'), input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end

describe '#tr' do
let(:input) { 'foo' }
let(:output) do
<<~OUTPUT
FOO
foo
OUTPUT
end

before { run_rf(%q('puts tr("a-z", "A-Z"); _'), input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end

describe '#tr!' do
let(:input) { 'foo' }
let(:output) do
<<~OUTPUT
FOO
FOO
OUTPUT
end

before { run_rf(%q('puts tr!("a-z", "A-Z"); _'), input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
end
end

# describe 'Method' do
#
#
# describe '#sub' do
# let(:input) { 'foofoo' }
# let(:output) do
# <<~OUTPUT
# barfoo
# foofoo
# OUTPUT
# end
#
# before { run_rf(%q('puts sub(/foo/, "bar"); _'), input) }
#
# it { expect(last_command_started).to be_successfully_executed }
# it { expect(last_command_started).to have_output output_string_eq output }
# end
#
# describe '#sub!' do
# let(:input) { 'foofoo' }
# let(:output) do
# <<~OUTPUT
# barfoo
# barfoo
# OUTPUT
# end
#
# before { run_rf(%q('puts sub!(/foo/, "bar"); _'), input) }
#
# it { expect(last_command_started).to be_successfully_executed }
# it { expect(last_command_started).to have_output output_string_eq output }
# end
#
# describe '#tr' do
# let(:input) { 'foo' }
# let(:output) do
# <<~OUTPUT
# FOO
# foo
# OUTPUT
# end
#
# before { run_rf(%q('puts tr("a-z", "A-Z"); _'), input) }
#
# it { expect(last_command_started).to be_successfully_executed }
# it { expect(last_command_started).to have_output output_string_eq output }
# end
#
# describe '#tr!' do
# let(:input) { 'foo' }
# let(:output) do
# <<~OUTPUT
# FOO
# FOO
# OUTPUT
# end
#
# before { run_rf(%q('puts tr!("a-z", "A-Z"); _'), input) }
#
# it { expect(last_command_started).to be_successfully_executed }
# it { expect(last_command_started).to have_output output_string_eq output }
# end
# end
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

with_them do
let(:input) { 'foo' }
let(:output) { input }
let(:args) { "-q 'puts #{name}'" }
let(:expect_output) { input }

before { run_rf("-q 'puts #{name}'", input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
it_behaves_like 'a successful exec'
end
end

Expand All @@ -23,18 +21,16 @@
with_them do
context 'when record is String' do
let(:input) { 'foo bar baz' }
let(:output) do
let(:args) { '-q "p $F[0],$F[1],$F[2]"' }
let(:expect_output) do
<<~OUTPUT
"foo"
"bar"
"baz"
OUTPUT
end

before { run_rf('-q "p $F[0],$F[1],$F[2]"', input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
it_behaves_like 'a successful exec'
end

context 'when record is Hash' do
Expand All @@ -47,34 +43,30 @@
}
JSON
end
let(:output) do
let(:args) { "-j -q 'p #{name}[0],#{name}[1],#{name}[2]'" }
let(:expect_output) do
<<~OUTPUT
["a", 1]
["b", 2]
["c", 3]
OUTPUT
end

before { run_rf("-j -q 'p #{name}[0],#{name}[1],#{name}[2]'", input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
it_behaves_like 'a successful exec'
end

context 'when record is Other class' do
let(:input) { '1' }
let(:output) do
let(:args) { "-j -q 'p #{name}[0],#{name}[1],#{name}[2]'" }
let(:expect_output) do
<<~OUTPUT
1
nil
nil
OUTPUT
end

before { run_rf("-j -q 'p #{name}[0],#{name}[1],#{name}[2]'", input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
it_behaves_like 'a successful exec'
end
end
end
Expand All @@ -92,18 +84,16 @@
baz
TEXT
end
let(:output) do
let(:args) { %('[#{name}, _].join(" ")') }
let(:expect_output) do
<<~OUTPUT
1 foo
2 bar
3 baz
OUTPUT
end

before { run_rf(%/'[#{name}, _].join(" ")'/, input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output output_string_eq output }
it_behaves_like 'a successful exec'
end
end
end
Loading

0 comments on commit 5a9d319

Please sign in to comment.