Skip to content

Commit 8a958bb

Browse files
Remaining non-resource tests
1 parent 3d86a23 commit 8a958bb

16 files changed

Lines changed: 433 additions & 2 deletions

File tree

crates/d/src/wit_common.d

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ public:
166166
T unwrapOrElse(D)(scope D fallback)
167167
if (is(D R == return) && is(R : T) && is(D == __parameters))
168168
{ return _present ? _value : fallback(); }
169+
170+
bool opEquals(in Option rhs) const {
171+
if (isSome != rhs.isSome) return false;
172+
173+
if (isSome) return unwrap == rhs.unwrap;
174+
else return true;
175+
}
176+
177+
size_t toHash() const @safe pure nothrow
178+
{
179+
if (isSome) return this.unwrap.hashOf(true.hashOf);
180+
return false.hashOf;
181+
}
169182
}
170183

171184
auto some(T)(inout T value) @safe @nogc nothrow {
@@ -240,6 +253,29 @@ public:
240253
ref inout(E) unwrapErr() inout @trusted @nogc nothrow return
241254
in (isErr) do { return _storage.error; }
242255
}
256+
257+
bool opEquals(in Result rhs) const {
258+
if (isErr != rhs.isErr) return false;
259+
260+
if (isErr) {
261+
static if (!is(E == void)) return unwrapErr == rhs.unwrapErr;
262+
else return true;
263+
}
264+
265+
static if (!is(T == void)) return unwrap == rhs.unwrap;
266+
else return true;
267+
}
268+
269+
size_t toHash() const @safe pure nothrow
270+
{
271+
if (isErr) {
272+
static if (!is(E == void)) return this.unwrapErr.hashOf(true.hashOf);
273+
return true.hashOf;
274+
}
275+
276+
static if (!is(T == void)) return this.unwrap.hashOf(false.hashOf);
277+
else return false.hashOf;
278+
}
243279
}
244280

