tannotations.nim
1 # NimYAML - YAML implementation in Nim 2 # (c) Copyright 2015-2023 Felix Krause 3 # 4 # See the file "copying.txt", included in this 5 # distribution, for details about the copyright. 6 7 import "../yaml" 8 import unittest 9 10 type 11 Config = ref object 12 docsRoot* {.defaultVal: "~/.example".}: string 13 draftsRoot*: string 14 15 Stuff = ref object 16 a {.transient.}: string 17 b: string 18 19 Part {.ignore: ["a", "b"].} = ref object 20 c: string 21 22 IgnoreAnything {.ignore: [].} = ref object 23 warbl: int 24 25 ContainerKind = enum 26 ckString, ckInt 27 28 Container {.implicit.} = object 29 case kind: ContainerKind 30 of ckString: 31 strVal: string 32 of ckInt: 33 intVal: int 34 35 Sparse {.sparse.} = ref object of RootObj 36 name*: Option[string] 37 description*: Option[string] 38 39 suite "Serialization Annotations": 40 test "load default value": 41 let input = "draftsRoot: foo" 42 var result: Config 43 load(input, result) 44 assert result.docsRoot == "~/.example", "docsRoot is " & result.docsRoot 45 assert result.draftsRoot == "foo", "draftsRoot is " & result.draftsRoot 46 47 test "load into object with transient fields": 48 let input = "b: warbl" 49 var result: Stuff 50 load(input, result) 51 assert result.b == "warbl" 52 assert result.a == "" 53 54 test "load into object with ignored keys": 55 let input = "{a: foo, c: bar, b: baz}" 56 var result: Part 57 load(input, result) 58 assert result.c == "bar" 59 60 test "load into object ignoring all other keys": 61 let input = "{tuirae: fg, rtuco: fgh, warbl: 1}" 62 var result: IgnoreAnything 63 load(input, result) 64 assert result.warbl == 1 65 66 test "load implicit variant object": 67 let input = "[foo, 13]" 68 var result: seq[Container] 69 load(input, result) 70 assert len(result) == 2 71 assert result[0].kind == ckString 72 assert result[1].kind == ckInt 73 74 test "load sparse type": 75 let input = "{}" 76 var result: Sparse 77 load(input, result) 78 assert result.name.isNone 79 assert result.description.isNone