nim-contract-abi/tests/contractabi/testCustomTypes.nim
2021-12-02 11:15:02 +01:00

32 lines
947 B
Nim

import std/unittest
import pkg/questionable/results
import contractabi
type CustomType = object
a: uint16
b: string
func encode(encoder: var AbiEncoder, custom: CustomType) =
encoder.write( (custom.a, custom.b) )
func decode(decoder: var AbiDecoder, T: type CustomType): ?!T =
let (a, b) = ?decoder.read( (uint16, string) )
success CustomType(a: a, b: b)
suite "custom types":
let custom = CustomType(a: 42, b: "ultimate answer")
test "can be encoded":
check AbiEncoder.encode(custom) == AbiEncoder.encode( (custom.a, custom.b) )
test "can be decoded":
let encoding = AbiEncoder.encode(custom)
check AbiDecoder.decode(encoding, CustomType) == success custom
test "can be embedded in tuples, arrays and sequences":
let embedding = (custom, [custom], @[custom])
let encoding = AbiEncoder.encode(embedding)
let decoded = AbiDecoder.decode(encoding, typeof(embedding))
check !decoded == embedding