mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-29 03:37:55 +00:00
Improve the marshal module (#16777)
* Improve marshal
Use runnableExamples
Refactor tests
* Readd {.inheritable.} test
Apply suggestions
This commit is contained in:
@@ -1,32 +1,15 @@
|
||||
discard """
|
||||
output: '''{"age": 12, "bio": "Я Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"}
|
||||
true
|
||||
true
|
||||
alpha 100
|
||||
omega 200
|
||||
Some(null)
|
||||
None[JsonNode]
|
||||
(numeric: "")
|
||||
hello world
|
||||
'''
|
||||
joinable: false
|
||||
"""
|
||||
import std/marshal
|
||||
|
||||
#[
|
||||
joinable: false pending https://github.com/nim-lang/Nim/issues/9754
|
||||
]#
|
||||
# TODO: add static tests
|
||||
|
||||
import marshal
|
||||
proc testit[T](x: T): string = $$to[T]($$x)
|
||||
|
||||
template testit(x) = discard $$to[typeof(x)]($$x)
|
||||
|
||||
var x: array[0..4, array[0..4, string]] = [
|
||||
["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
|
||||
["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
|
||||
["test", "1", "2", "3", "4"]]
|
||||
testit(x)
|
||||
var test2: tuple[name: string, s: int] = ("tuple test", 56)
|
||||
testit(test2)
|
||||
let test1: array[0..1, array[0..4, string]] = [
|
||||
["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"]]
|
||||
doAssert testit(test1) ==
|
||||
"""[["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"]]"""
|
||||
let test2: tuple[name: string, s: int] = ("tuple test", 56)
|
||||
doAssert testit(test2) == """{"Field0": "tuple test", "Field1": 56}"""
|
||||
|
||||
type
|
||||
TE = enum
|
||||
@@ -57,87 +40,86 @@ proc buildList(): PNode =
|
||||
result.prev.next = result
|
||||
result.prev.prev = result.next
|
||||
|
||||
var test3: TestObj
|
||||
test3.test = 42
|
||||
test3.test2 = blah
|
||||
testit(test3)
|
||||
let test3 = TestObj(test: 42, test2: blah)
|
||||
doAssert testit(test3) ==
|
||||
"""{"test": 42, "asd": 0, "test2": "blah", "help": ""}"""
|
||||
|
||||
var test4: ref tuple[a, b: string]
|
||||
new(test4)
|
||||
test4.a = "ref string test: A"
|
||||
test4.b = "ref string test: B"
|
||||
testit(test4)
|
||||
discard testit(test4) # serialization uses the pointer address, which is not consistent
|
||||
|
||||
var test5 = @[(0,1),(2,3),(4,5)]
|
||||
testit(test5)
|
||||
let test5 = @[(0,1),(2,3),(4,5)]
|
||||
doAssert testit(test5) ==
|
||||
"""[{"Field0": 0, "Field1": 1}, {"Field0": 2, "Field1": 3}, {"Field0": 4, "Field1": 5}]"""
|
||||
|
||||
var test7 = buildList()
|
||||
testit(test7)
|
||||
let test6: set[char] = {'A'..'Z', '_'}
|
||||
doAssert testit(test6) ==
|
||||
"""[65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 95]"""
|
||||
|
||||
var test6: set[char] = {'A'..'Z', '_'}
|
||||
testit(test6)
|
||||
let test7 = buildList()
|
||||
discard testit(test7) # serialization uses the pointer address, which is not consistent
|
||||
|
||||
|
||||
# bug #1352
|
||||
block:
|
||||
type
|
||||
Entity = object of RootObj
|
||||
name: string
|
||||
|
||||
type
|
||||
Entity = object of RootObj
|
||||
name: string
|
||||
Person = object of Entity
|
||||
age: int
|
||||
bio: string
|
||||
blob: string
|
||||
|
||||
Person = object of Entity
|
||||
age: int
|
||||
bio: string
|
||||
blob: string
|
||||
let instance1 = Person(name: "Cletus", age: 12,
|
||||
bio: "Я Cletus",
|
||||
blob: "ABC\x80")
|
||||
doAssert $$instance1 == """{"age": 12, "bio": "Я Cletus", "blob": [65, 66, 67, 128], "name": "Cletus"}"""
|
||||
doAssert to[Person]($$instance1).bio == instance1.bio
|
||||
doAssert to[Person]($$instance1).blob == instance1.blob
|
||||
|
||||
var instance1 = Person(name: "Cletus", age: 12,
|
||||
bio: "Я Cletus",
|
||||
blob: "ABC\x80")
|
||||
echo($$instance1)
|
||||
echo(to[Person]($$instance1).bio == instance1.bio) # true
|
||||
echo(to[Person]($$instance1).blob == instance1.blob) # true
|
||||
# bug #5757
|
||||
block:
|
||||
type
|
||||
Something = object
|
||||
x: string
|
||||
y: int
|
||||
|
||||
# bug 5757
|
||||
let data1 = """{"x": "alpha", "y": 100}"""
|
||||
let data2 = """{"x": "omega", "y": 200}"""
|
||||
|
||||
type
|
||||
Something = object
|
||||
x: string
|
||||
y: int
|
||||
var r = to[Something](data1)
|
||||
doAssert $r.x & " " & $r.y == "alpha 100"
|
||||
r = to[Something](data2)
|
||||
doAssert $r.x & " " & $r.y == "omega 200"
|
||||
|
||||
var data1 = """{"x": "alpha", "y": 100}"""
|
||||
var data2 = """{"x": "omega", "y": 200}"""
|
||||
block:
|
||||
type
|
||||
Foo = object
|
||||
a1: string
|
||||
a2: string
|
||||
a3: seq[string]
|
||||
a4: seq[int]
|
||||
a5: seq[int]
|
||||
a6: seq[int]
|
||||
var foo = Foo(a2: "", a4: @[], a6: @[1])
|
||||
foo.a6.setLen 0
|
||||
doAssert $$foo == """{"a1": "", "a2": "", "a3": [], "a4": [], "a5": [], "a6": []}"""
|
||||
doAssert testit(foo) == """{"a1": "", "a2": "", "a3": [], "a4": [], "a5": [], "a6": []}"""
|
||||
|
||||
var r = to[Something](data1)
|
||||
|
||||
echo r.x, " ", r.y
|
||||
|
||||
r = to[Something](data2)
|
||||
|
||||
echo r.x, " ", r.y
|
||||
|
||||
|
||||
type
|
||||
Foo = object
|
||||
a1: string
|
||||
a2: string
|
||||
a3: seq[string]
|
||||
a4: seq[int]
|
||||
a5: seq[int]
|
||||
a6: seq[int]
|
||||
var foo = Foo(a2: "", a4: @[], a6: @[1])
|
||||
foo.a6.setLen 0
|
||||
doAssert $$foo == """{"a1": "", "a2": "", "a3": [], "a4": [], "a5": [], "a6": []}"""
|
||||
testit(foo)
|
||||
|
||||
import options, json
|
||||
import std/[options, json]
|
||||
|
||||
# bug #15934
|
||||
block:
|
||||
let
|
||||
a1 = some(newJNull())
|
||||
a2 = none(JsonNode)
|
||||
echo ($$a1).to[:Option[JsonNode]]
|
||||
echo ($$a2).to[:Option[JsonNode]]
|
||||
|
||||
doAssert $($$a1).to[:Option[JsonNode]] == "Some(null)"
|
||||
doAssert $($$a2).to[:Option[JsonNode]] == "None[JsonNode]"
|
||||
doAssert ($$a1).to[:Option[JsonNode]] == some(newJNull())
|
||||
doAssert ($$a2).to[:Option[JsonNode]] == none(JsonNode)
|
||||
|
||||
# bug #15620
|
||||
block:
|
||||
@@ -148,10 +130,19 @@ block:
|
||||
numeric: string
|
||||
|
||||
let test = to[LegacyEntry](str)
|
||||
echo test
|
||||
doAssert $test == """(numeric: "")"""
|
||||
|
||||
# bug #16022
|
||||
block:
|
||||
let p: proc () = proc () = echo "hello world"
|
||||
let poc = (to[typeof(p)]($$p))
|
||||
poc()
|
||||
let p: proc (): string = proc (): string = "hello world"
|
||||
let poc = to[typeof(p)]($$p)
|
||||
doAssert poc() == "hello world"
|
||||
|
||||
block:
|
||||
type
|
||||
A {.inheritable.} = object
|
||||
B = object of A
|
||||
f: int
|
||||
|
||||
let a: ref A = new(B)
|
||||
doAssert $$a[] == "{}" # not "{f: 0}"
|
||||
|
||||
Reference in New Issue
Block a user