test: prove ordinary import binding modes
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user