move tests to testament (#16101)
* move tests to testament * minor * fix random * disable test random
This commit is contained in:
parent
d306a04466
commit
cbc793b30b
56 changed files with 2205 additions and 2232 deletions
|
|
@ -371,144 +371,3 @@ proc unsafeGet*[T](self: Option[T]): lent T {.inline.}=
|
|||
## Generally, using `get proc <#get,Option[T]>`_ is preferred.
|
||||
assert self.isSome
|
||||
result = self.val
|
||||
|
||||
when isMainModule:
|
||||
import unittest, sequtils
|
||||
|
||||
# RefPerson is used to test that overloaded `==` operator is not called by
|
||||
# options. It is defined here in the global scope, because otherwise the test
|
||||
# will not even consider the `==` operator. Different bug?
|
||||
type RefPerson = ref object
|
||||
name: string
|
||||
|
||||
proc `==`(a, b: RefPerson): bool =
|
||||
assert(not a.isNil and not b.isNil)
|
||||
a.name == b.name
|
||||
|
||||
suite "options":
|
||||
# work around a bug in unittest
|
||||
let intNone = none(int)
|
||||
let stringNone = none(string)
|
||||
|
||||
test "example":
|
||||
proc find(haystack: string, needle: char): Option[int] =
|
||||
for i, c in haystack:
|
||||
if c == needle:
|
||||
return some i
|
||||
|
||||
check("abc".find('c').get() == 2)
|
||||
|
||||
let result = "team".find('i')
|
||||
|
||||
check result == intNone
|
||||
check result.isNone
|
||||
|
||||
test "some":
|
||||
check some(6).get() == 6
|
||||
check some("a").unsafeGet() == "a"
|
||||
check some(6).isSome
|
||||
check some("a").isSome
|
||||
|
||||
test "none":
|
||||
expect UnpackDefect:
|
||||
discard none(int).get()
|
||||
check(none(int).isNone)
|
||||
check(not none(string).isSome)
|
||||
|
||||
test "equality":
|
||||
check some("a") == some("a")
|
||||
check some(7) != some(6)
|
||||
check some("a") != stringNone
|
||||
check intNone == intNone
|
||||
|
||||
when compiles(some("a") == some(5)):
|
||||
check false
|
||||
when compiles(none(string) == none(int)):
|
||||
check false
|
||||
|
||||
test "get with a default value":
|
||||
check(some("Correct").get("Wrong") == "Correct")
|
||||
check(stringNone.get("Correct") == "Correct")
|
||||
|
||||
test "$":
|
||||
check($(some("Correct")) == "Some(\"Correct\")")
|
||||
check($(stringNone) == "None[string]")
|
||||
|
||||
test "map with a void result":
|
||||
var procRan = 0
|
||||
some(123).map(proc (v: int) = procRan = v)
|
||||
check procRan == 123
|
||||
intNone.map(proc (v: int) = check false)
|
||||
|
||||
test "map":
|
||||
check(some(123).map(proc (v: int): int = v * 2) == some(246))
|
||||
check(intNone.map(proc (v: int): int = v * 2).isNone)
|
||||
|
||||
test "filter":
|
||||
check(some(123).filter(proc (v: int): bool = v == 123) == some(123))
|
||||
check(some(456).filter(proc (v: int): bool = v == 123).isNone)
|
||||
check(intNone.filter(proc (v: int): bool = check false).isNone)
|
||||
|
||||
test "flatMap":
|
||||
proc addOneIfNotZero(v: int): Option[int] =
|
||||
if v != 0:
|
||||
result = some(v + 1)
|
||||
else:
|
||||
result = none(int)
|
||||
|
||||
check(some(1).flatMap(addOneIfNotZero) == some(2))
|
||||
check(some(0).flatMap(addOneIfNotZero) == none(int))
|
||||
check(some(1).flatMap(addOneIfNotZero).flatMap(addOneIfNotZero) == some(3))
|
||||
|
||||
proc maybeToString(v: int): Option[string] =
|
||||
if v != 0:
|
||||
result = some($v)
|
||||
else:
|
||||
result = none(string)
|
||||
|
||||
check(some(1).flatMap(maybeToString) == some("1"))
|
||||
|
||||
proc maybeExclaim(v: string): Option[string] =
|
||||
if v != "":
|
||||
result = some v & "!"
|
||||
else:
|
||||
result = none(string)
|
||||
|
||||
check(some(1).flatMap(maybeToString).flatMap(maybeExclaim) == some("1!"))
|
||||
check(some(0).flatMap(maybeToString).flatMap(maybeExclaim) == none(string))
|
||||
|
||||
test "SomePointer":
|
||||
var intref: ref int
|
||||
check(option(intref).isNone)
|
||||
intref.new
|
||||
check(option(intref).isSome)
|
||||
|
||||
let tmp = option(intref)
|
||||
check(sizeof(tmp) == sizeof(ptr int))
|
||||
|
||||
var prc = proc (x: int): int = x + 1
|
||||
check(option(prc).isSome)
|
||||
prc = nil
|
||||
check(option(prc).isNone)
|
||||
|
||||
test "none[T]":
|
||||
check(none[int]().isNone)
|
||||
check(none(int) == none[int]())
|
||||
|
||||
test "$ on typed with .name":
|
||||
type Named = object
|
||||
name: string
|
||||
|
||||
let nobody = none(Named)
|
||||
check($nobody == "None[Named]")
|
||||
|
||||
test "$ on type with name()":
|
||||
type Person = object
|
||||
myname: string
|
||||
|
||||
let noperson = none(Person)
|
||||
check($noperson == "None[Person]")
|
||||
|
||||
test "Ref type with overloaded `==`":
|
||||
let p = some(RefPerson.new())
|
||||
check p.isSome
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue