2023-12-13 16:07:57 +07:00
|
|
|
# json-serialization
|
|
|
|
# Copyright (c) 2019-2023 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2023-12-19 12:00:24 +02:00
|
|
|
|
2018-12-18 01:01:06 +02:00
|
|
|
import
|
|
|
|
unittest,
|
2019-01-10 23:20:37 +01:00
|
|
|
../json_serialization/lexer, ./utils
|
2018-12-18 01:01:06 +02:00
|
|
|
|
2020-03-25 15:48:17 +02:00
|
|
|
template expectedToken(token: TokKind, additionalCheck = true) {.dirty.} =
|
2018-12-18 01:01:06 +02:00
|
|
|
lexer.next()
|
|
|
|
check:
|
|
|
|
lexer.tok == token
|
2020-03-25 15:48:17 +02:00
|
|
|
bool(additionalCheck)
|
2018-12-18 01:01:06 +02:00
|
|
|
|
2020-05-05 20:28:44 +03:00
|
|
|
template lexerTest(name, inputParam: string, expectations) {.dirty.} =
|
2018-12-18 01:01:06 +02:00
|
|
|
test name:
|
2021-12-15 10:34:49 +01:00
|
|
|
var input = test_dedent(inputParam)
|
2020-05-05 20:28:44 +03:00
|
|
|
var stream = unsafeMemoryInput(input)
|
2018-12-18 01:01:06 +02:00
|
|
|
var lexer = JsonLexer.init stream
|
|
|
|
expectations
|
|
|
|
|
|
|
|
template `=~`(lhs, rhs: float): bool =
|
|
|
|
abs(lhs - rhs) < 0.01
|
|
|
|
|
|
|
|
suite "lexer tests":
|
|
|
|
lexerTest "object with simple fields", """
|
|
|
|
{
|
|
|
|
"x": 10,
|
|
|
|
"y": "test"
|
|
|
|
}
|
|
|
|
""":
|
|
|
|
expectedToken tkCurlyLe
|
|
|
|
expectedToken tkString, lexer.strVal == "x"
|
|
|
|
expectedToken tkColon
|
2020-03-25 15:48:17 +02:00
|
|
|
expectedToken tkInt, lexer.absIntVal == 10
|
2018-12-18 01:01:06 +02:00
|
|
|
expectedToken tkComma
|
|
|
|
expectedToken tkString, lexer.strVal == "y"
|
|
|
|
expectedToken tkColon
|
|
|
|
expectedToken tkString, lexer.strVal == "test"
|
|
|
|
expectedToken tkCurlyRi
|
|
|
|
expectedToken tkEof
|
|
|
|
expectedToken tkEof # check that reading past the end is benign
|
|
|
|
|
|
|
|
lexerTest "int literal", "190":
|
2020-03-25 15:48:17 +02:00
|
|
|
expectedToken tkInt, lexer.absIntVal == 190
|
2018-12-18 01:01:06 +02:00
|
|
|
expectedToken tkEof
|
|
|
|
|
2019-08-14 18:34:59 +07:00
|
|
|
lexerTest "int64 literal", "3568257348920230622":
|
2020-03-25 15:48:17 +02:00
|
|
|
expectedToken tkInt, lexer.absIntVal == 3568257348920230622'u64
|
2019-08-14 18:34:59 +07:00
|
|
|
expectedToken tkEof
|
|
|
|
|
2018-12-18 01:01:06 +02:00
|
|
|
lexerTest "float literal", ".340":
|
|
|
|
expectedToken tkFloat, lexer.floatVal =~ 0.340
|
|
|
|
expectedToken tkEof
|
|
|
|
|
|
|
|
lexerTest "string literal", "\"hello\"":
|
|
|
|
expectedToken tkString, lexer.strVal == "hello"
|
|
|
|
expectedToken tkEof
|
|
|
|
|
|
|
|
lexerTest "mixed array", "[1, 2.0, \"test\", {}, [],]":
|
|
|
|
expectedToken tkBracketLe
|
2020-03-25 15:48:17 +02:00
|
|
|
expectedToken tkInt, lexer.absIntVal == 1
|
2018-12-18 01:01:06 +02:00
|
|
|
expectedToken tkComma
|
|
|
|
expectedToken tkFloat, lexer.floatVal =~ 2.0
|
|
|
|
expectedToken tkComma
|
|
|
|
expectedToken tkString, lexer.strVal == "test"
|
|
|
|
expectedToken tkComma
|
|
|
|
expectedToken tkCurlyLe
|
|
|
|
expectedToken tkCurlyRi
|
|
|
|
expectedToken tkComma
|
|
|
|
expectedToken tkBracketLe
|
|
|
|
expectedToken tkBracketRi
|
|
|
|
expectedToken tkComma
|
|
|
|
expectedToken tkBracketRi
|
|
|
|
expectedToken tkEof
|
|
|
|
|