2020-04-23 00:25:11 +02:00
|
|
|
require "../spec_helper"
|
|
|
|
|
2020-04-25 17:33:19 +02:00
|
|
|
enum TestEnum
|
|
|
|
Foo = 1
|
|
|
|
end
|
|
|
|
|
2020-04-23 00:25:11 +02:00
|
|
|
describe "CBOR helpers on basic types" do
|
|
|
|
describe "#from_cbor" do
|
|
|
|
tests = [
|
|
|
|
{String, Bytes[0x61, 0x61], "a"},
|
|
|
|
{UInt8, Bytes[0x18, 0x18], 24},
|
|
|
|
{UInt16, Bytes[0x19, 0x03, 0xe8], 1000},
|
|
|
|
{UInt32, Bytes[0x1a, 0x00, 0x0f, 0x42, 0x40], 1000000},
|
|
|
|
{UInt64, Bytes[0x1b, 0x00, 0x00, 0x00, 0xe8, 0xd4, 0xa5, 0x10, 0x00], 1000000000000},
|
|
|
|
{Int8, Bytes[0x29], -10},
|
2020-04-24 15:59:16 +02:00
|
|
|
{Bool, Bytes[0xf4], false},
|
|
|
|
{Bool, Bytes[0xf5], true},
|
2020-04-23 00:25:11 +02:00
|
|
|
{Bytes, Bytes[0x44, 0x01, 0x02, 0x03, 0x04], Bytes[0x01, 0x02, 0x03, 0x04]},
|
2020-04-24 15:59:16 +02:00
|
|
|
{Time,
|
|
|
|
Bytes[0xc0, 0x74, 0x32, 0x30, 0x31, 0x33, 0x2d, 0x30, 0x33, 0x2d, 0x32, 0x31, 0x54, 0x32, 0x30, 0x3a, 0x30, 0x34, 0x3a, 0x30, 0x30, 0x5a],
|
|
|
|
Time::Format::RFC_3339.parse("2013-03-21T20:04:00Z")},
|
|
|
|
{Time, Bytes[0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0], Time.unix(1363896240)},
|
2020-04-24 22:46:07 +02:00
|
|
|
{Time, Bytes[0xc1, 0xfb, 0x41, 0xd4, 0x52, 0xd9, 0xec, 0x20, 0x00, 0x00], Time.unix_ms((BigFloat.new(1363896240.5) * 1000).to_u64)},
|
2020-04-24 22:50:48 +02:00
|
|
|
{Nil, Bytes[0xf6], nil},
|
|
|
|
{Nil, Bytes[0xf7], nil},
|
2020-04-24 22:57:24 +02:00
|
|
|
{Float32, Bytes[0xfb, 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a], 1.1_f32},
|
|
|
|
{Float64, Bytes[0xfa, 0x47, 0xc3, 0x50, 0x00], 100000.0_f64},
|
2020-04-25 17:27:01 +02:00
|
|
|
{Set(Int8), Bytes[0x83, 0x01, 0x02, 0x03], Set(Int8){1, 2, 3}},
|
2020-04-24 23:43:57 +02:00
|
|
|
{Array(Int8), Bytes[0x83, 0x01, 0x02, 0x03], [1_i8, 2_i8, 3_i8]},
|
2020-04-25 17:27:01 +02:00
|
|
|
# {Array(Array(Int8) | Int8),
|
|
|
|
# Bytes[0x83, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05],
|
|
|
|
# [1_i8, [2_i8, 3_i8], [4_i8, 5_i8]]},
|
2020-04-24 23:43:57 +02:00
|
|
|
{Array(UInt8), Bytes[0x9f, 0xff], [] of UInt8},
|
|
|
|
# {Array(Array(Int8) | Int8),
|
|
|
|
# Bytes[0x9f, 0x01, 0x82, 0x02, 0x03, 0x9f, 0x04, 0x05, 0xff, 0xff],
|
|
|
|
# [1_i8, [2_i8, 3_i8], [4_i8, 5_i8]]},
|
2020-04-25 17:27:01 +02:00
|
|
|
{Hash(UInt8, UInt8), Bytes[0xa0], {} of UInt8 => UInt8},
|
|
|
|
{Hash(UInt8, UInt8), Bytes[0xa2, 0x01, 0x02, 0x03, 0x04], Hash(UInt8, UInt8){1 => 2, 3 => 4}},
|
2020-04-25 17:33:19 +02:00
|
|
|
{TestEnum, Bytes[0x1a, 0x00, 0x00, 0x00, 0x01], TestEnum::Foo},
|
2020-04-25 17:44:14 +02:00
|
|
|
{Tuple(Int8, Int8), Bytes[0x82, 0x01, 0x02], {1_i8, 2_i8}},
|
2020-04-25 18:13:56 +02:00
|
|
|
{NamedTuple(a: UInt8, b: Array(UInt8)), Bytes[0xa2, 0x61, 0x61, 0x01, 0x61, 0x62, 0x82, 0x02, 0x03], {a: 1_u8, b: [2_u8, 3_u8]}},
|
2020-04-25 18:35:07 +02:00
|
|
|
{BigInt, Bytes[0xc2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], BigInt.new("18446744073709551616")},
|
2020-05-08 11:40:18 +02:00
|
|
|
{BigInt, Bytes[0x18, 0x18], BigInt.new(24)}, # BigInt from int
|
|
|
|
{BigDecimal, Bytes[0xc4, 0x82, 0x21, 0x19, 0x6a, 0xb3], BigDecimal.new(273.15)},
|
|
|
|
{BigDecimal, Bytes[0xc5, 0x82, 0x20, 0x03], BigDecimal.new(1.5)},
|
2020-04-23 00:25:11 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
tests.each do |tt|
|
|
|
|
type, bytes, want = tt
|
|
|
|
|
2020-04-24 22:46:07 +02:00
|
|
|
it "decodes #{type}" do
|
2020-04-23 00:25:11 +02:00
|
|
|
res = type.from_cbor(bytes)
|
|
|
|
res.should eq(want)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|