Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add grep/grep_v to instance method #155

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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