test: prove ordinary import binding modes

This commit is contained in:
2026-08-14 10:56:54 +09:00
parent 792b6ecbe5
commit 10e02a00ee
20 changed files with 1284 additions and 672 deletions

View File

@@ -100,6 +100,37 @@ imports_to_str(const char *src, int *errs, const char **pkg)
return buf;
}
static int
must_import_shape(const char *src, int imports_only, const char *binding,
const char *path, const char *alias)
{
Arena *a = newarena();
Lex l;
Parser p;
char *wrapped = imports_only ? strdup(src) : wwtest_wrap(src);
lexinit(&l, a, imports_only ? "imports.ww" : "<test>", wrapped,
strlen(wrapped));
parserinit(&p, a, &l);
Node *file = imports_only ? parseimports(&p) : parsefile(&p);
Node *u = file ? file->list : NULL;
while (u != NULL && u->kind != N_USE) u = u->next;
int ok = p.errs == 0 && l.errs == 0 && u != NULL
&& u->str != NULL && strcmp(u->str, binding) == 0
&& u->usesource != NULL && strcmp(u->usesource, path) == 0
&& u->usepath != NULL && strcmp(u->usepath, path) == 0
&& ((alias == NULL && u->usealias == NULL)
|| (alias != NULL && u->usealias != NULL
&& strcmp(u->usealias, alias) == 0))
&& u->usepkgname == NULL && u->pos.file != NULL
&& u->pos.line > 0 && u->pos.col > 0;
if (!ok)
fprintf(stderr, "import shape mismatch (%s): %s\n",
imports_only ? "imports-only" : "full", src);
free(wrapped);
freearena(a);
return ok;
}
int
main(void)
{
@@ -108,6 +139,7 @@ main(void)
const char *parses[] = {
"import io;",
"import io.bufio;",
"import stable io.bufio;",
"def MAX: i32 = 4096;",
"export def MAX: i32 = 4096;",
"type point = struct { x: i32, y: i32 };",
@@ -193,6 +225,7 @@ main(void)
}
if (!must_contain("import io;", "(use \"io\"")) fail++;
if (!must_contain("import stable io.bufio;", "(use \"stable\"")) fail++;
if (!must_contain("def N: i32 = 4;", "(def \"N\"")) fail++;
if (!must_contain("def N: i32 = 4;", "(int 4")) fail++;
if (!must_contain("export fn f() void = {};", "(fn \"f\" export")) fail++;
@@ -201,6 +234,14 @@ main(void)
if (!must_contain("fn f() void = { x = 1; };", "(assign =")) fail++;
if (!must_contain("fn f() i32 = { return a + b; };", "(bin +")) fail++;
if (!must_contain("@symbol(\"x\") fn f() void;", "(attr \"symbol\""))fail++;
if (!must_import_shape("import io.bufio;", 0, "bufio", "io.bufio",
NULL)) fail++;
if (!must_import_shape("import stable io.bufio;", 0, "stable",
"io.bufio", "stable")) fail++;
if (!must_import_shape("package main;\nimport io.bufio;\n", 1,
"bufio", "io.bufio", NULL)) fail++;
if (!must_import_shape("package main;\nimport stable io.bufio;\n", 1,
"stable", "io.bufio", "stable")) fail++;
{
const char *src =
@@ -268,6 +309,6 @@ main(void)
fprintf(stderr, "%d parse tests failed\n", fail);
return 1;
}
printf("parse: %d/%d ok + 9 shape ok\n", n, n);
printf("parse: %d/%d ok + 14 shape ok\n", n, n);
return 0;
}

View File

@@ -5,6 +5,6 @@ export fn assert(b: bool, msg: str) void = { };
package main;
import m;
export fn main() i32 = {
assert(false, "routed to m.assert");
m.assert(false, "routed to m.assert");
return 42;
};

View File

@@ -11,4 +11,4 @@ export type gerr2 = !u8;
export fn op() (i32 | gerr1 | gerr2) = { return 0; };
package main;
import beta;
fn main() int = { match (beta.user()) { case let n: i32 => return n: int; case berr => return 99; }; };
fn main() int = { match (beta.user()) { case let n: i32 => return n: int; case beta.berr => return 99; }; };

View File

@@ -10,4 +10,4 @@ export type gerr1 = !void;
export fn other() (i32 | gerr1) = { return 0; };
package main;
import beta;
fn main() int = { match (beta.user()) { case let n: i32 => return n: int; case berr => return 99; }; };
fn main() int = { match (beta.user()) { case let n: i32 => return n: int; case beta.berr => return 99; }; };