From 5a9d319b8d8f0c82bbeb2de9d556910688305fa0 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 11 Nov 2023 17:06:27 +0900 Subject: [PATCH] refacotr: Use shared examples --- .../instance_methods_spec.rb} | 234 +++++++++--------- .../{ => container}/special_variables_spec.rb | 40 ++- spec/error_message_spec.rb | 165 +++--------- spec/feature/hash_spec.rb | 19 ++ spec/feature/integaer_float_spec.rb | 26 ++ spec/feature/nil_class_spec.rb | 29 +++ spec/feature/to_json_spec.rb | 16 ++ spec/feature_spec.rb | 161 ------------ spec/global_option_spec.rb | 48 ---- spec/{ => global_options}/help_spec.rb | 25 +- spec/global_options/in_place_option_spec.rb | 27 ++ spec/global_options/recursive_option_spec.rb | 25 ++ spec/global_options/version_spec.rb | 12 + spec/spec_helper.rb | 2 + spec/support/aruba.rb | 10 +- spec/support/shared_examples.rb | 35 +++ spec/version_spec.rb | 14 -- 17 files changed, 371 insertions(+), 517 deletions(-) rename spec/{method_spec.rb => container/instance_methods_spec.rb} (51%) rename spec/{ => container}/special_variables_spec.rb (51%) create mode 100644 spec/feature/hash_spec.rb create mode 100644 spec/feature/integaer_float_spec.rb create mode 100644 spec/feature/nil_class_spec.rb create mode 100644 spec/feature/to_json_spec.rb delete mode 100644 spec/feature_spec.rb delete mode 100644 spec/global_option_spec.rb rename spec/{ => global_options}/help_spec.rb (72%) create mode 100644 spec/global_options/in_place_option_spec.rb create mode 100644 spec/global_options/recursive_option_spec.rb create mode 100644 spec/global_options/version_spec.rb create mode 100644 spec/support/shared_examples.rb delete mode 100644 spec/version_spec.rb diff --git a/spec/method_spec.rb b/spec/container/instance_methods_spec.rb similarity index 51% rename from spec/method_spec.rb rename to spec/container/instance_methods_spec.rb index b7917cc..e6550a9 100644 --- a/spec/method_spec.rb +++ b/spec/container/instance_methods_spec.rb @@ -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' => { @@ -63,7 +53,7 @@ condition: '_1 == "3"', output: { without_block: '3 foo qux', - with_block: 3 + with_block: '3' } }, 'FalseClass' => { @@ -95,18 +85,26 @@ 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 @@ -114,14 +112,6 @@ %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' => { @@ -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 diff --git a/spec/special_variables_spec.rb b/spec/container/special_variables_spec.rb similarity index 51% rename from spec/special_variables_spec.rb rename to spec/container/special_variables_spec.rb index e564689..db2eaef 100644 --- a/spec/special_variables_spec.rb +++ b/spec/container/special_variables_spec.rb @@ -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 @@ -23,7 +21,8 @@ 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" @@ -31,10 +30,7 @@ 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 @@ -47,7 +43,8 @@ } 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] @@ -55,15 +52,13 @@ 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 @@ -71,10 +66,7 @@ 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 @@ -92,7 +84,8 @@ baz TEXT end - let(:output) do + let(:args) { %('[#{name}, _].join(" ")') } + let(:expect_output) do <<~OUTPUT 1 foo 2 bar @@ -100,10 +93,7 @@ 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 diff --git a/spec/error_message_spec.rb b/spec/error_message_spec.rb index 12e4d0e..4a1baf2 100644 --- a/spec/error_message_spec.rb +++ b/spec/error_message_spec.rb @@ -1,98 +1,29 @@ describe 'Show error message' do - context 'when invalid option' do - before { run_rf('--invalid-option') } - - let(:error_message) do - 'Error: invalid option: --invalid-option' - end - - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr error_message } - end - - context 'when invalid type' do - before { run_rf('-t test') } - - let(:error_message) do - 'Error: "test" is invalid type. possible values: text,json,yaml' - end - - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr error_message } - end - - context 'when missing argument' do - let(:error_message_prefix) do - 'Error: missing argument: ' - end - - %w[-t -F].each do |option| - describe option do - before { run_rf(option) } - - it { expect(last_command_started).not_to be_successfully_executed } - - it { - error_message = "#{error_message_prefix}#{option}" - expect(last_command_started).to have_output_on_stderr error_message - } - end - end - end - - context 'when syntax error' do - let(:input) { "test\n" } - let(:error_message) do - 'Error: line 1: syntax error, unexpected end of file' - end - - before { run_rf('if', input) } - - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr error_message } + using RSpec::Parameterized::TableSyntax + + where(:args, :expect_output) do + '--invalid-option' | 'Error: invalid option: --invalid-option' + '-t test' | 'Error: "test" is invalid type. possible values: text,json,yaml' + '-t' | 'Error: missing argument: -t' + '-F' | 'Error: missing argument: -F' + '_ not_found_file' | 'Error: file not found: not_found_file' + '_ .' | 'Error: .: is a directory' + '-f program_file' | 'Error: No such file or directory - open program_file' + '-R -i _' | 'Error: -R, -i: conflict options' + 'if' | 'Error: line 1: syntax error, unexpected end of file' + '_.very_useful_method' | "Error: undefined method 'very_useful_method'" + 'unknown_method' | "Error: undefined method 'unknown_method'" end - context 'when no method error (StandardError)' do + with_them do let(:input) { "test\n" } - let(:error_message) do - "Error: undefined method 'very_useful_method'" - end - - before { run_rf('_.very_useful_method', input) } - - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr error_message } - end - - context 'when file not found' do - let(:file) { 'not_found_file' } - let(:error_message) do - "Error: file not found: #{file}" - end - - before { run_rf("'' #{file}") } - - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr error_message } - end - - context 'when file is directory' do - let(:file) { '.' } - let(:error_message) do - "Error: #{file}: is a directory" - end - - before { run_rf("_ #{file}") } - - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr error_message } + it_behaves_like 'a failed exec' end context 'when permission denied' do - let(:file) { 'testfile' } - let(:error_message) do - "Error: #{file}: permission denied" - end + let(:file) { 'permission_denied_file' } + let(:args) { "_ #{file}" } + let(:expect_output) { "Error: #{file}: permission denied" } before do touch(file) @@ -102,20 +33,21 @@ else chmod(0o000, file) end - run_rf("_ #{file}") + end + after do # restore permissions `icacls #{expand_path(file)} /inheritancelevel:e` if windows? end - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr error_message } + it_behaves_like 'a failed exec' end context 'when enable debug mode' do let(:input) { "test\n" } - let(:error_message) do - <<~OUTPUT + let(:args) { 'if' } + let(:expect_output) do + Regexp.new(Regexp.escape(<<~OUTPUT)) Error: line 1: syntax error, unexpected end of file (Rf::SyntaxError) trace (most recent call last): @@ -124,53 +56,12 @@ before do ENV['RF_DEBUG'] = 'y' - run_rf('if', input) - ENV['RF_DEBUG'] = nil - end - - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr include_output_string error_message } - end - - context 'when method missing' do - describe 'internal method' do - let(:input) { "test\n" } - let(:error_message) { "Error: undefined method 'unknown_method'" } - - before { run_rf('unknown_method', input) } - - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr include_output_string error_message } end - describe 'Hash method' do - let(:input) { load_fixture('json/hash.json') } - let(:error_message) { "Error: undefined method 'unknown_method'" } - - before { run_rf('_.unknown_method', input) } - - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr include_output_string error_message } + after do + ENV['RF_DEBUG'] = nil end - end - - context 'when program file not found' do - let(:input) { "test\n" } - let(:error_message) { 'Error: No such file or directory - open program_file' } - - before { run_rf('-f program_file', input) } - - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr include_output_string error_message } - end - - context 'when conflict options' do - let(:input) { "test\n" } - let(:error_message) { 'Error: -R, -i: conflict options' } - - before { run_rf('-R -i _', input) } - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr include_output_string error_message } + it_behaves_like 'a failed exec' end end diff --git a/spec/feature/hash_spec.rb b/spec/feature/hash_spec.rb new file mode 100644 index 0000000..f8fa7eb --- /dev/null +++ b/spec/feature/hash_spec.rb @@ -0,0 +1,19 @@ +describe 'Automatic accessor addition to Hash' do + where do + { + 'key is exist' => { + args: '-j _.foo', + expect_output: 'bar' + }, + 'key is not exist' => { + args: '-j _.piyo.class.to_s', + expect_output: 'NilClass' + } + } + end + + with_them do + let(:input) { '{"foo": "bar"}' } + it_behaves_like 'a successful exec' + end +end diff --git a/spec/feature/integaer_float_spec.rb b/spec/feature/integaer_float_spec.rb new file mode 100644 index 0000000..3a144bc --- /dev/null +++ b/spec/feature/integaer_float_spec.rb @@ -0,0 +1,26 @@ +describe 'Extended addition feature for Integer and Float' do + where(:case_name, :args, :expect_output) do + def random_number(klass) + number = rand(100) + klass == 'Float' ? number.to_f : number + end + + %w[Integer Float].product(%w[+ - * /]).map do |klass, mark| + left = random_number(klass) + statement = %(#{left} #{mark} "1") + answer = left.__send__(mark, 1) + + [ + "#{klass}#+ with String argument", + %('#{statement}'), + answer.to_s + ] + end + end + + with_them do + let(:input) { '' } + + it_behaves_like 'a successful exec', input: '1' + end +end diff --git a/spec/feature/nil_class_spec.rb b/spec/feature/nil_class_spec.rb new file mode 100644 index 0000000..8f2ecf1 --- /dev/null +++ b/spec/feature/nil_class_spec.rb @@ -0,0 +1,29 @@ +describe 'Extended addition feature for NilClass' do + using RSpec::Parameterized::TableSyntax + + describe '#+' do + where(:input, :expect_output) do + %w[1 2 3].join("\n") | '6' + %w[1.1 2.2 3.3].join("\n") | '6.6' + %w[foo bar baz].join("\n") | 'foobarbaz' + end + + with_them do + let(:args) do + %w[-q 's+=_1; at_exit { puts s }'] + end + + it_behaves_like 'a successful exec' + end + end + + describe '#<<' do + let(:input) { %w[foo bar baz].join("\n") } + let(:args) do + %w[-q 's<<=_1; at_exit { puts s }'] + end + let(:expect_output) { '["foo", "bar", "baz"]' } + + it_behaves_like 'a successful exec' + end +end diff --git a/spec/feature/to_json_spec.rb b/spec/feature/to_json_spec.rb new file mode 100644 index 0000000..79371bc --- /dev/null +++ b/spec/feature/to_json_spec.rb @@ -0,0 +1,16 @@ +describe 'to_json' do + let(:input) { '' } + let(:args) do + %('{"foo": "bar"}.to_json') + end + + let(:expect_output) do + <<~OUTPUT + { + "foo":"bar" + } + OUTPUT + end + + it_behaves_like 'a successful exec' +end diff --git a/spec/feature_spec.rb b/spec/feature_spec.rb deleted file mode 100644 index 22f76d3..0000000 --- a/spec/feature_spec.rb +++ /dev/null @@ -1,161 +0,0 @@ -describe 'Feature' do - context 'for Integer' do - where do - { - '#+' => { - mark: '+', - output: 2 - }, - '#-' => { - mark: '-', - output: 0 - }, - '#*' => { - mark: '*', - output: 1 - }, - '#/' => { - mark: '/', - output: 1 - } - } - end - - with_them do - context 'when other is Integer' do - let(:input) { '1' } - - before { run_rf("-q 'puts 1 #{mark} 1'", input) } - - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } - end - - context 'when other is String' do - let(:input) { '1' } - - before { run_rf("-q 'puts 1 #{mark} _1'", input) } - - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } - end - end - end - - context 'for Float' do - where do - { - '#+' => { - mark: '+', - output: 2.0 - }, - '#-' => { - mark: '-', - output: 0.0 - }, - '#*' => { - mark: '*', - output: 1.0 - }, - '#/' => { - mark: '/', - output: 1.0 - } - } - end - - with_them do - context 'when other is Float' do - let(:input) { '1' } - - before { run_rf("-q 'puts 1.0 #{mark} 1.0'", input) } - - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } - end - - context 'when other is String' do - let(:input) { '1' } - - before { run_rf("-q 'puts 1.0 #{mark} _1'", input) } - - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } - end - end - end - - context 'for Hash' do - describe 'auto accessor' do - let(:input) { '{"foo": "bar"}' } - - where do - { - 'key is exist' => { - command: '_.foo', - output: 'bar' - }, - 'key is not exist' => { - command: '_.piyo.class.to_s', - output: 'NilClass' - } - } - end - - with_them do - before { run_rf("-j -r '#{command}'", input) } - - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } - end - end - end - - describe 'to_json' do - let(:input) { load_fixture('json/hash.json') } - let(:output) do - before { run_rf("-j -q 'puts _.to_json'", input) } - - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } - end - end - - context 'for NilClass' do - describe '#+' do - where do - { - 'Integer' => { - input: %w[1 2 3].join("\n"), - output: '6' - }, - 'Float' => { - input: %w[1.1 2.2 3.3].join("\n"), - output: '6.6' - }, - 'String' => { - input: %w[foo bar baz].join("\n"), - output: 'foobarbaz' - } - } - end - - with_them do - before { run_rf("-q 's+=_1; at_exit { puts s }'", 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 '#<<' do - let(:input) { %w[foo bar baz].join("\n") } - let(:output) { '["foo", "bar", "baz"]' } - - before { run_rf("-q 's <<= _1; at_exit { p s }'", input) } - - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } - end - end -end diff --git a/spec/global_option_spec.rb b/spec/global_option_spec.rb deleted file mode 100644 index f4e2351..0000000 --- a/spec/global_option_spec.rb +++ /dev/null @@ -1,48 +0,0 @@ -describe 'Global options' do - context 'with -R option' do - let(:output) do - <<~OUTPUT - ./a/b/c: abc - ./foo/bar: foobar - OUTPUT - end - - before do - FileUtils.mkdir_p(expand_path('a/b')) - write_file('a/b/c', 'abc') - FileUtils.mkdir_p(expand_path('foo')) - write_file('foo/bar', 'foobar') - - run_rf('-R _ .') - end - - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } - end - - describe '-i/--in-place option' do - let(:output) { '' } - - where(:option, :suffix) do - [ - ['-i', ''], - ['--in-place', ''], - ['-i', '.bak'], - ['--in-place=', '.bak'] - ] - end - - with_them do - before do - write_file('foo', 'foo') - run_rf(%(#{option}#{suffix} '"bar"' foo)) - end - - let(:actucal) { read_file("foo#{suffix}") } - - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } - it { expect(actucal).to eq "bar\n" } - end - end -end diff --git a/spec/help_spec.rb b/spec/global_options/help_spec.rb similarity index 72% rename from spec/help_spec.rb rename to spec/global_options/help_spec.rb index 7517e4a..02fd06c 100644 --- a/spec/help_spec.rb +++ b/spec/global_options/help_spec.rb @@ -1,5 +1,5 @@ describe 'Show help text' do - let(:help_text) do + let(:expect_output) do <<~TEXT Usage: rf [filter type] [options] 'command' file ... rf [filter type] [options] -f program_file file ... @@ -35,24 +35,19 @@ TEXT end - describe '--help' do - before { run_rf('--help') } - - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stdout output_string_eq help_text } + where(:args) do + %w[--help -h] end - context 'when empty option' do - before { run_rf('') } - - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr output_string_eq help_text } + with_them do + it_behaves_like 'a successful exec' end - context 'when not enough option' do - before { run_rf('-t text') } + where(:args) do + ['', '-t text'] + end - it { expect(last_command_started).not_to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stderr output_string_eq help_text } + with_them do + it_behaves_like 'a failed exec' end end diff --git a/spec/global_options/in_place_option_spec.rb b/spec/global_options/in_place_option_spec.rb new file mode 100644 index 0000000..bf24f4b --- /dev/null +++ b/spec/global_options/in_place_option_spec.rb @@ -0,0 +1,27 @@ +describe 'Behavior with in-place option' do + using RSpec::Parameterized::TableSyntax + + where(:opts, :suffix) do + '-i' | '' + '-i' | '.bak' + '--in-place' | '' + '--in-place=' | '.bak' + end + + with_them do + let(:args) do + %(#{opts}#{suffix} '"bar"' foo) + end + let(:expect_output) { '' } + + before do + write_file('foo', 'foo') + end + + it_behaves_like 'a successful exec' do + let(:file_content) { read_file("foo#{suffix}") } + + it { expect(file_content).to eq "bar\n" } + end + end +end diff --git a/spec/global_options/recursive_option_spec.rb b/spec/global_options/recursive_option_spec.rb new file mode 100644 index 0000000..f44f8a8 --- /dev/null +++ b/spec/global_options/recursive_option_spec.rb @@ -0,0 +1,25 @@ +describe 'Behavior with recursive option' do + where(:opts) do + %w[-R --recursive] + end + + with_them do + let(:args) { "#{opts} _ ." } + let(:expect_output) do + <<~OUTPUT + ./a/b/c: abc + ./foo/bar: foobar + OUTPUT + end + + before do + FileUtils.mkdir_p(expand_path('a/b')) + write_file('a/b/c', 'abc') + + FileUtils.mkdir_p(expand_path('foo')) + write_file('foo/bar', 'foobar') + end + + it_behaves_like 'a successful exec' + end +end diff --git a/spec/global_options/version_spec.rb b/spec/global_options/version_spec.rb new file mode 100644 index 0000000..c0431c9 --- /dev/null +++ b/spec/global_options/version_spec.rb @@ -0,0 +1,12 @@ +require_relative '../../mrblib/rf/version' + +describe 'Show version', type: :aruba do + describe '--version' do + let(:args) { '--version' } + let(:expect_output) do + /^rf #{Rf::VERSION} \(mruby \d\.\d\.\d [0-9a-f]+\)$/ + end + + it_behaves_like 'a successful exec' + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6cdb70a..6873dc4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -14,6 +14,8 @@ def load_fixture(path) # need each parallel test to have its own working directory config.include Aruba::Api config.before { setup_aruba } + + Kernel.srand config.seed end def windows? diff --git a/spec/support/aruba.rb b/spec/support/aruba.rb index 0e06554..de666e4 100644 --- a/spec/support/aruba.rb +++ b/spec/support/aruba.rb @@ -18,7 +18,8 @@ def rf_path def run_rf(args, input = nil) @rf_path ||= rf_path - command = run_command("#{rf_path} #{args}") + a = args.is_a?(Array) ? args.join(' ') : args + command = run_command("#{rf_path} #{a}") if input # chomp to remove unintended \n @@ -27,6 +28,13 @@ def run_rf(args, input = nil) end command.wait + + # Call the stop method here to avoid IO waiting on command output. + # This approach efficiently handles the output processing by caching it immediately, + # preventing any potential delays due to IO operations. + command.stop + + command end def read_file(path) diff --git a/spec/support/shared_examples.rb b/spec/support/shared_examples.rb new file mode 100644 index 0000000..5fc6d99 --- /dev/null +++ b/spec/support/shared_examples.rb @@ -0,0 +1,35 @@ +shared_examples 'a successful exec' do |_| + before do + run_rf(args, (input if defined?(input))) + end + + describe 'exit status' do + it 'is success' do + expect(last_command_started).to be_successfully_executed + end + end + + describe 'output' do + it 'is expected output' do + expect(last_command_started.output).to match expect_output + end + end +end + +shared_examples 'a failed exec' do |_| + before do + run_rf(args, (input if defined?(input))) + end + + describe 'exit status' do + it 'is failure' do + expect(last_command_started).not_to be_successfully_executed + end + end + + describe 'output' do + it 'is expected output' do + expect(last_command_started.output).to match expect_output + end + end +end diff --git a/spec/version_spec.rb b/spec/version_spec.rb deleted file mode 100644 index f3ad623..0000000 --- a/spec/version_spec.rb +++ /dev/null @@ -1,14 +0,0 @@ -require_relative '../mrblib/rf/version' - -describe 'Show version', type: :aruba do - describe '--version' do - let(:output) do - /^rf #{Rf::VERSION} \(mruby \d\.\d\.\d [0-9a-f]+\)$/ - end - - before { run_rf('--version') } - - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stdout output } - end -end