summary refs log tree commit diff stats
path: root/tests/stdlib/toptions.nim
blob: 71c52a07e49aade68075d3d0d8e7fb4f9e38a269 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
discard """
  targets: "c js"
"""

import std/[json, options]


# 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


template disableJsVm(body) =
  # something doesn't work in JS VM
  when defined(js):
    when nimvm: discard
    else: body
  else:
    body

proc main() =
  type
    Foo = ref object
      test: string
    Test = object
      foo: Option[Foo]

  let js = """{"foo": {"test": "123"}}"""
  let parsed = parseJson(js)
  let a = parsed.to(Test)
  doAssert $(%*a) == """{"foo":{"test":"123"}}"""

  block options:
    # work around a bug in unittest
    let intNone = none(int)
    let stringNone = none(string)

    block example:
      proc find(haystack: string, needle: char): Option[int] =
        for i, c in haystack:
          if c == needle:
            return some i

      doAssert("abc".find('c').get() == 2)

      let result = "team".find('i')

      doAssert result == intNone
      doAssert result.isNone

    block some:
      doAssert some(6).get() == 6
      doAssert some("a").unsafeGet() == "a"
      doAssert some(6).isSome
      doAssert some("a").isSome

    block none:
      doAssertRaises UnpackDefect:
        discard none(int).get()
      doAssert(none(int).isNone)
      doAssert(not none(string).isSome)

    block equality:
      doAssert some("a") == some("a")
      doAssert some(7) != some(6)
      doAssert some("a") != stringNone
      doAssert intNone == intNone

      when compiles(some("a") == some(5)):
        doAssert false
      when compiles(none(string) == none(int)):
        doAssert false

    block get_with_a_default_value:
      doAssert(some("Correct").get("Wrong") == "Correct")
      doAssert(stringNone.get("Correct") == "Correct")

    block stringify:
      doAssert($(some("Correct")) == "some(\"Correct\")")
      doAssert($(stringNone) == "none(string)")

    disableJsVm:
      block map_with_a_void_result:
        var procRan = 0
        # TODO closure anonymous functions doesn't work in VM with JS
        # Error: cannot evaluate at compile time: procRan
        some(123).map(proc (v: int) = procRan = v)
        doAssert procRan == 123
        intNone.map(proc (v: int) = doAssert false)

    block map:
      doAssert(some(123).map(proc (v: int): int = v * 2) == some(246))
      doAssert(intNone.map(proc (v: int): int = v * 2).isNone)

    block filter:
      doAssert(some(123).filter(proc (v: int): bool = v == 123) == some(123))
      doAssert(some(456).filter(proc (v: int): bool = v == 123).isNone)
      doAssert(intNone.filter(proc (v: int): bool = doAssert false).isNone)

    block flatMap:
      proc addOneIfNotZero(v: int): Option[int] =
        if v != 0:
          result = some(v + 1)
        else:
          result = none(int)

      doAssert(some(1).flatMap(addOneIfNotZero) == some(2))
      doAssert(some(0).flatMap(addOneIfNotZero) == none(int))
      doAssert(some(1).flatMap(addOneIfNotZero).flatMap(addOneIfNotZero) == some(3))

      proc maybeToString(v: int): Option[string] =
        if v != 0:
          result = some($v)
        else:
          result = none(string)

      doAssert(some(1).flatMap(maybeToString) == some("1"))

      proc maybeExclaim(v: string): Option[string] =
        if v != "":
          result = some v & "!"
        else:
          result = none(string)

      doAssert(some(1).flatMap(maybeToString).flatMap(maybeExclaim) == some("1!"))
      doAssert(some(0).flatMap(maybeToString).flatMap(maybeExclaim) == none(string))

    block SomePointer:
      var intref: ref int
      doAssert(option(intref).isNone)
      intref.new
      doAssert(option(intref).isSome)

      let tmp = option(intref)
      doAssert(sizeof(tmp) == sizeof(ptr int))

      var prc = proc (x: int): int = x + 1
      doAssert(option(prc).isSome)
      prc = nil
      doAssert(option(prc).isNone)

    block:
      doAssert(none[int]().isNone)
      doAssert(none(int) == none[int]())

    # "$ on typed with .name"
    block:
      type Named = object
        name: string

      let nobody = none(Named)
      doAssert($nobody == "none(Named)")

    # "$ on type with name()"
    block:
      type Person = object
        myname: string

      let noperson = none(Person)
      doAssert($noperson == "none(Person)")

    # "Ref type with overloaded `==`"
    block:
      let p = some(RefPerson.new())
      doAssert p.isSome

    block: # test cstring
      block:
        let x = some("".cstring)
        doAssert x.isSome
        doAssert x.get == ""

      block:
        let x = some("12345".cstring)
        doAssert x.isSome
        doAssert x.get == "12345"

      block:
        let x = "12345".cstring
        let y = some(x)
        doAssert y.isSome
        doAssert y.get == "12345"

      block:
        let x = none(cstring)
        doAssert x.isNone
        doAssert $x == "none(cstring)"


static: main()
main()