all serialization tests green again

This commit is contained in:
Felix Krause 2020-11-10 15:40:01 +01:00
parent 4305bccbf0
commit 9d92e8a2c5
4 changed files with 50 additions and 58 deletions

View File

@ -374,7 +374,7 @@ suite "Serialization":
test "Load Tuple - missing field": test "Load Tuple - missing field":
let input = "str: value\nb: true" let input = "str: value\nb: true"
var result: MyTuple var result: MyTuple
expectConstructionError(2, 8, "While constructing MyTuple: Missing field: \"i\""): expectConstructionError(1, 1, "While constructing MyTuple: Missing field: \"i\""):
load(input, result) load(input, result)
test "Load Tuple - duplicate field": test "Load Tuple - duplicate field":
@ -421,7 +421,7 @@ suite "Serialization":
test "Load custom object - missing field": test "Load custom object - missing field":
let input = "surname: Pan\nage: 12\n " let input = "surname: Pan\nage: 12\n "
var result: Person var result: Person
expectConstructionError(3, 3, "While constructing Person: Missing field: \"firstnamechar\""): expectConstructionError(1, 1, "While constructing Person: Missing field: \"firstnamechar\""):
load(input, result) load(input, result)
test "Load custom object - duplicate field": test "Load custom object - duplicate field":
@ -477,27 +477,26 @@ suite "Serialization":
let input = @[Animal(name: "Bastet", kind: akCat, purringIntensity: 7), let input = @[Animal(name: "Bastet", kind: akCat, purringIntensity: 7),
Animal(name: "Anubis", kind: akDog, barkometer: 13)] Animal(name: "Anubis", kind: akDog, barkometer: 13)]
var output = dump(input, tsNone, asTidy, blockOnly) var output = dump(input, tsNone, asTidy, blockOnly)
assertStringEqual yamlDirs & """ assertStringEqual yamlDirs & "\n" &
"- \n" &
- " - \n" &
- " name: Bastet\n" &
name: Bastet " - \n" &
- " kind: akCat\n" &
kind: akCat " - \n" &
- " purringIntensity: 7\n" &
purringIntensity: 7 "- \n" &
- " - \n" &
- " name: Anubis\n" &
name: Anubis " - \n" &
- " kind: akDog\n" &
kind: akDog " - \n" &
- " barkometer: 13", output
barkometer: 13""", output
test "Load custom variant object - missing field": test "Load custom variant object - missing field":
let input = "[{name: Bastet}, {kind: akCat}]" let input = "[{name: Bastet}, {kind: akCat}]"
var result: Animal var result: Animal
expectConstructionError(1, 32, "While constructing Animal: Missing field: \"purringIntensity\""): expectConstructionError(1, 1, "While constructing Animal: Missing field: \"purringIntensity\""):
load(input, result) load(input, result)
test "Load non-variant object with transient fields": test "Load non-variant object with transient fields":
@ -512,7 +511,7 @@ suite "Serialization":
test "Load non-variant object with transient fields - unknown field": test "Load non-variant object with transient fields - unknown field":
let input = "{b: b, c: c, d: d}" let input = "{b: b, c: c, d: d}"
var result: NonVariantWithTransient var result: NonVariantWithTransient
expectConstructionError(1, 9, "While constructing NonVariantWithTransient: Field \"c\" is transient and may not occur in input"): expectConstructionError(1, 8, "While constructing NonVariantWithTransient: Field \"c\" is transient and may not occur in input"):
load(input, result) load(input, result)
test "Dump non-variant object with transient fields": test "Dump non-variant object with transient fields":
@ -534,7 +533,7 @@ suite "Serialization":
test "Load variant object with transient fields, error": test "Load variant object with transient fields, error":
let input = "[gStorable: gc, kind: deC, neverThere: foo]" let input = "[gStorable: gc, kind: deC, neverThere: foo]"
var result: VariantWithTransient var result: VariantWithTransient
expectConstructionError(1, 38, "While constructing VariantWithTransient: Field \"neverThere\" is transient and may not occur in input"): expectConstructionError(1, 28, "While constructing VariantWithTransient: Field \"neverThere\" is transient and may not occur in input"):
load(input, result) load(input, result)
test "Dump variant object with transient fields": test "Dump variant object with transient fields":
@ -543,20 +542,19 @@ suite "Serialization":
VariantWithTransient(kind: deC, gStorable: "a", gTemporary: "b", VariantWithTransient(kind: deC, gStorable: "a", gTemporary: "b",
neverThere: 42)] neverThere: 42)]
let output = dump(input, tsNone, asTidy, blockOnly) let output = dump(input, tsNone, asTidy, blockOnly)
assertStringEqual yamlDirs & """ assertStringEqual yamlDirs & "\n" &
"- \n" &
- " - \n" &
- " gStorable: gs\n" &
gStorable: gs " - \n" &
- " kind: deA\n" &
kind: deA " - \n" &
- " cStorable: cs\n" &
cStorable: cs "- \n" &
- " - \n" &
- " gStorable: a\n" &
gStorable: a " - \n" &
- " kind: deC", output
kind: deC""", output
test "Load object with ignored key": test "Load object with ignored key":
let input = "[{x: 1, y: 2}, {x: 3, z: 4, y: 5}, {z: [1, 2, 3], x: 4, y: 5}]" let input = "[{x: 1, y: 2}, {x: 3, z: 4, y: 5}, {z: [1, 2, 3], x: 4, y: 5}]"
@ -573,7 +571,7 @@ suite "Serialization":
test "Load object with ignored key - unknown field": test "Load object with ignored key - unknown field":
let input = "{x: 1, y: 2, zz: 3}" let input = "{x: 1, y: 2, zz: 3}"
var result: WithIgnoredField var result: WithIgnoredField
expectConstructionError(1, 16, "While constructing WithIgnoredField: Unknown field: \"zz\""): expectConstructionError(1, 14, "While constructing WithIgnoredField: Unknown field: \"zz\""):
load(input, result) load(input, result)
when not defined(JS): when not defined(JS):
@ -586,13 +584,13 @@ suite "Serialization":
b.next = c b.next = c
c.next = a c.next = a
var output = dump(a, tsRootOnly, asTidy, blockOnly) var output = dump(a, tsRootOnly, asTidy, blockOnly)
assertStringEqual yamlDirs & """!example.net:Node &a assertStringEqual yamlDirs & "!example.net:Node &a \n" &
value: a "value: a\n" &
next: "next: \n" &
value: b " value: b\n" &
next: " next: \n" &
value: c " value: c\n" &
next: *a""", output " next: *a", output
test "Load cyclic data structure": test "Load cyclic data structure":
let input = yamlDirs & """!n!system:seq(example.net:Node) let input = yamlDirs & """!n!system:seq(example.net:Node)
@ -651,7 +649,7 @@ next:
test "Custom representObject": test "Custom representObject":
let input = @[1.BetterInt, 9998887.BetterInt, 98312.BetterInt] let input = @[1.BetterInt, 9998887.BetterInt, 98312.BetterInt]
var output = dump(input, tsAll, asTidy, blockOnly) var output = dump(input, tsAll, asTidy, blockOnly)
assertStringEqual yamlDirs & """!n!system:seq(test:BetterInt) assertStringEqual yamlDirs & "!n!system:seq(test:BetterInt) \n" &
- !test:BetterInt 1 "- !test:BetterInt 1\n" &
- !test:BetterInt 9_998_887 "- !test:BetterInt 9_998_887\n" &
- !test:BetterInt 98_312""", output "- !test:BetterInt 98_312", output

