Skip to content

Commit 6283813

Browse files
committed
Add TypeRegistry specs
1 parent 112ddf4 commit 6283813

2 files changed

Lines changed: 145 additions & 3 deletions

File tree

gems/smithy-schema/lib/smithy-schema/type_registry.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def register(*shapes)
2323
shapes.each do |s|
2424
@registry[s.id] = s
2525

26-
case s.class
26+
case s
2727
when Shapes::StructureShape
2828
@schema_by_types[s.type] = s if s.type
2929
when Shapes::UnionShape
@@ -50,14 +50,14 @@ def schema_by_id(id)
5050
# with the given typed shape.
5151
# @param [Class] type
5252
# @return [Boolean]
53-
def scheme_by_type?(type)
53+
def schema_by_type?(type)
5454
@schema_by_types.key?(type)
5555
end
5656

5757
# Returns the shape schema registered for the given typed shape.
5858
# @param [Class] type
5959
# @return [Shapes::Shape, nil]
60-
def scheme_by_type(type)
60+
def schema_by_type(type)
6161
@schema_by_types[type]
6262
end
6363

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../spec_helper'
4+
5+
module Smithy
6+
module Schema
7+
describe TypeRegistry do
8+
subject do
9+
registry = TypeRegistry.new
10+
registry.register(shape)
11+
registry
12+
end
13+
14+
let(:runtime_shape) do
15+
Struct.new(:string, keyword_init: true) do
16+
include Smithy::Schema::Structure
17+
end
18+
end
19+
20+
let(:fake_shape) do
21+
Struct.new(:foo, keyword_init: true) do
22+
include Smithy::Schema::Structure
23+
end
24+
end
25+
26+
let(:shape) do
27+
shape = Shapes::StructureShape.new(id: 'thing')
28+
string = Shapes::StringShape.new(id: 'smithy.api#String')
29+
shape.add_member(:string, 'stringMember', string)
30+
shape.type = runtime_shape
31+
shape
32+
end
33+
34+
describe '#initialize' do
35+
subject { TypeRegistry.new }
36+
it 'defaults to empty registry' do
37+
expect(subject.registry).to be_empty
38+
end
39+
end
40+
41+
describe '#register' do
42+
it 'register a schema' do
43+
subject.register(Shapes::StructureShape.new(id: 'thing2'))
44+
expect(subject.registry).to include('thing2')
45+
end
46+
47+
it 'register an array of schemas' do
48+
subject.register(
49+
Shapes::StructureShape.new(id: 'thing2'),
50+
Shapes::StructureShape.new(id: 'thing3')
51+
)
52+
expect(subject.registry).to include('thing2', 'thing3')
53+
end
54+
55+
it 'raises when invalid input is given' do
56+
expect do
57+
subject.register(1, 2)
58+
end.to raise_error(ArgumentError)
59+
end
60+
end
61+
62+
describe '#schema_by_id?' do
63+
it 'returns true if registered' do
64+
expect(subject.schema_by_id?('thing')).to be true
65+
end
66+
67+
it 'returns false if not registered' do
68+
expect(subject.schema_by_id?('unknown')).to be false
69+
end
70+
end
71+
72+
describe '#schema_by_id' do
73+
it 'returns schema' do
74+
expect(subject.schema_by_id('thing')).to be(shape)
75+
end
76+
77+
it 'returns nil if schema is not found' do
78+
expect(subject.schema_by_id('unknown')).to be_nil
79+
end
80+
end
81+
82+
describe '#schema_by_type?' do
83+
it 'returns true if registered' do
84+
expect(subject.schema_by_type?(runtime_shape)).to be true
85+
end
86+
87+
it 'returns false if not registered' do
88+
expect(subject.schema_by_type?(fake_shape)).to be false
89+
end
90+
end
91+
92+
describe '#schema_by_type' do
93+
it 'returns schema' do
94+
expect(subject.schema_by_type(runtime_shape)).to be(shape)
95+
end
96+
97+
it 'returns nil if schema is not found' do
98+
expect(subject.schema_by_type(fake_shape)).to be_nil
99+
end
100+
end
101+
102+
describe '#convert_as_typed' do
103+
it 'returns a typed shape' do
104+
document = Document.new(runtime_shape.new(string: 'foo'), schema: shape)
105+
typed_shape = subject.convert_as_typed(document)
106+
expect(typed_shape).to be_a(runtime_shape)
107+
expect(typed_shape[:string]).to eq('foo')
108+
end
109+
110+
it 'raises when given document does not have a discriminator' do
111+
expect do
112+
subject.convert_as_typed(Document.new('foo'))
113+
end.to raise_error(ArgumentError)
114+
end
115+
116+
it 'raises when given document discriminator is not found' do
117+
shape = Shapes::StructureShape.new(id: 'thing2')
118+
shape.type = runtime_shape
119+
doc = Document.new(runtime_shape.new(string: 'foo'), schema: shape)
120+
expect do
121+
subject.convert_as_typed(doc)
122+
end.to raise_error(ArgumentError)
123+
end
124+
end
125+
126+
describe '.compose' do
127+
it 'returns a combined registry' do
128+
registry = TypeRegistry.new
129+
registry.register(Shapes::StructureShape.new(id: 'thing2'))
130+
new_registry = TypeRegistry.compose(subject, registry)
131+
expect(new_registry.registry).to include('thing', 'thing2')
132+
end
133+
134+
it 'raises when invalid input is given' do
135+
expect do
136+
TypeRegistry.compose(1, 2)
137+
end.to raise_error(ArgumentError)
138+
end
139+
end
140+
end
141+
end
142+
end

0 commit comments

Comments
 (0)