Skip to content

Commit

Permalink
Merge pull request #155 from buty4649/add-grep-and-grep_v-to-instance…
Browse files Browse the repository at this point in the history
…-method

Add grep/grep_v to instance method
  • Loading branch information
buty4649 authored Jan 27, 2024
2 parents 6c02314 + 14afa26 commit 781081e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 70 deletions.
22 changes: 16 additions & 6 deletions mrblib/rf/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(*)
Expand All @@ -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
Expand All @@ -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
Expand Down
78 changes: 14 additions & 64 deletions spec/container/instance_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 781081e

Please sign in to comment.