View File

@ -348,6 +348,7 @@ proc beforeImplicitRoot(c: Context, e: var Event): bool =
if c.lex.cur != Token.Indentation: if c.lex.cur != Token.Indentation:
raise c.generateError("Unexpected token (expected line start): " & $c.lex.cur) raise c.generateError("Unexpected token (expected line start): " & $c.lex.cur)
c.inlineStart = c.lex.curEndPos c.inlineStart = c.lex.curEndPos
c.headerStart = c.lex.curEndPos
c.updateIndentation(c.lex.recentIndentation()) c.updateIndentation(c.lex.recentIndentation())
c.lex.next() c.lex.next()
case c.lex.cur case c.lex.cur

View File

@ -77,8 +77,6 @@ const
uriChars = {'a' .. 'z', 'A' .. 'Z', '0' .. '9', '#', ';', '/', '?', ':', uriChars = {'a' .. 'z', 'A' .. 'Z', '0' .. '9', '#', ';', '/', '?', ':',
'@', '&', '-', '=', '+', '$', '_', '.', '~', '*', '\'', '(', ')'} '@', '&', '-', '=', '+', '$', '_', '.', '~', '*', '\'', '(', ')'}
tagShorthandChars = {'a' .. 'z', 'A' .. 'Z', '0' .. '9', '-'} tagShorthandChars = {'a' .. 'z', 'A' .. 'Z', '0' .. '9', '-'}
suffixChars = {'a' .. 'z', 'A' .. 'Z', '0' .. '9', '#', ';', '/', '?', '@',
'&', '=', '+', '$', '_', '.', '!', '~', '*', '\'', '-'}
nodePropertyKind* = {Token.TagHandle, Token.VerbatimTag, Token.Anchor} nodePropertyKind* = {Token.TagHandle, Token.VerbatimTag, Token.Anchor}
scalarTokenKind* = {Token.Plain, Token.SingleQuoted, Token.DoubleQuoted, scalarTokenKind* = {Token.Plain, Token.SingleQuoted, Token.DoubleQuoted,
Token.Literal, Token.Folded} Token.Literal, Token.Folded}
@ -743,7 +741,8 @@ proc currentLine*(lex: Lexer): string {.locks: 0.} =
proc next*(lex: var Lexer) = proc next*(lex: var Lexer) =
while not lex.state(lex): discard while not lex.state(lex): discard
debug("lexer -> " & $lex.cur) debug("lexer -> [" & $lex.curStartPos.line & "," & $lex.curStartPos.column &
"-" & $lex.curEndPos.line & "," & $lex.curEndPos.column & "] " & $lex.cur)
proc init*(lex: var Lexer, source: Stream) {.raises: [IOError, OSError].} = proc init*(lex: var Lexer, source: Stream) {.raises: [IOError, OSError].} =
lex.source.open(source) lex.source.open(source)
@ -815,7 +814,6 @@ proc outsideDoc(lex: var Lexer): bool =
return true return true
proc yamlVersion(lex: var Lexer): bool = proc yamlVersion(lex: var Lexer): bool =
debug("lex: yamlVersion")
while lex.c in space: lex.advance() while lex.c in space: lex.advance()
lex.startToken() lex.startToken()
lex.readNumericSubtoken() lex.readNumericSubtoken()
@ -831,7 +829,6 @@ proc yamlVersion(lex: var Lexer): bool =
return true return true
proc tagShorthand(lex: var Lexer): bool = proc tagShorthand(lex: var Lexer): bool =
debug("lex: tagShorthand")
while lex.c in space: lex.advance() while lex.c in space: lex.advance()
if lex.c != '!': if lex.c != '!':
raise lex.generateError("Illegal character, tag shorthand must start with '!': " & escape("" & lex.c)) raise lex.generateError("Illegal character, tag shorthand must start with '!': " & escape("" & lex.c))
@ -855,7 +852,6 @@ proc tagShorthand(lex: var Lexer): bool =
return true return true
proc tagUri(lex: var Lexer): bool = proc tagUri(lex: var Lexer): bool =
debug("lex: tagUri")
while lex.c in space: lex.advance() while lex.c in space: lex.advance()
lex.startToken() lex.startToken()
if lex.c == '<': if lex.c == '<':
@ -867,7 +863,6 @@ proc tagUri(lex: var Lexer): bool =
return true return true
proc unknownDirParams(lex: var Lexer): bool = proc unknownDirParams(lex: var Lexer): bool =
debug("lex: unknownDirParams")
while lex.c in space: lex.advance() while lex.c in space: lex.advance()
if lex.c in lineEnd + {'#'}: if lex.c in lineEnd + {'#'}:
lex.state = expectLineEnd lex.state = expectLineEnd
@ -880,7 +875,6 @@ proc unknownDirParams(lex: var Lexer): bool =
return true return true
proc expectLineEnd(lex: var Lexer): bool = proc expectLineEnd(lex: var Lexer): bool =
debug("lex: expectLineEnd")
while lex.c in space: lex.advance() while lex.c in space: lex.advance()
if lex.c notin commentOrLineEnd: if lex.c notin commentOrLineEnd:
raise lex.generateError("Unexpected character (expected line end): " & escape("" & lex.c)) raise lex.generateError("Unexpected character (expected line end): " & escape("" & lex.c))
@ -888,7 +882,6 @@ proc expectLineEnd(lex: var Lexer): bool =
return false return false
proc lineStart(lex: var Lexer): bool = proc lineStart(lex: var Lexer): bool =
debug("lex: lineStart")
return case lex.startLine() return case lex.startLine()
of lsDirectivesEndMarker: lex.lineDirEnd() of lsDirectivesEndMarker: lex.lineDirEnd()
of lsDocumentEndMarker: lex.lineDocEnd() of lsDocumentEndMarker: lex.lineDocEnd()
@ -1129,7 +1122,7 @@ proc atSuffix(lex: var Lexer): bool =
var curStart = lex.tokenStart - 1 var curStart = lex.tokenStart - 1
while true: while true:
case lex.c case lex.c
of suffixChars: lex.advance() of uriChars: lex.advance()
of '%': of '%':
if curStart <= lex.source.bufpos - 2: if curStart <= lex.source.bufpos - 2:
lex.evaluated.add(lex.source.buf[curStart..lex.source.bufpos - 2]) lex.evaluated.add(lex.source.buf[curStart..lex.source.bufpos - 2])

View File

@ -909,7 +909,7 @@ proc constructObjectDefault*[O: object|tuple](
var i = 0 var i = 0
for fname, value in fieldPairs(result): for fname, value in fieldPairs(result):
if not matched[i]: if not matched[i]:
raise s.constructionError(e.startPos, "While constructing " & raise s.constructionError(startPos, "While constructing " &
typetraits.name(O) & ": Missing field: " & escape(fname)) typetraits.name(O) & ": Missing field: " & escape(fname))
inc(i) inc(i)
else: ensureAllFieldsPresent(s, O, result, matched, startPos) else: ensureAllFieldsPresent(s, O, result, matched, startPos)