aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGravatar zinc <zinc@downsta.ir> 2026-10-14 09:33:03 +0000
committerGravatar zinc <zinc@downsta.ir> 2026-10-14 09:33:03 +0000
commitebaa15f02dd0e5aa358ea2e527ae4557e40b4e9c (patch)
tree9f53ccfed1eedd90df2747bdfd0e9ea907caee95 /tests
parenttodo: record the reverted unchecked trial and what it showed (diff)
constants inside structs, and typo suggestions for fields and static members
const NAME = e; inside a struct body declares a compile-time constant named Struct.NAME, reached as Struct.NAME or by bare name in the struct's methods. A misspelled field or Struct.name member now ends its error with '; did you mean 'name'?' on the member word, which the language server turns into a quick fix.
Diffstat (limited to 'tests')
-rw-r--r--tests/drivers/hint_fixes_test.js4
-rw-r--r--tests/errors/static_const_typo.zc5
-rw-r--r--tests/errors/static_typo.zc5
-rw-r--r--tests/langerrors/struct_const_runtime.zc6
-rw-r--r--tests/programs/struct_const.zc28
-rwxr-xr-xtests/test.sh1
-rw-r--r--tests/typeerrors/field_typo.zc5
7 files changed, 54 insertions, 0 deletions
diff --git a/tests/drivers/hint_fixes_test.js b/tests/drivers/hint_fixes_test.js
index 20d3dd1..a388911 100644
--- a/tests/drivers/hint_fixes_test.js
+++ b/tests/drivers/hint_fixes_test.js
@@ -76,5 +76,9 @@ fixes('a misspelled local function is corrected', 'int count_items() { return 1;
'int count_items() { return 1; }\nfn main() {\n\treturn count_items();\n}\n', 'change to `count_items`');
fixes('a qualified name from a file library adds its include', 'include rt;\nfn main() {\n\treturn actor.vector();\n}\n', 'actor', 0,
'include rt;\ninclude actor;\nfn main() {\n\treturn actor.vector();\n}\n', 'include actor');
+fixes('a misspelled field is corrected', 'include rt;\nstruct point { int length, width; }\nfn main() {\n\tpoint* p = rt.new(point);\n\treturn p.lenght;\n}\n', 'lenght', 0,
+ 'include rt;\nstruct point { int length, width; }\nfn main() {\n\tpoint* p = rt.new(point);\n\treturn p.length;\n}\n', 'change to `length`');
+fixes('a misspelled static function of a library group is corrected', 'include rt;\ninclude std;\nfn main() {\n\treturn std.Text.itao(5, 0);\n}\n', 'itao', 0,
+ 'include rt;\ninclude std;\nfn main() {\n\treturn std.Text.itoa(5, 0);\n}\n', 'change to `itoa`');
console.log(ok + ' hint fix checks passed, ' + bad + ' failed');
process.exit(bad ? 1 : 0);
diff --git a/tests/errors/static_const_typo.zc b/tests/errors/static_const_typo.zc
new file mode 100644
index 0000000..c7f2b7b
--- /dev/null
+++ b/tests/errors/static_const_typo.zc
@@ -0,0 +1,5 @@
+namespace;
+
+// expect: MAXX: is not a static function of this struct; did you mean 'MAX'?
+struct Text { const MAX = 24; int len; };
+fn main() { return Text.MAXX; }
diff --git a/tests/errors/static_typo.zc b/tests/errors/static_typo.zc
new file mode 100644
index 0000000..97f6513
--- /dev/null
+++ b/tests/errors/static_typo.zc
@@ -0,0 +1,5 @@
+namespace;
+
+// expect: itao: is not a static function of this struct; did you mean 'itoa'?
+struct Text { int len; static int itoa(int n) { return n; } };
+fn main() { return Text.itao(1); }
diff --git a/tests/langerrors/struct_const_runtime.zc b/tests/langerrors/struct_const_runtime.zc
new file mode 100644
index 0000000..b55626e
--- /dev/null
+++ b/tests/langerrors/struct_const_runtime.zc
@@ -0,0 +1,6 @@
+namespace;
+
+// expect: a struct constant must be a constant expression
+int f(int x) { return x; }
+struct Text { const MAX = f(3); int len; }
+fn main() { return 0; }
diff --git a/tests/programs/struct_const.zc b/tests/programs/struct_const.zc
new file mode 100644
index 0000000..59af8fd
--- /dev/null
+++ b/tests/programs/struct_const.zc
@@ -0,0 +1,28 @@
+namespace;
+include rt;
+
+struct Text {
+ const MAX = 24, MIN = 1;
+ const int WIDE = MAX * 2;
+ int len, tag;
+ static int top() { return MAX; }
+ static int span() { return MAX - MIN; }
+ int room() { return MAX - this.len; }
+ int total() { int[MIN + 2] a; a[2] = WIDE; return a[2] + this.tag; }
+}
+
+const MAX = 99;
+
+int main() {
+ Text t;
+ t.len = 5; t.tag = 7;
+ rt.put_n(Text.MAX); rt.put_c(32);
+ rt.put_n(Text.MIN + Text.WIDE); rt.put_c(32);
+ rt.put_n(Text.top()); rt.put_c(32);
+ rt.put_n(Text.span()); rt.put_c(32);
+ rt.put_n(t.room()); rt.put_c(32);
+ rt.put_n(t.total()); rt.put_c(32);
+ rt.put_n(t.len + t.tag); rt.put_c(32);
+ rt.put_n(MAX); rt.put_c(10);
+ return 0;
+}
diff --git a/tests/test.sh b/tests/test.sh
index 46625a6..7ae7b9d 100755
--- a/tests/test.sh
+++ b/tests/test.sh
@@ -123,6 +123,7 @@ run(){ # $1 = vm command, $2 = label
chk "$($vm < tests/programs/generics.zc | md5sum)" "$(md5sum < tests/programs/generics.expected)" "$l generic functions are compiled per type arguments"
chk "$($vm < tests/programs/genmeth.zc | md5sum)" "$(md5sum < tests/programs/genmeth.expected)" "$l methods of generic structs are compiled per type arguments"
chk "$($vm < tests/programs/static.zc)" "14 5 9" "$l static functions of a struct are called as Struct.name(args)"
+ chk "$($vm < tests/programs/struct_const.zc)" "24 49 24 23 19 55 12 99" "$l constants declared in a struct are reached as Struct.NAME, and by bare name in its methods"
chk "$($vm < tests/programs/methodown.zc)" "7 9" "$l struct methods and functions return owned objects (.u)"
chk "$($vm < tests/programs/typedmethods.zc)" "14 5 13" "$l struct methods declare a return type and typed parameters"
chk "$($vm < tests/programs/enumscope.zc)" "0 5 6 blue" "$l enum members are reached as Enum.MEMBER, also in a case"
diff --git a/tests/typeerrors/field_typo.zc b/tests/typeerrors/field_typo.zc
new file mode 100644
index 0000000..aa64209
--- /dev/null
+++ b/tests/typeerrors/field_typo.zc
@@ -0,0 +1,5 @@
+namespace;
+
+// expect: point has no field lenght; did you mean 'length'?
+struct point { int length, width; };
+fn main() { point* p; return p.lenght; }