From ff37e61b544d60a0987806826f4ee7e7f0f73584 Mon Sep 17 00:00:00 2001 From: munna0908 Date: Fri, 30 May 2025 15:53:57 +0530 Subject: [PATCH] add tests for Time and TimeDate --- serde/cbor/deserializer.nim | 14 ++++++++------ tests/cbor/testObjects.nim | 7 ++++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/serde/cbor/deserializer.nim b/serde/cbor/deserializer.nim index eacf5be..8721c03 100644 --- a/serde/cbor/deserializer.nim +++ b/serde/cbor/deserializer.nim @@ -494,30 +494,32 @@ proc parseTime(n: CborNode): Time = else: assert false -proc fromCborHook*(v: var DateTime; n: CborNode): ?!void = +proc fromCbor*(_: type DateTime; n: CborNode): ?!DateTime = ## Parse a `DateTime` from the tagged string representation ## defined in RCF7049 section 2.4.1. + var v: DateTime if n.tag.isSome: try: if n.tag.get == 0 and n.kind == cborText: v = parseDateText(n) - return success() + return success(v) elif n.tag.get == 1 and n.kind in {cborUnsigned, cborNegative, cborFloat}: v = parseTime(n).utc - return success() + return success(v) except ValueError as e: return failure(e) -proc fromCborHook*(v: var Time; n: CborNode): ?!void = +proc fromCbor*(_: type Time; n: CborNode): ?!Time = ## Parse a `Time` from the tagged string representation ## defined in RCF7049 section 2.4.1. + var v: Time if n.tag.isSome: try: if n.tag.get == 0 and n.kind == cborText: v = parseDateText(n).toTime - return success() + return success(v) elif n.tag.get == 1 and n.kind in {cborUnsigned, cborNegative, cborFloat}: v = parseTime(n) - return success() + return success(v) except ValueError as e: return failure(e) func isTagged*(n: CborNode): bool = diff --git a/tests/cbor/testObjects.nim b/tests/cbor/testObjects.nim index ab22498..04485fa 100644 --- a/tests/cbor/testObjects.nim +++ b/tests/cbor/testObjects.nim @@ -1,6 +1,7 @@ import std/unittest import std/options import std/streams +import std/times import std/macros import pkg/serde import pkg/questionable @@ -59,6 +60,8 @@ type refNewInner: NewType # Custom reference type refNil: ref Inner # Nil reference customPoint: CustomPoint # Custom type + time: Time # Time + date: DateTime # DateTime # Custom deserialization for CustomColor enum # Converts a CBOR negative integer to a CustomColor enum value @@ -169,7 +172,9 @@ suite "CBOR deserialization": refInner: refInner, # reference to object refNewInner: refNewObj, # custom reference type refNil: nil, # nil reference - customPoint: CustomPoint(x: 15, y: 25) # custom type + customPoint: CustomPoint(x: 15, y: 25), # custom type + time: getTime(), # time + date: now().utc # date ) # Test serialization using encode helper