Simplify optionals tests

This commit is contained in:
Flaviu Tamas 2015-05-19 16:58:39 -04:00
commit ae0c8573f2

View file

@ -117,50 +117,45 @@ proc `==`*(a, b: Option): bool =
when isMainModule: when isMainModule:
template expect(E: expr, body: stmt) = import unittest
try:
body
assert false, E.type.name & " not raised"
except E:
discard
suite "optionals":
# work around a bug in unittest
let intNone = none(int)
let stringNone = none(string)
block: # example test "example":
proc find(haystack: string, needle: char): Option[int] = proc find(haystack: string, needle: char): Option[int] =
for i, c in haystack: for i, c in haystack:
if c == needle: if c == needle:
return some i return some i
assert("abc".find('c').get() == 2) check("abc".find('c').get() == 2)
let result = "team".find('i') let result = "team".find('i')
assert result == none(int) check result == intNone
assert result.has == false check result.isNone
block: # some test "some":
assert some(6).get() == 6 check some(6).get() == 6
assert some("a").unsafeGet() == "a" check some("a").unsafeGet() == "a"
assert some(6).isSome check some(6).isSome
assert some("a").isSome check some("a").isSome
block: # none test "none":
expect FieldError: expect FieldError:
discard none(int).get() discard none(int).get()
assert(none(int).isNone) check(none(int).isNone)
assert(not none(string).isSome) check(not none(string).isSome)
block: # equality test "equality":
assert some("a") == some("a") check some("a") == some("a")
assert some(7) != some(6) check some(7) != some(6)
assert some("a") != none(string) check some("a") != stringNone
assert none(int) == none(int) check intNone == intNone
when compiles(some("a") == some(5)): when compiles(some("a") == some(5)):
assert false check false
when compiles(none(string) == none(int)): when compiles(none(string) == none(int)):
assert false check false
block: # stringification
assert "some(7)" == $some(7)
assert "none(int)" == $none(int)