json: assert encoded struct with a sumtype (#11970)

pull/11979/head
Anton Zavodchikov 2021-09-25 22:29:52 +05:00 committed by GitHub
parent 970768288d
commit 80c15607da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 2 deletions

View File

@ -59,6 +59,7 @@ struct SomeGame {
} }
fn test_encode_decode_sumtype() ? { fn test_encode_decode_sumtype() ? {
t := time.now()
game := SomeGame{ game := SomeGame{
title: 'Super Mega Game' title: 'Super Mega Game'
player: Human{'Monke'} player: Human{'Monke'}
@ -67,7 +68,7 @@ fn test_encode_decode_sumtype() ? {
Item{'Cookie'}, Item{'Cookie'},
Animal.cat, Animal.cat,
'Stool', 'Stool',
time.now(), t,
] ]
} }
eprintln('Game: $game') eprintln('Game: $game')
@ -75,8 +76,9 @@ fn test_encode_decode_sumtype() ? {
enc := json.encode(game) enc := json.encode(game)
eprintln('Encoded Game: $enc') eprintln('Encoded Game: $enc')
dec := json.decode(SomeGame, enc) ? assert enc == '{"title":"Super Mega Game","player":{"name":"Monke","_type":"Human"},"other":[{"tag":"Pen","_type":"Item"},{"tag":"Cookie","_type":"Item"},1,"Stool",{"_type":"Time","value":$t.unix_time()}]}'
dec := json.decode(SomeGame, enc) ?
eprintln('Decoded Game: $dec') eprintln('Decoded Game: $dec')
assert game.title == dec.title assert game.title == dec.title