Skip to content

Commit a764a09

Browse files
Merge pull request #167 from code0-tech/fix-generic-mappers
Fix GenericType and add some specs
2 parents e1405fc + 015e67b commit a764a09

File tree

2 files changed

+68
-30
lines changed

2 files changed

+68
-30
lines changed

build/ruby/lib/tucana/shared/shared.data_type.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ def from_hash(config)
223223
end
224224

225225
if config.key?(:generic_mappers)
226-
self.generic_mappers = config.fetch(:generic_mappers).map { |mapper_config| GenericMapper.from_hash(mapper_config) }
226+
self.generic_mappers.clear
227+
config[:generic_mappers].each do |mapper_config|
228+
self.generic_mappers << GenericMapper.from_hash(mapper_config)
229+
end
227230
end
228231

229232
self
@@ -245,8 +248,11 @@ def to_h
245248

246249
def from_hash(config)
247250
self.target = config[:target]
248-
self.source = config[:source].map do |source|
249-
DataTypeIdentifier.from_hash(source)
251+
if config.key?(:source)
252+
self.source.clear
253+
config[:source].each do |source|
254+
self.source << DataTypeIdentifier.from_hash(source)
255+
end
250256
end
251257

252258
if config.key?(:generic_combinations)

build/ruby/spec/tucana/shared/shared.data_type_spec.rb

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,38 @@
33
Tucana.load_protocol(:shared)
44

55
RSpec.describe Tucana::Shared::DefinitionDataTypeRule do
6-
describe "#create" do
6+
describe '#from_hash' do
7+
context 'GenericType#from_hash' do
8+
it do
9+
generic_type = Tucana::Shared::GenericType.new.from_hash({
10+
data_type_identifier: 'SomeIdentifier',
11+
generic_mappers: [
12+
{
13+
source: [{ data_type_identifier: 'SomeType' }],
14+
target: 'T',
15+
generic_combinations: [:AND]
16+
}
17+
]
18+
})
19+
20+
expect(generic_type).not_to be_nil
21+
end
22+
end
23+
context 'GenericMapper#from_hash' do
24+
context 'when type is :map_to_generic_type' do
25+
it do
26+
generic_mapper = Tucana::Shared::GenericMapper.new.from_hash({
27+
source: [{ data_type_identifier: 'SomeType' }],
28+
target: 'T',
29+
generic_combinations: [:AND]
30+
})
31+
32+
expect(generic_mapper.to_s).to eq("<Tucana::Shared::GenericMapper: source: [<Tucana::Shared::DataTypeIdentifier: data_type_identifier: \"SomeType\">], target: \"T\", generic_combinations: [:AND]>")
33+
end
34+
end
35+
end
36+
end
37+
describe '#create' do
738
context 'DataTypeIdentifier#to_h' do
839
context 'generic_key' do
940
it do
@@ -37,75 +68,76 @@
3768
end
3869
end
3970

40-
context "with :contains_key variant" do
41-
it "sets the contains_key field" do
42-
config = { key: "test_key", data_type_identifier: { data_type_identifier: "test_type" } }
71+
context 'with :contains_key variant' do
72+
it 'sets the contains_key field' do
73+
config = { key: 'test_key', data_type_identifier: { data_type_identifier: 'test_type' } }
4374
rule = described_class.create(:contains_key, config)
4475
expect(rule.contains_key).to be_a(Tucana::Shared::DefinitionDataTypeContainsKeyRuleConfig)
4576
end
4677
end
4778

48-
context "with :contains_type variant" do
49-
it "sets the contains_type field" do
50-
config = { data_type_identifier: { data_type_identifier: "test_type" } }
79+
context 'with :contains_type variant' do
80+
it 'sets the contains_type field' do
81+
config = { data_type_identifier: { data_type_identifier: 'test_type' } }
5182
rule = described_class.create(:contains_type, config)
5283
expect(rule.contains_type).to be_a(Tucana::Shared::DefinitionDataTypeContainsTypeRuleConfig)
5384
end
5485
end
5586

56-
context "with :item_of_collection variant" do
57-
it "sets the item_of_collection field" do
87+
context 'with :item_of_collection variant' do
88+
it 'sets the item_of_collection field' do
5889
config = { items: %w[item1 item2] }
5990
rule = described_class.create(:item_of_collection, config)
6091
expect(rule.item_of_collection).to be_a(Tucana::Shared::DataTypeItemOfCollectionRuleConfig)
6192
end
6293
end
6394

64-
context "with :number_range variant" do
65-
it "sets the number_range field" do
95+
context 'with :number_range variant' do
96+
it 'sets the number_range field' do
6697
config = { from: 1, to: 10 }
6798
rule = described_class.create(:number_range, config)
6899
expect(rule.number_range).to be_a(Tucana::Shared::DataTypeNumberRangeRuleConfig)
69100
end
70101
end
71102

72-
context "with :regex variant" do
73-
it "sets the regex field" do
74-
config = { pattern: "\\d+" }
103+
context 'with :regex variant' do
104+
it 'sets the regex field' do
105+
config = { pattern: '\\d+' }
75106
rule = described_class.create(:regex, config)
76107
expect(rule.regex).to be_a(Tucana::Shared::DataTypeRegexRuleConfig)
77108
end
78109
end
79110

80-
context "with :input_types variant" do
81-
it "sets the input_types field" do
82-
config = { input_types: [{ data_type_identifier: { data_type_identifier: "test_type" }, input_identifier: "test_input" }] }
111+
context 'with :input_types variant' do
112+
it 'sets the input_types field' do
113+
config = { input_types: [{ data_type_identifier: { data_type_identifier: 'test_type' },
114+
input_identifier: 'test_input' }] }
83115
rule = described_class.create(:input_types, config)
84116
expect(rule.input_types).to be_a(Tucana::Shared::DefinitionDataTypeInputTypesRuleConfig)
85117
end
86118
end
87119

88-
context "with :return_type variant" do
89-
it "sets the return_type field" do
90-
config = { data_type_identifier: { data_type_identifier: "test_type" } }
120+
context 'with :return_type variant' do
121+
it 'sets the return_type field' do
122+
config = { data_type_identifier: { data_type_identifier: 'test_type' } }
91123
rule = described_class.create(:return_type, config)
92124
expect(rule.return_type).to be_a(Tucana::Shared::DefinitionDataTypeReturnTypeRuleConfig)
93125
end
94126
end
95127

96-
context "with :parent_type variant" do
97-
it "sets the parent_type field" do
98-
config = { parent_type: { data_type_identifier: "test_type" } }
128+
context 'with :parent_type variant' do
129+
it 'sets the parent_type field' do
130+
config = { parent_type: { data_type_identifier: 'test_type' } }
99131
rule = described_class.create(:parent_type, config)
100132
expect(rule.parent_type).to be_a(Tucana::Shared::DefinitionDataTypeParentTypeRuleConfig)
101133
end
102134
end
103135

104-
context "with unknown variant" do
105-
it "raises UnexpectedRuleType error" do
106-
expect {
136+
context 'with unknown variant' do
137+
it 'raises UnexpectedRuleType error' do
138+
expect do
107139
described_class.create(:unknown, {})
108-
}.to raise_error(Tucana::Shared::UnexpectedRuleType, "Unknown rule type unknown")
140+
end.to raise_error(Tucana::Shared::UnexpectedRuleType, 'Unknown rule type unknown')
109141
end
110142
end
111143
end

0 commit comments

Comments
 (0)