[soft] take out yTagNimNilString/Seq and related (?) nil tests

This commit is contained in:
Vindaar 2018-10-03 19:49:05 +02:00 committed by flyx
parent 7ad13d98d9
commit 4821ef95fb
3 changed files with 74 additions and 70 deletions

View File

@ -205,16 +205,16 @@ suite "Serialization":
assert result[3] == NegInf
assert classify(result[4]) == fcNan
test "Load nil string":
let input = "!<tag:nimyaml.org,2016:nil:string> \"\""
var result: string
load(input, result)
assert isNil(result)
test "Dump nil string":
let input: string = nil
var output = dump(input, tsNone, asTidy, blockOnly)
assertStringEqual yamlDirs & "\n!n!nil:string \"\"", output
#test "Load nil string":
# let input = "!<tag:nimyaml.org,2016:nil:string> \"\""
# var result: string
# load(input, result)
# assert result.len == 0
#
#test "Dump nil string":
# let input: string = ""
# var output = dump(input, tsNone, asTidy, blockOnly)
# assertStringEqual yamlDirs & "\n!n!nil:string \"\"", output
test "Load timestamps":
let input = "[2001-12-15T02:59:43.1Z, 2001-12-14t21:59:43.10-05:00, 2001-12-14 21:59:43.10-5]"
@ -237,16 +237,16 @@ suite "Serialization":
var output = dump(input, tsNone, asTidy, blockOnly)
assertStringEqual yamlDirs & "\n- a\n- b", output
test "Load nil seq":
let input = "!<tag:nimyaml.org,2016:nil:seq> \"\""
var result: seq[int]
load(input, result)
assert isNil(result)
test "Dump nil seq":
let input: seq[int] = nil
var output = dump(input, tsNone, asTidy, blockOnly)
assertStringEqual yamlDirs & "\n!n!nil:seq \"\"", output
#test "Load nil seq":
# let input = "!<tag:nimyaml.org,2016:nil:seq> \"\""
# var result: seq[int]
# load(input, result)
# assert result.len == 0
#
#test "Dump nil seq":
# let input: seq[int] = @[]
# var output = dump(input, tsNone, asTidy, blockOnly)
# assertStringEqual yamlDirs & "\n!n!nil:seq \"\"", output
test "Load char set":
let input = "- a\n- b"
@ -641,30 +641,30 @@ next:
assert result.c == "cde"
assert result.d == "d"
when not defined(JS):
test "Load nil values":
let input = "- ~\n- !!str ~"
var result: seq[ref string]
try: load(input, result)
except YamlConstructionError:
let ex = (ref YamlConstructionError)(getCurrentException())
echo "line ", ex.line, ", column ", ex.column, ": ", ex.msg
echo ex.lineContent
raise ex
assert(result.len == 2)
assert(result[0] == nil)
assert(result[1][] == "~")
test "Dump nil values":
var input = newSeq[ref string]()
input.add(nil)
input.add(new string)
input[1][] = "~"
var output = dump(input, tsRootOnly, asTidy, blockOnly)
assertStringEqual(yamlDirs &
"!n!system:seq(tag:yaml.org;2002:str) \n- !!null ~\n- !!str ~",
output)
#when not defined(JS):
# test "Load nil values":
# let input = "- ~\n- !!str ~"
# var result: seq[ref string]
# try: load(input, result)
# except YamlConstructionError:
# let ex = (ref YamlConstructionError)(getCurrentException())
# echo "line ", ex.line, ", column ", ex.column, ": ", ex.msg
# echo ex.lineContent
# raise ex
#
# assert(result.len == 2)
# assert(result[0] == nil)
# assert(result[1][] == "~")
#
# test "Dump nil values":
# var input = newSeq[ref string]()
# input.add(nil)
# input.add(new string)
# input[1][] = "~"
# var output = dump(input, tsRootOnly, asTidy, blockOnly)
# assertStringEqual(yamlDirs &
# "!n!system:seq(tag:yaml.org;2002:str) \n- !!null ~\n- !!str ~",
# output)
test "Custom constructObject":
let input = "- 1\n- !test:BetterInt 2"

View File

