2020-04-19 00:18:54 +02:00
|
|
|
require "../spec_helper"
|
|
|
|
|
|
|
|
describe CBOR::Lexer do
|
2020-04-19 20:40:10 +02:00
|
|
|
describe "read_token" do
|
|
|
|
describe "reads an int" do
|
|
|
|
tests = [
|
|
|
|
{value: 0, bytes: Bytes[0x00]},
|
|
|
|
{value: 1, bytes: Bytes[0x01]},
|
|
|
|
{value: 10, bytes: Bytes[0x0a]},
|
|
|
|
{value: 23, bytes: Bytes[0x17]},
|
|
|
|
{value: 24, bytes: Bytes[0x18, 0x18]},
|
|
|
|
{value: 25, bytes: Bytes[0x18, 0x19]},
|
|
|
|
{value: 100, bytes: Bytes[0x18, 0x64]},
|
|
|
|
{value: 1000, bytes: Bytes[0x19, 0x03, 0xe8]},
|
|
|
|
{value: 1000000, bytes: Bytes[0x1a, 0x00, 0x0f, 0x42, 0x40]},
|
|
|
|
{value: 1000000000000, bytes: Bytes[0x1b, 0x00, 0x00, 0x00, 0xe8, 0xd4, 0xa5, 0x10, 0x00]},
|
|
|
|
{value: -1, bytes: Bytes[0x20]},
|
|
|
|
{value: -10, bytes: Bytes[0x29]},
|
|
|
|
{value: -100, bytes: Bytes[0x38, 0x63]},
|
|
|
|
{value: -1000, bytes: Bytes[0x39, 0x03, 0xe7]},
|
|
|
|
]
|
2020-04-19 00:18:54 +02:00
|
|
|
|
2020-04-19 20:40:10 +02:00
|
|
|
tests.each do |tt|
|
|
|
|
it "reads #{tt[:bytes].hexstring} as #{tt[:value].to_s}" do
|
|
|
|
lexer = CBOR::Lexer.new(tt[:bytes])
|
2020-04-19 00:18:54 +02:00
|
|
|
|
2020-04-21 15:02:31 +02:00
|
|
|
token = lexer.read_next
|
|
|
|
token.should_not be_nil
|
|
|
|
next unless token
|
|
|
|
|
2020-04-22 11:17:06 +02:00
|
|
|
token.kind.should eq(CBOR::Kind::Int)
|
|
|
|
token.value.as(Int).should eq(tt[:value])
|
2020-04-19 20:40:10 +02:00
|
|
|
end
|
2020-04-19 00:18:54 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|