summaryrefslogtreecommitdiff
path: root/tests/features/test_destructuring.zc
diff options
context:
space:
mode:
Diffstat (limited to 'tests/features/test_destructuring.zc')
-rw-r--r--tests/features/test_destructuring.zc38
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/features/test_destructuring.zc b/tests/features/test_destructuring.zc
new file mode 100644
index 0000000..b898582
--- /dev/null
+++ b/tests/features/test_destructuring.zc
@@ -0,0 +1,38 @@
+struct Point {
+ x: int;
+ y: int;
+}
+
+struct Rect {
+ w: int;
+ h: int;
+}
+
+fn tuple_ret() -> (int, int) {
+ return (10, 20);
+}
+
+test "test_destructuring" {
+ var p = Point{x: 1, y: 2};
+
+ // Explicit Struct Destructuring (Shorthand)
+ var Point{x, y} = p;
+ assert(x == 1, "x is 1");
+ assert(y == 2, "y is 2");
+
+ // Explicit Struct Destructuring (Renamed)
+ var Point{x: a, y: b} = p;
+ assert(a == 1, "a is 1");
+ assert(b == 2, "b is 2");
+
+ // Anonymous Struct Destructuring (Inferred)
+ // Note: Anonymous block only supports shorthand {x, y} currently.
+ // var {x: x2} = p; // Not supported yet in anonymous block parser
+
+ // Tuple Destructuring
+ var (t1, t2) = tuple_ret();
+ assert(t1 == 10, "t1 is 10");
+ assert(t2 == 20, "t2 is 20");
+
+ println "Destructuring OK";
+}