|
9 | 9 | # This file may not be copied, modified, or distributed except according to
|
10 | 10 | # those terms.
|
11 | 11 |
|
| 12 | +import options |
12 | 13 | import ./helpers
|
13 | 14 | import ../libp2p/utility
|
14 | 15 |
|
@@ -71,3 +72,88 @@ suite "Utility":
|
71 | 72 | test "unsuccessful safeConvert from uint to int":
|
72 | 73 | check not (compiles do:
|
73 | 74 | result: uint = safeConvert[int, uint](11.uint))
|
| 75 | + |
| 76 | +suite "withValue and valueOr templates": |
| 77 | + type |
| 78 | + TestObj = ref object |
| 79 | + x: int |
| 80 | + |
| 81 | + proc objIncAndOpt(self: TestObj): Opt[TestObj] = |
| 82 | + self.x.inc() |
| 83 | + return Opt.some(self) |
| 84 | + |
| 85 | + proc objIncAndOption(self: TestObj): Option[TestObj] = |
| 86 | + self.x.inc() |
| 87 | + return some(self) |
| 88 | + |
| 89 | + test "withValue calls right branch when Opt/Option is none": |
| 90 | + var counter = 0 |
| 91 | + # check Opt/Option withValue with else |
| 92 | + Opt.none(TestObj).withValue(v): |
| 93 | + fail() |
| 94 | + else: |
| 95 | + counter.inc() |
| 96 | + none(TestObj).withValue(v): |
| 97 | + fail() |
| 98 | + else: |
| 99 | + counter.inc() |
| 100 | + check counter == 2 |
| 101 | + |
| 102 | + # check Opt/Option withValue without else |
| 103 | + Opt.none(TestObj).withValue(v): |
| 104 | + fail() |
| 105 | + none(TestObj).withValue(v): |
| 106 | + fail() |
| 107 | + |
| 108 | + test "withValue calls right branch when Opt/Option is some": |
| 109 | + var counter = 1 |
| 110 | + # check Opt/Option withValue with else |
| 111 | + Opt.some(counter).withValue(v): |
| 112 | + counter.inc(v) |
| 113 | + else: |
| 114 | + fail() |
| 115 | + some(counter).withValue(v): |
| 116 | + counter.inc(v) |
| 117 | + else: |
| 118 | + fail() |
| 119 | + |
| 120 | + # check Opt/Option withValue without else |
| 121 | + Opt.some(counter).withValue(v): |
| 122 | + counter.inc(v) |
| 123 | + some(counter).withValue(v): |
| 124 | + counter.inc(v) |
| 125 | + check counter == 16 |
| 126 | + |
| 127 | + test "withValue calls right branch when Opt/Option is some with proc call": |
| 128 | + var obj = TestObj(x: 0) |
| 129 | + # check Opt/Option withValue with else |
| 130 | + objIncAndOpt(obj).withValue(v): |
| 131 | + v.x.inc() |
| 132 | + else: |
| 133 | + fail() |
| 134 | + objIncAndOption(obj).withValue(v): |
| 135 | + v.x.inc() |
| 136 | + else: |
| 137 | + fail() |
| 138 | + |
| 139 | + # check Opt/Option withValue without else |
| 140 | + objIncAndOpt(obj).withValue(v): |
| 141 | + v.x.inc() |
| 142 | + objIncAndOption(obj).withValue(v): |
| 143 | + v.x.inc() |
| 144 | + |
| 145 | + check obj.x == 8 |
| 146 | + |
| 147 | + test "valueOr calls with and without proc call": |
| 148 | + var obj = none(TestObj).valueOr: |
| 149 | + TestObj(x: 0) |
| 150 | + check obj.x == 0 |
| 151 | + obj = some(TestObj(x: 2)).valueOr: |
| 152 | + fail() |
| 153 | + return |
| 154 | + check obj.x == 2 |
| 155 | + |
| 156 | + obj = objIncAndOpt(obj).valueOr: |
| 157 | + fail() |
| 158 | + return |
| 159 | + check obj.x == 3 |
0 commit comments