@ -1148,11 +1148,11 @@ proc constructChild*(s: var YamlStream, c: ConstructionContext,
result: var string) =
let item = s.peek()
if item.kind == yamlScalar:
if item.scalarTag == yTagNimNilString:
discard s.next()
result = ""
return
elif item.scalarTag notin
#if item.scalarTag == yTagNimNilString:
# discard s.next()
# result = ""
# return
if item.scalarTag notin
[yTagQuestionMark, yTagExclamationMark, yamlTag(string)]:
raise s.constructionError("Wrong tag for string")
elif item.scalarAnchor != yAnchorNone:
@ -1163,10 +1163,11 @@ proc constructChild*[T](s: var YamlStream, c: ConstructionContext,
result: var seq[T]) =
let item = s.peek()
if item.kind == yamlScalar:
if item.scalarTag == yTagNimNilSeq:
discard s.next()
result = @[]
return
#if item.scalarTag == yTagNimNilSeq:
# TODO: this whole branch still makes sense
discard s.next()
result = @[]
return
elif item.kind == yamlStartSeq:
if item.seqTag notin [yTagQuestionMark, yamlTag(seq[T])]:
raise s.constructionError("Wrong tag for " & typetraits.name(seq[T]))
@ -1231,16 +1232,19 @@ proc constructChild*[O](s: var YamlStream, c: ConstructionContext,
raise e
proc representChild*(value: string, ts: TagStyle, c: SerializationContext) =
if value.len == 0: c.put(scalarEvent("", yTagNimNilString))
else:
let tag = presentTag(string, ts)
representObject(value, ts, c,
if tag == yTagQuestionMark and guessType(value) != yTypeUnknown:
yTagExclamationMark else: tag)
#if value.len == 0: c.put(scalarEvent("", yTagNimNilString))
#else:
let tag = presentTag(string, ts)
representObject(value, ts, c,
if tag == yTagQuestionMark and guessType(value) != yTypeUnknown:
yTagExclamationMark
else:
tag)
proc representChild*[T](value: seq[T], ts: TagStyle, c: SerializationContext) =
if value.len == 0: c.put(scalarEvent("", yTagNimNilSeq))
else: representObject(value, ts, c, presentTag(seq[T], ts))
#if value.len == 0: c.put(scalarEvent("", yTagNimNilSeq))
#else:
representObject(value, ts, c, presentTag(seq[T], ts))
proc representChild*[O](value: ref O, ts: TagStyle, c: SerializationContext) =
if isNil(value): c.put(scalarEvent("~", yTagNull))

View File

@ -89,10 +89,10 @@ const
## This tag is used in serialization for the name of a field of an
## object. It may contain any string scalar that is a valid Nim symbol.
yTagNimNilString* : TagId = 101.TagId ## for strings that are nil
yTagNimNilSeq* : TagId = 102.TagId ## \
## for seqs that are nil. This tag is used regardless of the seq's generic
## type parameter.
#yTagNimNilString* : TagId = 101.TagId ## for strings that are nil
#yTagNimNilSeq* : TagId = 102.TagId ## \
## for seqs that are nil. This tag is used regardless of the seq's generic
## type parameter.
yFirstStaticTagId* : TagId = 1000.TagId ## \
## The first ``TagId`` assigned by the ``setTagId`` templates.
@ -211,8 +211,8 @@ proc initSerializationTagLibrary*(): TagLibrary =
result.tags[y"value"] = yTagValue
result.tags[y"binary"] = yTagBinary
result.tags[n"field"] = yTagNimField
result.tags[n"nil:string"] = yTagNimNilString
result.tags[n"nil:seq"] = yTagNimNilSeq
#result.tags[n"nil:string"] = yTagNimNilString
#result.tags[n"nil:seq"] = yTagNimNilSeq
var
serializationTagLibrary* = initSerializationTagLibrary() ## \
@ -275,8 +275,8 @@ static:
registeredUris.add(y"binary")
# special tags used by serialization
registeredUris.add(n"field")
registeredUris.add(n"nil:string")
registeredUris.add(n"nil:seq")
#registeredUris.add(n"nil:string")
#registeredUris.add(n"nil:seq")
# tags for Nim's standard types
setTagUri(char, n"system:char", yTagNimChar)
@ -318,4 +318,4 @@ iterator handles*(tagLib: TagLibrary): tuple[prefix, handle: string] =
proc nimTag*(suffix: string): string =
## prepends NimYAML's tag repository prefix to the given suffix. For example,
## ``nimTag("system:char")`` yields ``"tag:nimyaml.org,2016:system:char"``.
nimyamlTagRepositoryPrefix & suffix
nimyamlTagRepositoryPrefix & suffix