Skip to content

Commit

Permalink
raise error when unknown field is given
Browse files Browse the repository at this point in the history
  • Loading branch information
taichi-ishitani committed Jul 20, 2024
1 parent 9394e90 commit 3586856
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 19 deletions.
6 changes: 3 additions & 3 deletions lib/rggen/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
require_relative 'core/base/feature_factory'
require_relative 'core/base/feature_layer_extension'

require_relative 'core/input_base/error'
require_relative 'core/input_base/input_value'
require_relative 'core/input_base/input_data'
require_relative 'core/input_base/input_value_extractor'
require_relative 'core/input_base/error'
require_relative 'core/input_base/conversion_utility'
require_relative 'core/input_base/loader'
require_relative 'core/input_base/json_loader'
Expand All @@ -60,8 +60,8 @@
require_relative 'core/input_base/hash_list_parser'
require_relative 'core/input_base/feature_factory'

require_relative 'core/configuration/input_data'
require_relative 'core/configuration/error'
require_relative 'core/configuration/input_data'
require_relative 'core/configuration/component'
require_relative 'core/configuration/component_factory'
require_relative 'core/configuration/feature'
Expand All @@ -74,8 +74,8 @@
require_relative 'core/configuration/yaml_loader'
require_relative 'core/configuration'

require_relative 'core/register_map/input_data'
require_relative 'core/register_map/error'
require_relative 'core/register_map/input_data'
require_relative 'core/register_map/component'
require_relative 'core/register_map/component_factory'
require_relative 'core/register_map/feature'
Expand Down
9 changes: 9 additions & 0 deletions lib/rggen/core/configuration/input_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ module RgGen
module Core
module Configuration
class InputData < InputBase::InputData
include RaiseError

def initialize(valid_value_lists, &block)
super(nil, valid_value_lists, &block)
end

undef_method :child

private

def raise_unknown_field_error(field_name, position)
message = "unknown configuration field is given: #{field_name}"
error(message, position)
end
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/rggen/core/input_base/input_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ def initialize(layer, valid_value_lists, *_args)

def value(value_name, value, position = nil)
symbolized_name = value_name.to_sym
valid_value?(symbolized_name) &&
assign_value(symbolized_name, value, position)
valid_value?(symbolized_name) ||
raise_unknown_field_error(symbolized_name, position)
assign_value(symbolized_name, value, position)
end

def []=(value_name, position = nil, value)
Expand Down
6 changes: 5 additions & 1 deletion lib/rggen/core/input_base/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ def extract_value(read_data, layer, value_name)
end

def filter_layer_data(layer_data, layer)
layer_data.slice(*valid_values(layer))
if @ignore_values.key?(layer)
layer_data.except(*@ignore_values[layer])
else
layer_data
end
end

def format_sub_layer_data(_read_data, _layer, _file)
Expand Down
7 changes: 7 additions & 0 deletions lib/rggen/core/register_map/input_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module RgGen
module Core
module RegisterMap
class InputData < InputBase::InputData
include RaiseError

module Root
def register_block(value_list = nil, &block)
child(:register_block, value_list, &block)
Expand Down Expand Up @@ -51,6 +53,11 @@ def initialize(layer, valid_value_list, configuration)
def create_child_data(layer, &block)
super(layer, @configuration, &block)
end

def raise_unknown_field_error(field_name, position)
message = "unknown register map field is given: #{field_name}"
error(message, position)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rggen/core/configuration/hash_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def read_file(_file)
end

context '#read_dataがHashを返す場合' do
let(:load_data) { { foo: 0, bar: 1, qux: 2} }
let(:load_data) { { foo: 0, bar: 1 } }

before do
loader.load_data = load_data
Expand Down
46 changes: 46 additions & 0 deletions spec/rggen/core/configuration/input_data_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

RSpec.describe RgGen::Core::Configuration::InputData do
describe '#value/#[]=' do
let(:valid_value_lists) do
{ nil => [:foo, :bar] }
end

let(:position) do
Struct.new(:x, :y).new(0, 1)
end

let(:input_data) do
described_class.new(valid_value_lists)
end

def input_value(value)
RgGen::Core::InputBase::InputValue.new(value, position)
end

def raise_configuration_error(message)
raise_error RgGen::Core::Configuration::ConfigurationError, message
end

context '入力値名が入力値リスト上にない場合' do
it 'ConfigurationErrorを起こす' do
expect { input_data.value(:baz, input_value(0)) }
.to raise_configuration_error 'unknown configuration field is given: baz'
expect { input_data.value('baz', input_value(0)) }
.to raise_configuration_error 'unknown configuration field is given: baz'

expect { input_data[:baz] = input_value(0) }
.to raise_configuration_error 'unknown configuration field is given: baz'

expect { input_data['baz'] = input_value(0) }
.to raise_configuration_error 'unknown configuration field is given: baz'

