about summary refs log tree commit diff
path: root/lang/zig/enum-literal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'lang/zig/enum-literal.zig')
-rw-r--r--lang/zig/enum-literal.zig35
1 files changed, 35 insertions, 0 deletions
diff --git a/lang/zig/enum-literal.zig b/lang/zig/enum-literal.zig
new file mode 100644
index 0000000..587cfb2
--- /dev/null
+++ b/lang/zig/enum-literal.zig
@@ -0,0 +1,35 @@
+const expect = @import("std").testing.expect;
+
+const Color = enum { auto, off, on };
+
+test "enum literals" {
+    const color1: Color = .auto;
+    const color2 = Color.auto;
+    expect(color1 == color2);
+    expect(color1 == .auto);
+}
+
+test "switch using enum literals" {
+    expect(switch (Color.on) {
+        .auto => false,
+        .on => true,
+        .off => false,
+    });
+}
+
+const Number = enum(u8) { one, two, three, _ };
+
+test "switch on non-exhaustive enum" {
+    const number = Number.one;
+    expect(switch (number) {
+        .one => true,
+        .two,
+        .three => false,
+        _ => false,
+    });
+    const is_one = switch (number) {
+        .one => true,
+        else => false,
+    };
+    expect(is_one);
+}