2022-09-27 12:55:58 +00:00
|
|
|
import std/unittest
|
2024-03-21 10:53:45 +00:00
|
|
|
import codex/utils/options
|
2023-06-22 18:01:21 +00:00
|
|
|
import ../helpers
|
2022-09-27 12:55:58 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
checksuite "optional casts":
|
2022-09-27 12:55:58 +00:00
|
|
|
test "casting value to same type works":
|
|
|
|
check 42 as int == some 42
|
|
|
|
|
|
|
|
test "casting value to unrelated type evaluates to None":
|
|
|
|
check 42 as string == string.none
|
|
|
|
|
|
|
|
test "casting value to subtype works":
|
|
|
|
type
|
|
|
|
BaseType = ref object of RootObj
|
|
|
|
SubType = ref object of BaseType
|
|
|
|
let x: BaseType = SubType()
|
|
|
|
check x as SubType == SubType(x).some
|
|
|
|
|
|
|
|
test "casting to unrelated subtype evaluates to None":
|
|
|
|
type
|
|
|
|
BaseType = ref object of RootObj
|
|
|
|
SubType = ref object of BaseType
|
|
|
|
OtherType = ref object of BaseType
|
|
|
|
let x: BaseType = SubType()
|
|
|
|
check x as OtherType == OtherType.none
|
|
|
|
|
|
|
|
test "casting works on optional types":
|
|
|
|
check 42.some as int == some 42
|
|
|
|
check 42.some as string == string.none
|
|
|
|
check int.none as int == int.none
|
2024-03-21 10:53:45 +00:00
|
|
|
|
|
|
|
checksuite "Optionalize":
|
|
|
|
test "does not except non-object types":
|
|
|
|
static:
|
|
|
|
doAssert not compiles(Optionalize(int))
|
|
|
|
|
|
|
|
test "converts object fields to option":
|
|
|
|
type BaseType = object
|
|
|
|
a: int
|
|
|
|
b: bool
|
|
|
|
c: string
|
|
|
|
d: Option[string]
|
|
|
|
|
|
|
|
type OptionalizedType = Optionalize(BaseType)
|
|
|
|
|
|
|
|
check OptionalizedType.a is Option[int]
|
|
|
|
check OptionalizedType.b is Option[bool]
|
|
|
|
check OptionalizedType.c is Option[string]
|
|
|
|
check OptionalizedType.d is Option[string]
|