add tests for Time and TimeDate

This commit is contained in:
munna0908 2025-05-30 15:53:57 +05:30
parent a96971968d
commit ff37e61b54
No known key found for this signature in database
GPG Key ID: 2FFCD637E937D3E6
2 changed files with 14 additions and 7 deletions

View File

@ -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 =

View File

@ -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