Skip to content

Commit

Permalink
Preparation for rggen/rggen#221 (#155)
Browse files Browse the repository at this point in the history
* support custom error position

* create input featurs by using all arguments
  • Loading branch information
taichi-ishitani authored Jan 21, 2025
1 parent 60250b9 commit 82e85d9
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/rggen/core/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module DSL
:enable_all,
:delete,
:setup_plugin,
:update_plugin,
:update_plugin
].each do |method_name|
def_delegator :'RgGen.builder', method_name
end
Expand Down
13 changes: 7 additions & 6 deletions lib/rggen/core/input_base/component_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,20 @@ def create_input_data(*_args, &)
end

def create_features(component, *sources)
create_active_features(component, sources.last)
create_passive_features(component)
create_active_features(component, sources)
create_passive_features(component, sources)
end

def create_active_features(component, input_data)
def create_active_features(component, sources)
active_feature_factories.each do |name, factory|
create_feature(component, factory, input_data[name])
input_data = sources.last[name]
create_feature(component, factory, *sources[0..-2], input_data)
end
end

def create_passive_features(component)
def create_passive_features(component, sources)
passive_feature_factories.each_value do |factory|
create_feature(component, factory)
create_feature(component, factory, *sources[0..-2])
end
end

Expand Down
11 changes: 9 additions & 2 deletions lib/rggen/core/input_base/verifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def message(&block)
@message = block
end

def position(&block)
@position = block
end

def verify(feature, *values)
if @error_checker
feature.instance_exec(*values, &@error_checker)
Expand All @@ -31,8 +35,11 @@ def verify(feature, *values)
private

def default_error_check(feature, values)
feature.instance_exec(*values, &@condition) &&
feature.__send__(:error, feature.instance_exec(*values, &@message))
return unless feature.instance_exec(*values, &@condition)

message = feature.instance_exec(*values, &@message)
position = @position && feature.instance_exec(*values, &@position)
feature.__send__(:error, message, position)
end
end
end
Expand Down
49 changes: 40 additions & 9 deletions spec/rggen/core/input_base/component_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,41 @@ def find_child_factory(*args)

describe 'フィーチャーの生成' do
describe '能動フィーチャーの生成' do
it '生成したコンポーネントと、引数の末尾から取り出した入力値を引数として、能動フィーチャーを生成する' do
expect(foo_feature_factories[:foo_0]).to receive(:create).with(equal(component), equal(input_data[:foo_0])).and_call_original
expect(foo_feature_factories[:foo_1]).to receive(:create).with(equal(component), equal(input_data[:foo_1])).and_call_original
it '生成したコンポーネントと、引数の末尾を入力値として、能動フィーチャーを生成する' do
expect(foo_feature_factories[:foo_0])
.to receive(:create)
.with(equal(component), equal(input_data[:foo_0]))
.and_call_original
expect(foo_feature_factories[:foo_1])
.to receive(:create)
.with(equal(component), equal(input_data[:foo_1]))
.and_call_original
foo_factory.create(parent, input_data)

expect(foo_feature_factories[:foo_0])
.to receive(:create)
.with(equal(component), equal(other_input_data), equal(input_data[:foo_0]))
.and_call_original
expect(foo_feature_factories[:foo_1])
.to receive(:create)
.with(equal(component), equal(other_input_data), equal(input_data[:foo_1]))
.and_call_original
foo_factory.create(parent, other_input_data, input_data)
end
end

describe '受動フィーチャーの生成' do
it '生成したコンポーネントを引数として、受動フィーチャーを生成する' do
expect(foo_feature_factories[:foo_2]).to receive(:create).with(equal(component)).and_call_original
it '生成したコンポーネントと与えられた引数の末尾以外を引数として、受動フィーチャーを生成する' do
expect(foo_feature_factories[:foo_2])
.to receive(:create)
.with(equal(component))
.and_call_original
foo_factory.create(parent, input_data)

expect(foo_feature_factories[:foo_2])
.to receive(:create)
.with(equal(component), equal(other_input_data))
.and_call_original
foo_factory.create(parent, other_input_data, input_data)
end
end
Expand Down Expand Up @@ -199,8 +224,12 @@ def read_file(file)

foo_factory.create(other_input_data, input_files)

expect(foo_feature_factories[:foo_0]).to have_received(:create).with(anything, equal(input_datas[1][:foo_0]))
expect(bar_factory).to have_received(:create).with(anything, anything, equal(input_datas[0]))
expect(foo_feature_factories[:foo_0])
.to have_received(:create)
.with(anything, anything, equal(input_datas[1][:foo_0]))
expect(bar_factory)
.to have_received(:create)
.with(anything, anything, equal(input_datas[0]))
end
end

Expand All @@ -215,9 +244,11 @@ def read_file(file)
context '空のファイルリストを与えた場合' do
it '欠損値使って、自身の組み立てを行う' do
expect(foo_feature_factories[:foo_0])
.to receive(:create).with(anything, equal(RgGen::Core::InputBase::NAValue))
.to receive(:create)
.with(anything, anything, equal(RgGen::Core::InputBase::NAValue))
expect(foo_feature_factories[:foo_1])
.to receive(:create).with(anything, equal(RgGen::Core::InputBase::NAValue))
.to receive(:create)
.with(anything, anything, equal(RgGen::Core::InputBase::NAValue))
expect(bar_factory)
.not_to receive(:create)
foo_factory.create(other_input_data, [])
Expand Down
6 changes: 3 additions & 3 deletions spec/rggen/core/input_base/feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def extract_value(arg)
message { message_baz_0 }
end

def error(message)
def error(message, _position)
raise message
end
end
Expand Down Expand Up @@ -491,7 +491,7 @@ def error(message)
message { message_baz_1 }
end

def error(message)
def error(message, _position)
raise message
end
end
Expand Down Expand Up @@ -546,7 +546,7 @@ def error(message)
message { message_baz_1 }
end

def error(message)
def error(message, _position)
raise message
end
end
Expand Down
22 changes: 20 additions & 2 deletions spec/rggen/core/input_base/verifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
describe '#verify' do
let(:feature) do
klass = Class.new do
def error(message)
raise message
def error(message, position = nil)
if position
raise "#{message} #{position}"
else
raise message
end
end
end
klass.new
Expand Down Expand Up @@ -43,6 +47,20 @@ def error(message)
expect { verifier.verify(feature, 'bar') }.to raise_error 'bar error!'
end
end

context '#positionが与えられている場合' do
specify '#positionの評価結果がエラーの位置情報になる' do
error_message = 'error!'
error_position = double('error position')
verifier = described_class.new do
error_condition { true }
message { error_message }
position { error_position }
end

expect { verifier.verify(feature) }.to raise_error "#{error_message} #{error_position}"
end
end
end
end
end

0 comments on commit 82e85d9

Please sign in to comment.