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 prepend option #150

Merged
merged 1 commit into from
Jan 13, 2025
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
6 changes: 3 additions & 3 deletions lib/rggen/core/input_base/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def input_pattern(pattern_or_patterns, ...)

attr_reader :input_matcher

def verify(scope, &)
@verifiers ||= {}
(@verifiers[scope] ||= []) << create_verifier(&)
def verify(scope, prepend: false, &)
((@verifiers ||= {})[scope] ||= [])
.__send__(prepend && :prepend || :push, create_verifier(&))
end

attr_reader :verifiers
Expand Down
110 changes: 110 additions & 0 deletions spec/rggen/core/input_base/feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,116 @@ def error(message)
create_feature.verify(:all)
}.not_to raise_error
end

context 'prependオプションの指定がない場合' do
it '先に登録され多検証ブロックの結果が優先される' do
feature = create_feature do
verify(:feature) do
error_condition { condition_foo_0 }
message { message_foo_0 }
end
verify(:feature) do
error_condition { condition_foo_1 }
message { message_foo_1 }
end

verify(:component) do
error_condition { condition_bar_0 }
message { message_bar_0 }
end
verify(:component) do
error_condition { condition_bar_1 }
message { message_bar_1 }
end

verify(:all) do
error_condition { condition_baz_0 }
message { message_baz_0 }
end
verify(:all) do
error_condition { condition_baz_1 }
message { message_baz_1 }
end

def error(message)
raise message
end
end

allow(feature).to receive(:condition_foo_0).and_return(true)
allow(feature).to receive(:message_foo_0).and_return('error foo 0')
allow(feature).to receive(:condition_foo_1).and_return(true)
allow(feature).to receive(:message_foo_1).and_return('error foo 1')
expect { feature.verify(:feature) }.to raise_error('error foo 0')

allow(feature).to receive(:condition_bar_0).and_return(true)
allow(feature).to receive(:message_bar_0).and_return('error bar 0')
allow(feature).to receive(:condition_bar_1).and_return(true)
allow(feature).to receive(:message_bar_1).and_return('error bar 1')
expect { feature.verify(:component) }.to raise_error('error bar 0')

allow(feature).to receive(:condition_baz_0).and_return(true)
allow(feature).to receive(:message_baz_0).and_return('error baz 0')
allow(feature).to receive(:condition_baz_1).and_return(true)
allow(feature).to receive(:message_baz_1).and_return('error baz 1')
expect { feature.verify(:all) }.to raise_error('error baz 0')
end
end

context 'prependオプションの指定がある場合' do
it '後に登録され多検証ブロックの結果が優先される' do
feature = create_feature do
verify(:feature) do
error_condition { condition_foo_0 }
message { message_foo_0 }
end
verify(:feature, prepend: true) do
error_condition { condition_foo_1 }
message { message_foo_1 }
end

verify(:component) do
error_condition { condition_bar_0 }
message { message_bar_0 }
end
verify(:component, prepend: true) do
error_condition { condition_bar_1 }
message { message_bar_1 }
end

verify(:all) do
error_condition { condition_baz_0 }
message { message_baz_0 }
end
verify(:all, prepend: true) do
error_condition { condition_baz_1 }
message { message_baz_1 }
end

def error(message)
raise message
end
end

allow(feature).to receive(:condition_foo_0).and_return(true)
allow(feature).to receive(:message_foo_0).and_return('error foo 0')
allow(feature).to receive(:condition_foo_1).and_return(true)
allow(feature).to receive(:message_foo_1).and_return('error foo 1')
expect { feature.verify(:feature) }.to raise_error('error foo 1')

allow(feature).to receive(:condition_bar_0).and_return(true)
allow(feature).to receive(:message_bar_0).and_return('error bar 0')
allow(feature).to receive(:condition_bar_1).and_return(true)
allow(feature).to receive(:message_bar_1).and_return('error bar 1')
expect { feature.verify(:component) }.to raise_error('error bar 1')

allow(feature).to receive(:condition_baz_0).and_return(true)
allow(feature).to receive(:message_baz_0).and_return('error baz 0')
allow(feature).to receive(:condition_baz_1).and_return(true)
allow(feature).to receive(:message_baz_1).and_return('error baz 1')
expect { feature.verify(:all) }.to raise_error('error baz 1')
end
end
end

describe '#printables' do
Expand Down
Loading