crystal-cbor/spec/cbor_any.cr

34 lines
1.0 KiB
Crystal

require "./spec_helper"
describe CBOR do
describe "CBOR::Any" do
it "from JSON::Any - 1" do
h_any = {"a" => "b", "c" => "d", "e" => true, "f" => 10}
json_any = JSON.parse h_any.to_json
cbor_any = CBOR::Any.new json_any
cbor_any.to_cbor.hexstring.should eq "a461616162616361646165f561660a"
end
it "from JSON::Any - 2" do
h_any = {"a" => "b", "c" => "d", "e" => true, "f" => 10}
json_any = JSON.parse h_any.to_json
cbor_any = CBOR::Any.new json_any
cbor_any["f"].should eq 10
end
it "from array" do
array = CBOR::Any.from_cbor [ "a", "b", "c", "d" ].to_cbor
array.to_cbor.hexstring.should eq "846161616261636164"
end
it "from hash" do
h_cany = Hash(String | Int32, String | Int32).new
h_cany["name"] = "Alice"
h_cany["age"] = 30
h_cany["size"] = 160
cbor_any_hash = CBOR::Any.from_cbor h_cany.to_cbor
cbor_any_hash.to_cbor.hexstring.should eq "a3646e616d6565416c69636563616765181e6473697a6518a0"
end
end
end