nim-serde/tests/json/testPragmas.nim

51 lines
1.5 KiB
Nim
Raw Normal View History

2024-02-07 09:40:48 +11:00
import std/unittest
import pkg/serde
suite "json serialization pragmas":
test "fails to compile when object marked with 'serialize' specifies options":
2024-02-08 09:18:55 +11:00
type MyObj {.serialize(key = "test", ignore = true).} = object
2024-02-07 09:40:48 +11:00
check not compiles(%MyObj())
test "compiles when object marked with 'serialize' only":
2024-02-08 09:18:55 +11:00
type MyObj {.serialize.} = object
2024-02-07 09:40:48 +11:00
check compiles(%MyObj())
test "fails to compile when field marked with 'deserialize' specifies mode":
2024-02-08 09:18:55 +11:00
type MyObj = object
field {.deserialize(mode = OptIn).}: bool
2024-02-07 09:40:48 +11:00
check not compiles(MyObj.fromJson("""{"field":true}"""))
test "compiles when object marked with 'deserialize' specifies mode":
2024-02-08 09:18:55 +11:00
type MyObj {.deserialize(mode = OptIn).} = object
field: bool
2024-02-07 09:40:48 +11:00
check compiles(MyObj.fromJson("""{"field":true}"""))
test "fails to compile when object marked with 'deserialize' specifies key":
2024-02-08 09:18:55 +11:00
type MyObj {.deserialize("test").} = object
field: bool
2024-02-07 09:40:48 +11:00
check not compiles(MyObj.fromJson("""{"field":true}"""))
test "compiles when field marked with 'deserialize' specifies key":
2024-02-08 09:18:55 +11:00
type MyObj = object
field {.deserialize("test").}: bool
2024-02-07 09:40:48 +11:00
check compiles(MyObj.fromJson("""{"field":true}"""))
test "compiles when field marked with empty 'deserialize'":
2024-02-08 09:18:55 +11:00
type MyObj = object
field {.deserialize.}: bool
2024-02-07 09:40:48 +11:00
check compiles(MyObj.fromJson("""{"field":true}"""))
test "compiles when field marked with 'serialize'":
2024-02-08 09:18:55 +11:00
type MyObj = object
field {.serialize.}: bool
2024-02-07 09:40:48 +11:00
2024-02-08 09:18:55 +11:00
check compiles(%MyObj())