expect { input_data.value(:foo, input_value(0)) }
.not_to raise_error

expect { input_data[:bar] = input_value(1) }
.not_to raise_error
end
end
end
end
14 changes: 2 additions & 12 deletions spec/rggen/core/input_base/input_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ def input_value(value)
end
end

context '入力値名が指定された階層の入力値リスト上にない場合' do
it '入力値の設定をしない' do
foo_data.value :bar_0, bar_values[:bar_0]
foo_data.value :qux_0, 0

expect(foo_data).not_to have_value :bar_0
expect(foo_data).not_to have_value :qux_0
end
end

specify '#[]=でも同様に設定できる' do
foo_data[:foo_0] = foo_values[:foo_0]
foo_data[:foo_1, position] = foo_values[:foo_1]
Expand Down Expand Up @@ -188,8 +178,8 @@ def input_value(value)

describe '入力値名リスト' do
before do
foo_data.child(:bar, foo_0: foo_values[:foo_0], bar_0: bar_values[:bar_0], baz_0: baz_values[:baz_0])
foo_data.children[0].child(:baz, foo_1: foo_values[:foo_1], bar_1: bar_values[:bar_1], baz_1: baz_values[:baz_1])
foo_data.child(:bar, bar_0: bar_values[:bar_0])
foo_data.children[0].child(:baz, baz_1: baz_values[:baz_1])
end

let(:bar_data) { foo_data.children[0] }
Expand Down
74 changes: 74 additions & 0 deletions spec/rggen/core/register_map/input_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,84 @@
}
end

let(:position) do
Struct.new(:x, :y).new(0, 1)
end

def create_input_data(layer, configuration = nil)
described_class.new(layer, valid_value_lists, configuration)
end

def input_value(value)
RgGen::Core::InputBase::InputValue.new(value, position)
end

def raise_register_map_error(message)
raise_error RgGen::Core::RegisterMap::RegisterMapError, message
end

describe '#value/#[]=' do
context '入力値名が入力値リスト上にない場合' do
it 'RegisterMapErrorを起こす' do
input_data = create_input_data(:register_block)
expect { input_data.value(:bar, input_value(0)) }
.to raise_register_map_error 'unknown register map field is given: bar'
expect { input_data.value('bar', input_value(0)) }
.to raise_register_map_error 'unknown register map field is given: bar'
expect { input_data[:bar] = input_value(0) }
.to raise_register_map_error 'unknown register map field is given: bar'
expect { input_data['bar'] = input_value(0) }
.to raise_register_map_error 'unknown register map field is given: bar'
expect { input_data.value(:foo, input_value(0)) }
.not_to raise_error
expect { input_data[:foo] = input_value(0) }
.not_to raise_error

input_data = create_input_data(:register_file)
expect { input_data.value(:foo, input_value(0)) }
.to raise_register_map_error 'unknown register map field is given: foo'
expect { input_data.value('foo', input_value(0)) }
.to raise_register_map_error 'unknown register map field is given: foo'
expect { input_data[:foo] = input_value(0) }
.to raise_register_map_error 'unknown register map field is given: foo'
expect { input_data['foo'] = input_value(0) }
.to raise_register_map_error 'unknown register map field is given: foo'
expect { input_data.value(:bar, input_value(0)) }
.not_to raise_error
expect { input_data[:bar] = input_value(0) }
.not_to raise_error

input_data = create_input_data(:register)
expect { input_data.value(:qux, input_value(0)) }
.to raise_register_map_error 'unknown register map field is given: qux'
expect { input_data.value('qux', input_value(0)) }
.to raise_register_map_error 'unknown register map field is given: qux'
expect { input_data[:qux] = input_value(0) }
.to raise_register_map_error 'unknown register map field is given: qux'
expect { input_data['qux'] = input_value(0) }
.to raise_register_map_error 'unknown register map field is given: qux'
expect { input_data.value(:baz, input_value(0)) }
.not_to raise_error
expect { input_data[:baz] = input_value(0) }
.not_to raise_error

input_data = create_input_data(:bit_field)
expect { input_data.value(:baz, input_value(0)) }
.to raise_register_map_error 'unknown register map field is given: baz'
expect { input_data.value('baz', input_value(0)) }
.to raise_register_map_error 'unknown register map field is given: baz'
expect { input_data[:baz] = input_value(0) }
.to raise_register_map_error 'unknown register map field is given: baz'
expect { input_data['baz'] = input_value(0) }
.to raise_register_map_error 'unknown register map field is given: baz'
expect { input_data.value(:qux, input_value(0)) }
.not_to raise_error
expect { input_data[:qux] = input_value(0) }
.not_to raise_error
end
end
end

context '階層がrootの場合' do
let(:input_data) do
create_input_data(:root)
Expand Down

0 comments on commit 3586856

Please sign in to comment.