diff --git a/mrblib/rf/container.rb b/mrblib/rf/container.rb index 14ef003..0a60b7e 100644 --- a/mrblib/rf/container.rb +++ b/mrblib/rf/container.rb @@ -38,6 +38,10 @@ def hash? _.instance_of?(Hash) end + def array? + _.instance_of?(Array) + end + def puts(*) $output.write(generate_line_prefix) $output.puts(*) @@ -62,6 +66,18 @@ def generate_line_prefix end end + %i[dig].each do |sym| + define_method(sym) do |*args| + _.__send__(sym, *args) if hash? + end + end + + %i[grep grep_v].each do |sym| + define_method(sym) do |*args, &block| + _.__send__(sym, *args, &block) if array? + end + end + def match(condition) regexp = if condition.is_a?(Regexp) condition @@ -83,12 +99,6 @@ def match?(condition) end alias m? match? - %i[dig].each do |sym| - define_method(sym) do |*args| - _.__send__(sym, *args) if hash? - end - end - def respond_to_missing?(sym, *) # check for _0, _1, _2, _3, ... sym.to_s =~ /\A_(0|[1-9]\d*)\z/ || super diff --git a/spec/container/instance_methods_spec.rb b/spec/container/instance_methods_spec.rb index e6550a9..f1bfcac 100644 --- a/spec/container/instance_methods_spec.rb +++ b/spec/container/instance_methods_spec.rb @@ -27,6 +27,20 @@ it_behaves_like 'a successful exec' end + where(:command, :expect_output) do + 'grep(/foo/)' | %w[foo].join("\n") + 'grep_v(/bar/)' | %w[foo baz].join("\n") + 'grep(/foo/){|i| i + "hoge" }' | %w[foohoge].join("\n") + 'grep_v(/bar/){|i| i+ "hoge" }' | %w[foohoge bazhoge].join("\n") + end + + with_them do + let(:input) { %w[foo bar baz].join("\n") } + let(:args) { %(-s '#{command}') } + + it_behaves_like 'a successful exec' + end + %w[match m].each do |method| describe "Container##{method}" do where do @@ -214,67 +228,3 @@ end 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