Skip to content

Commit

Permalink
Add cbor union shape testing?
Browse files Browse the repository at this point in the history
  • Loading branch information
jterapin committed Feb 27, 2025
1 parent a3e14a8 commit 3b6fc00
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions gems/smithy-client/spec/smithy-client/codecs/cbor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
module Smithy
module Client
module Codecs
class TestUnion < Schema::Union
class StringValue < TestUnion
def to_h
{ string_value: super(__getobj__) }
end
end

class Unknown < TestUnion
def initialize(name:, value:)
super({ name: name || 'Unknown', value: value })
end

def to_h
{ unknown: super(__getobj__) }
end
end
end

describe CBOR do
let(:string_shape) { Schema::Shapes::StringShape.new(id: 'string') }

Expand All @@ -20,20 +38,27 @@ module Codecs
end

let(:typed_struct) do
Struct.new(:s, :l, :m, keyword_init: true) do
Struct.new(:foo, :bar, :foo_bar, keyword_init: true) do
include Schema::Structure
end
end

let(:structure_shape) do
struct = Schema::Shapes::StructureShape.new(id: 'structure')
struct.add_member(:s, 's', string_shape)
struct.add_member(:l, 'l', list_shape)
struct.add_member(:m, 'm', map_shape)
struct.add_member(:foo, 'foo', string_shape)
struct.add_member(:bar, 'bar', list_shape)
struct.add_member(:foo_bar, 'fooBar', map_shape)
struct.type = typed_struct
struct
end

let(:union_shape) do
shape = Schema::Shapes::UnionShape.new(id: 'union')
shape.add_member(:s, 's', string_shape, TestUnion::StringValue)
shape.type = TestUnion
shape
end

it 'serializes returns nil when given shape is Prelude::Unit' do
expect(subject.serialize(Schema::Shapes::Prelude::Unit, '')).to be_nil
end
Expand All @@ -43,10 +68,16 @@ module Codecs
end

it 'serializes and deserializes a complex data' do
typed = typed_struct.new(s: 'foo', l: %w[foo bar], m: { 'foo' => 'bar'.dup })
typed = typed_struct.new(foo: 'foo', bar: %w[foo bar], foo_bar: { 'foo' => 'bar'.dup })
serialized = subject.serialize(structure_shape, typed)
expect(subject.deserialize(structure_shape, serialized)).to eq(typed)
end

it 'serializes and deserializes a union data' do
typed = { s: 'foo' }
serialized = subject.serialize(union_shape, typed)
expect(subject.deserialize(union_shape, serialized)).to eq(typed)
end
end
end
end
Expand Down

0 comments on commit 3b6fc00

Please sign in to comment.