/
nv-lang
/
nova
Обзор
Документация
Войти
/
nv-lang
/
nova
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
std/src/encoding/serde/sum_autoderive_test.nv
77 строк
3 KB
Evgeniy Golovin
195: std/ -> std/src/ (git mv, module-path без изменений)
13 июл 2026, 01:13
13 июл 2026, 01:13
be4fcab
Код
Авторство
О чём код?
// Plan 180 Ф.2-sum (D345) — sum auto-derive via #impl(Serialize + Deserialize). // Externally-tagged (Q4, default): unit → bare string "V"; single-payload → // {"V": x}; tuple → {"V": [a, b]}; record → {"V": {fields}}. Peer of // tagging_test.nv (internally/adjacently-tagged); this file is the sole // coverage of the externally-tagged DEFAULT mode (no `#serde(...)` attribute). // No hand-written @serialize/.deserialize — the compiler synthesizes them. // Migrated from nova_tests/serde/sum_autoderive.nv (Plan 182 Ф.2). module encoding.serde #impl(Serialize + Deserialize + Equal) type Msg enum | Ping | Echo(str) | Pair(int, int) | Labelled { id int, label str } | Maybe(Option[int]) | Strs([]str) test "unit variant → bare string wire, round-trip" { ro m Msg = Ping ro enc = json_encode(m)!! assert(enc == "\"Ping\"") assert(json_decode[Msg](enc)!! == Ping) } test "single-payload variant → object wire, round-trip" { ro m Msg = Echo("hi") ro enc = json_encode(m)!! assert(enc == "{\"Echo\":\"hi\"}") assert(json_decode[Msg](enc)!! == Echo("hi")) } test "tuple variant → array-payload wire, round-trip" { ro m Msg = Pair(3, 4) ro dec = json_decode[Msg](json_encode(m)!!)!! assert(dec == Pair(3, 4)) assert(dec != Pair(4, 3)) } test "record variant → object-payload wire, round-trip" { ro m Msg = Labelled { id: 7, label: "z" } ro dec = json_decode[Msg](json_encode(m)!!)!! assert(dec == Labelled { id: 7, label: "z" }) } test "variant with Option payload — Some and None" { ro a Msg = Maybe(Some(5)) assert(json_decode[Msg](json_encode(a)!!)!! == Maybe(Some(5))) ro b Msg = Maybe(None) assert(json_decode[Msg](json_encode(b)!!)!! == Maybe(None)) } test "variant with Vec payload" { ro m Msg = Strs(["a", "b", "c"]) ro dec = json_decode[Msg](json_encode(m)!!)!! assert(dec == Strs(["a", "b", "c"])) } test "unknown variant tag → Err(UnknownVariant), not panic" { match json_decode[Msg]("{\"Nope\": 1}") { Ok(_) => assert(false, "expected UnknownVariant") Err(e) => match e.kind { UnknownVariant { name, expected } => assert(name == "Nope") _ => assert(false, "wrong kind") } } } test "unknown unit tag (bare string) → Err(UnknownVariant)" { match json_decode[Msg]("\"Bogus\"") { Ok(_) => assert(false) Err(e) => match e.kind { UnknownVariant { name, expected } => assert(name == "Bogus") _ => assert(false) } } }