crystal-cbor/spec/cbor/from_cbor_spec.cr

32 lines
1.2 KiB
Crystal

require "../spec_helper"
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},
{Bool, Bytes[0xf4], false},
{Bool, Bytes[0xf5], true},
{Bytes, Bytes[0x44, 0x01, 0x02, 0x03, 0x04], Bytes[0x01, 0x02, 0x03, 0x04]},
{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)},
{Time, Bytes[0xc1, 0xfb, 0x41, 0xd4, 0x52, 0xd9, 0xec, 0x20, 0x00, 0x00], Time.unix_ms((BigFloat.new(1363896240.5) * 1000).to_u64)},
]
tests.each do |tt|
type, bytes, want = tt
it "decodes #{type}" do
res = type.from_cbor(bytes)
res.should eq(want)
end
end
end
end