245281
auto ok(E, T)(inout T value) @safe @nogc nothrow {

tests/runtime/common-types/leaf.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import wit.test.common.leaf;
22
import wit.common;
33

44
@witExport("test:common/to-test", "wrap")
5-
R1 wrap(in F1 flag) {
5+
R1 wrap(F1 flag) {
66
switch (flag.bits) with (F1) {
77
case a.bits:
88
return R1(1, flag);

tests/runtime/common-types/middle.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import wit.common;
44
import imps = wit.test.common.to_test.imports;
55

66
@witExport("test:common/to-test", "wrap")
7-
R1 wrap(in F1 flag) {
7+
R1 wrap(F1 flag) {
88
return imps.wrap(flag);
99
}
1010

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import wit.test.results.intermediate;
2+
import imports = wit.test.results.test.imports;
3+
4+
import wit.common;
5+
6+
@witExport("test:results/test", "string-error")
7+
Result!(float, WitString) stringError(float a) {
8+
return imports.stringError(a);
9+
}
10+
11+
@witExport("test:results/test", "enum-error")
12+
Result!(float, E) enumError(float a) {
13+
return imports.enumError(a);
14+
}
15+
16+
@witExport("test:results/test", "record-error")
17+
Result!(float, E2) recordError(float a) {
18+
return imports.recordError(a);
19+
}
20+
21+
@witExport("test:results/test", "variant-error")
22+
Result!(float, E3) variantError(float a) {
23+
return imports.variantError(a);
24+
}
25+
26+
@witExport("test:results/test", "empty-error")
27+
Result!(uint, void) emptyError(uint a) {
28+
return imports.emptyError(a);
29+
}
30+
31+
@witExport("test:results/test", "double-error")
32+
Result!(Result!(void, WitString), WitString) doubleError(uint a) {
33+
return imports.doubleError(a);
34+
}
35+
36+
alias Exports = wit.test.results.intermediate.Exports!(
37+
stringError,
38+
enumError,
39+
recordError,
40+
variantError,
41+
emptyError,
42+
doubleError
43+
);

tests/runtime/results/leaf.d

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import wit.test.results.leaf;
2+
3+
import wit.common;
4+
5+
@witExport("test:results/test", "string-error")
6+
Result!(float, WitString) stringError(float a) {
7+
if (a == 0.0) {
8+
return "zero".witList.witClone.err!float;
9+
}
10+
11+
return a.ok!WitString;
12+
}
13+
14+
@witExport("test:results/test", "enum-error")
15+
Result!(float, E) enumError(float a) {
16+
if (a == 0.0) {
17+
return E.a.err!float;
18+
}
19+
20+
return a.ok!E;
21+
}
22+
23+
@witExport("test:results/test", "record-error")
24+
Result!(float, E2) recordError(float a) {
25+
if (a == 0.0) {
26+
return E2(
27+
line: 420,
28+
column: 0
29+
).err!float;
30+
} else if (a == 1.0) {
31+
return E2(
32+
line: 77,
33+
column: 2
34+
).err!float;
35+
}
36+
37+
return a.ok!E2;
38+
}
39+
40+
@witExport("test:results/test", "variant-error")
41+
Result!(float, E3) variantError(float a) {
42+
if (a == 0.0) {
43+
return E3.e2(E2(
44+
line: 420,
45+
column: 0
46+
)).err!float;
47+
} else if (a == 1.0) {
48+
return E3.e1(E.b).err!float;
49+
} else if (a == 2.0) {
50+
return E3.e1(E.c).err!float;
51+
}
52+
53+
return a.ok!E3;
54+
}
55+
56+
@witExport("test:results/test", "empty-error")
57+
Result!(uint, void) emptyError(uint a) {
58+
if (a == 0) {
59+
return err!uint;
60+
} else if (a == 1) {
61+
return 42u.ok!void;
62+
}
63+
64+
return a.ok!void;
65+
}
66+
67+
@witExport("test:results/test", "double-error")
68+
Result!(Result!(void, WitString), WitString) doubleError(uint a) {
69+
if (a == 0) {
70+
return ok!WitString.ok!WitString;
71+
} else if (a == 1) {
72+
return "one".witList.witClone.err!void.ok!WitString;
73+
}
74+
75+
return "two".witList.witClone.err!(Result!(void, WitString));
76+
}
77+
78+
alias Exports = wit.test.results.leaf.Exports!(
79+
stringError,
80+
enumError,
81+
recordError,
82+
variantError,
83+
emptyError,
84+
doubleError
85+
);

tests/runtime/results/runner.d

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import wit.test.results.runner;
2+
3+
import wit.common;
4+
5+
@witExport("$root", "run")
6+
void run() {
7+
{
8+
auto result = stringError(0.0);
9+
scope(exit) result.witFree;
10+
11+
assert(result == "zero".witList.err!float);
12+
}
13+
14+
{
15+
auto result = stringError(1.0);
16+
scope(exit) result.witFree;
17+
18+
assert(result == 1.0f.ok!WitString);
19+
}
20+
21+
assert(enumError(0.0) == E.a.err!float);
22+
assert(enumError(1.0) == 1.0f.ok!E);
23+
24+
assert(recordError(0.0) == E2(
25+
line: 420,
26+
column: 0
27+
).err!float);
28+
assert(recordError(1.0) == E2(
29+
line: 77,
30+
column: 2
31+
).err!float);
32+
assert(recordError(2.0).isOk);
33+
34+
assert(variantError(0.0) == E3.e2(E2(
35+
line: 420,
36+
column: 0
37+
)).err!float);
38+
assert(variantError(1.0) == E3.e1(E.b).err!float);
39+
assert(variantError(2.0) == E3.e1(E.c).err!float);
40+
41+
assert(emptyError(0) == err!uint);
42+
assert(emptyError(1) == 42u.ok!void);
43+
assert(emptyError(2) == 2u.ok!void);
44+
45+
{
46+
auto result = doubleError(0);
47+
scope(exit) result.witFree;
48+
49+
assert(result == ok!WitString.ok!WitString);
50+
}
51+
52+
{
53+
auto result = doubleError(1);
54+
scope(exit) result.witFree;
55+
56+
assert(result == "one".witList.err!void.ok!WitString);
57+
}
58+
59+
{
60+
auto result = doubleError(2);
61+
scope(exit) result.witFree;
62+
63+
assert(result == "two".witList.err!(Result!(void, WitString)));
64+
}
65+
}
66+
67+
alias Exports = wit.test.results.runner.Exports!(
68+
run
69+
);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import wit.my.strings.runner;
2+
3+
import wit.common;
4+
5+
@witExport("$root", "run")
6+
void run() {
7+
foo("hello".witList);
8+
9+
auto str = bar();
10+
scope(exit) str.witFree;
11+
12+
assert(str == "world");
13+
}
14+
15+
alias Exports = wit.my.strings.runner.Exports!(
16+
run
17+
);

tests/runtime/strings-alias/test.d

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import wit.my.strings.test;
2+
3+
import wit.common;
4+
5+
@witExport("cat", "foo")
6+
void foo(in MyString str) {
7+
assert(str == "hello");
8+
}
9+
10+
@witExport("cat", "bar")
11+
MyString bar() {
12+
return "world".witList.witClone;
13+
}
14+
15+
alias Exports = wit.my.strings.test.Exports!(
16+
foo,
17+
bar
18+
);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import wit.my.strings.runner;
2+
3+
import wit.common;
4+
5+
@witExport("$root", "run")
6+
void run() {
7+
foo("hello".witList);
8+
9+
auto str = bar();
10+
scope(exit) str.witFree;
11+
12+
assert(str == "world");
13+
}
14+
15+
alias Exports = wit.my.strings.runner.Exports!(
16+
run
17+
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import wit.my.strings.test;
2+
3+
import wit.common;
4+
5+
@witExport("cat", "foo")
6+
void foo(in WitString str) {
7+
assert(str == "hello");
8+
}
9+
10+
@witExport("cat", "bar")
11+
WitString bar() {
12+
return "world".witList.witClone;
13+
}
14+
15+
alias Exports = wit.my.strings.test.Exports!(
16+
foo,
17+
bar
18+
);

0 commit comments

Comments
 (0)