aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/while.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-11-27 00:23:54 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-11-27 00:27:52 -0700
commit17f057c5568bda6b011a213c95aa9538f6fb6a78 (patch)
treed988df1935461c2b28f88cea5f6f50effb240128 /test/behavior/while.zig
parentf0deef1d79db272fa80ef0323b4382ee1936a3e4 (diff)
downloadzig-17f057c5568bda6b011a213c95aa9538f6fb6a78.tar.gz
zig-17f057c5568bda6b011a213c95aa9538f6fb6a78.zip
stage2: implement `@typeName`
* stage1: change the `@typeName` of `@TypeOf(undefined)`, `@TypeOf(null)`, and `@TypeOf(.foo)` to match stage2. * move passing behavior tests to the passing-for-stage2 section.
Diffstat (limited to 'test/behavior/while.zig')
-rw-r--r--test/behavior/while.zig37
1 files changed, 37 insertions, 0 deletions
diff --git a/test/behavior/while.zig b/test/behavior/while.zig
index 9e9ac5dc01..f2f0bc0bbe 100644
--- a/test/behavior/while.zig
+++ b/test/behavior/while.zig
@@ -153,6 +153,20 @@ test "while with optional as condition with else" {
try expect(got_else == 1);
}
+test "while with error union condition" {
+ numbers_left = 10;
+ var sum: i32 = 0;
+ var got_else: i32 = 0;
+ while (getNumberOrErr()) |value| {
+ sum += value;
+ } else |err| {
+ try expect(err == error.OutOfNumbers);
+ got_else += 1;
+ }
+ try expect(sum == 45);
+ try expect(got_else == 1);
+}
+
test "while on bool with else result follow else prong" {
const result = while (returnFalse()) {
break @as(i32, 10);
@@ -199,3 +213,26 @@ fn returnFalse() bool {
fn returnTrue() bool {
return true;
}
+
+test "return with implicit cast from while loop" {
+ returnWithImplicitCastFromWhileLoopTest() catch unreachable;
+}
+fn returnWithImplicitCastFromWhileLoopTest() anyerror!void {
+ while (true) {
+ return;
+ }
+}
+
+test "while on error union with else result follow else prong" {
+ const result = while (returnError()) |value| {
+ break value;
+ } else |_| @as(i32, 2);
+ try expect(result == 2);
+}
+
+test "while on error union with else result follow break prong" {
+ const result = while (returnSuccess(10)) |value| {
+ break value;
+ } else |_| @as(i32, 2);
+ try expect(result == 10);
+}