import std/[times, typetraits, random, strutils, options, sets, tables], unittest2, faststreams/inputs, ../../serialization, ../object_serialization type Meter* = distinct int Mile* = distinct int Simple* = object x*: int y*: string distance*: Meter ignored*: int Transaction* = object amount*: int time*: DateTime sender*: string receiver*: string BaseType* = object of RootObj a*: string b*: int BaseTypeRef* = ref BaseType DerivedType* = object of BaseType c*: int d*: string DerivedRefType* = ref object of BaseType c*: int d*: string DerivedFromRefType* = ref object of DerivedRefType e*: int RefTypeDerivedFromRoot* = ref object of RootObj a*: int b*: string Foo = object x*: uint64 y*: string z*: seq[int] Bar = object b*: string f*: Foo # Baz should use custom serialization # The `i` field should be multiplied by two while deserializing and # `ignored` field should be set to 10 Baz = object f*: Foo i*: int ignored* {.dontSerialize.}: int ListOfLists = object lists*: seq[ListOfLists] NoExpectedResult = distinct int ObjectKind* = enum A B CaseObject* = object case kind*: ObjectKind of A: a*: int other*: CaseObjectRef else: b*: int CaseObjectRef* = ref CaseObject HoldsCaseObject* = object value: CaseObject HoldsSet* = object a*: int s*: HashSet[string] HoldsOption* = object r*: ref Simple o*: Option[Simple] HoldsArray* = object data*: seq[int] AnonTuple* = (int, string, float64) AbcTuple* = tuple[a: int, b: string, c: float64] XyzTuple* = tuple[x: int, y: string, z: float64] HoldsTuples* = object t1*: AnonTuple t2*: AbcTuple t3*: XyzTuple static: assert isCaseObject(CaseObject) assert isCaseObject(CaseObjectRef) assert(not isCaseObject(Transaction)) assert(not isCaseObject(HoldsSet)) Meter.borrowSerialization int Simple.setSerializedFields distance, x, y proc default(T: typedesc): T = discard func caseObjectEquals(a, b: CaseObject): bool {.raises: [].} func `==`*(a, b: CaseObjectRef): bool {.raises: [].} = let nils = ord(a.isNil) + ord(b.isNil) if nils == 0: caseObjectEquals(a[], b[]) else: nils == 2 func caseObjectEquals(a, b: CaseObject): bool {.raises: [].} = # TODO This is needed to work-around a Nim overload selection issue if a.kind != b.kind: return false case a.kind of A: if a.a != b.a: return false a.other == b.other of B: a.b == b.b func `==`*(a, b: CaseObject): bool = caseObjectEquals(a, b) func `==`*(a, b: ListOfLists): bool = if a.lists.len != b.lists.len: return false for i in 0..