-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patheithertest.nt
153 lines (123 loc) · 3.3 KB
/
eithertest.nt
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
module eithertest;
macro import std.macro.assert;
import neat.base : LocRange;
import std.string : itoa;
extern(C) void print(string);
(size_t | :else) find(string text, string marker) {
for (i in 0 .. text.length - marker.length + 1) {
if (text[i .. i + marker.length] == marker)
return i;
}
return :else;
}
void main() {
(int | float) either1 = 5;
string s = either1.case(int i: "int " ~ itoa(i), float: "float");
// should be one ref
// TODO
// assert(*cast(size_t*) s.base == 1);
assert(s == "int 5");
// case() does not need to be complete
int test_case_default() {
float f = either1.case(int i: return i);
assert(false);
}
assert(test_case_default == 5);
float f = 2;
(int | float) either2 = f;
assert(either2.case(int i: "int", float: "float") == "float");
mut (int, int | int) either3 = (2, 2);
either3 = 5;
int test() {
(int, int) remainder = either3.case(
(int a, int b): (a, b),
int i: return i,
);
assert(false);
}
assert(test == 5);
mut int count;
(:a | :b) countCalls() { count += 1; return :a; }
countCalls.case { (:a): {} (:b): {} }
assert(count == 1);
((:a | :b), (:c | :d)) test = (:a, :c);
(:else | :success) test1() {
size_t pos = "HelloWorld".find("oWo")? else return :else;
assert(pos == 4);
return :success;
}
test1.case {
:else: assert(false);
:success: {}
}
:else test2() {
auto pos = "HelloWorld".find("uwu")? else return :else;
assert(false);
}
test2;
((:a | :b), int | :c) test3 = (:a, 5);
assert(test3 == (:a, 5));
(:a | :b) test4 = :a;
assert(test4 == :a);
// but case can also take a pure expression
assert(5.case(5: "A", 6: "B") == "A");
testObjHack;
import std.error : Error;
(:a | Error | :c) foo() { return :a; }
(:a | Error | void) test() {
:c c = foo.case(:a: return :a)?;
}
Error bar() { return new Error("Hello World"); }
Error barTest() {
bottom b = bar?;
assert(false);
}
{
(int | :else) a = 3;
assert(a?.(true) else false);
}
{
(int | :else) a = :else;
assert(a?.(false) else true);
}
print("Success.");
}
class Foo {
this() { }
}
class Bar {
Foo foo;
this(this.foo) {}
}
class Baz {
nullable Foo foo;
this(this.foo) {}
}
void testObjHack() {
bool test(nullable Foo nullableFoo) {
Foo foo = nullableFoo.case(null: return false);
return true;
}
assert(!test(null));
assert(test(new Foo));
nullable Foo test2(nullable Bar bar) {
Foo foo = bar.case(null: return null).foo;
return foo;
}
assert(!test2(null));
assert(test2(new Bar(new Foo)));
bool test3(nullable Foo nullableFoo) {
(bottom | bool) die() { return false; }
Foo foo = nullableFoo.case(null: die.case(bool b: return b));
return true;
}
assert(!test3(null));
assert(test3(new Foo));
void test4(nullable Baz nullableBaz, (Foo | bool) outcome) {
assert((nullableBaz?.foo? else false) == outcome);
}
auto foo = new Foo;
test4(null, false);
test4(new Baz(null), false);
test4(new Baz(foo), foo);
}