Files
sakurac/tests/libraries.sakura
T

367 lines
17 KiB
Plaintext
Raw Permalink Normal View History

2026-10-26 10:14:11 -05:00
# Tests of how a program's library files are found: `core` whole, and the
# modules of the others its files refer to, a round at a time, by their paths.
import std::io;
import std::process;
import std::testing;
import system;
module sakura::analyzer;
#! A resolver of the libraries a check reads: this repository's `core`, read
#! whole, and its `system` and runtime, from its root, where the tests run, and
#! the standard library.
#! @returns The resolver.
def test_finder(): resolver {
var made := resolver::new();
made.whole("core", "core");
made.add_library("system", "system");
made.add_library("runtime", "runtime");
made.add_library("std", std_folder());
return made;
}
#! The standard library's folder: its repository beside this one.
#! @returns The folder.
def std_folder(): strview {
return "../std";
}
#! Text that was lexed and parsed, with what both reported.
struct parsed {
var source: source;
var syntax: syntax;
var notes: vector<note>;
}
#! Lexes and parses the text as a file, as the analyzer's own tests do, with
#! the names of the program it is of, as the files a resolver is given are.
#! @param text The text.
#! @param names The program's names.
#! @returns Its source, syntax, and notes.
def parse(text: strview, names: &mut names): parsed {
var made := parsed {
source = source::from("test.sakura", text),
syntax = syntax::new(),
notes = vector::<note>::new()
};
var lexing := lexer::new(&mut made.source, names, &mut made.notes, 0);
lexing.tokenize_all(&mut made.syntax);
var parsing := parser::new(&mut made.syntax, names, &mut made.notes, 0);
_ = parsing.parse();
return made;
}
#! The library files a program of one file reads: what it refers to, then what
#! those refer to, until nothing new is, each as its path within its library,
#! as `std/io/file.sakura`.
#! @param finder The resolver.
#! @param text The program.
#! @returns The paths, in the order they were found.
def files_read(finder: &mut resolver, text: strview): vector<string> {
var found := vector::<string>::new();
var names := names::new();
val program := parse(text, &mut names);
finder.refer(&program.syntax, &names, &mut found);
var done: u64 = 0;
while done < found.length {
val to := found.length;
for i in done..to {
var at := path::parse(found[i].span) else (_) { continue; };
val held := io::read_file(&at) else (_) { continue; };
val read := parse(held.span, &mut names);
finder.refer(&read.syntax, &names, &mut found);
}
done = to;
}
return found;
}
#! Whether a path read ends as the text does.
#! @param paths The paths.
#! @param end The text.
#! @returns Whether one does.
def reads_any(paths: &vector<string>, end: strview): bool {
for &each in paths {
return true if each.span.ends_with(end);
}
return false;
}
@test def a_program_reads_core_and_the_modules_its_files_refer_to(t: &mut tester): void {
# With no import, nothing but `core`, which is read whole, apart from this.
var alone := test_finder();
val none := files_read(&mut alone, "def main(): void { var v := vector::<i64>::new(); v.append(0..4 is range<i64> ? 1 : 2); }");
t.expects(none.length == 0);
# `std::io`, the root holding it, and `system`, which its files name by path;
# not `std::serial`, `std::math`, or `std::testing`, which no file refers to.
var printing := test_finder();
val io := files_read(&mut printing, "import std::io;\ndef main(): void { io::println(\"hi\"); }");
t.expects(reads_any(&io, "std/io/console.sakura") && reads_any(&io, "std/io/path.sakura"));
t.expects(!reads_any(&io, "std/net/uri.sakura") && !reads_any(&io, "std/containers/deque.sakura"));
t.expects(reads_any(&io, "system/system.sakura") && reads_any(&io, "system/linux/linux.sakura"));
t.expects(!reads_any(&io, "std/serial/serial.sakura") && !reads_any(&io, "std/math/exp.sakura"));
t.expects(!reads_any(&io, "std/testing/testing.sakura"));
# A path reads the module it begins with, with no import; one beginning with
# no module's name, as an enum's, reads nothing.
var pathed := test_finder();
val exits := files_read(&mut pathed, "enum color { red }\ndef main(): void { val c := color::red; system::exit(0); }");
t.expects(reads_any(&exits, "system/system.sakura") && !reads_any(&exits, "std/net/uri.sakura"));
# A module's files reach a sibling they import, as `std::thread`'s do
# `std::time`'s, which is read too.
var threading := test_finder();
val threads := files_read(&mut threading, "import std::thread;\ndef main(): void {}");
t.expects(reads_any(&threads, "std/thread/thread.sakura") && reads_any(&threads, "std/time/duration.sakura"));
t.expects(!reads_any(&threads, "std/serial/serial.sakura"));
# An executable asks for the runtime, a library of its own beside `core`,
# which reads `system` and nothing of the standard library.
var building := test_finder();
var runtime := vector::<string>::new();
building.want("runtime", &mut runtime);
var names := names::new();
var done: u64 = 0;
while done < runtime.length {
val to := runtime.length;
for i in done..to {
var at := path::parse(runtime[i].span) else (_) { continue; };
val held := io::read_file(&at) else (_) { continue; };
val read := parse(held.span, &mut names);
building.refer(&read.syntax, &names, &mut runtime);
}
done = to;
}
t.expects(reads_any(&runtime, "runtime/start.sakura") && reads_any(&runtime, "runtime/memory.sakura"));
t.expects(reads_any(&runtime, "system/windows/windows.sakura"));
val library := std_folder();
for &each in runtime {
t.expects(!each.span.starts_with(library));
}
}
@test def a_module_no_file_refers_to_is_never_read(t: &mut tester): void {
# A library's module whose file does not parse is no matter where nothing
# refers to it, and is read where something does.
val folder := $"{scratch_folder("libraries")}/shelf";
write_text($"{folder}/shelf.sakura", "module shelf;\npub def one(): i64 { return 1; }\n");
write_text($"{folder}/broken/broken.sakura", "module shelf::broken;\npub def oops( {\n");
write_text($"{folder}/parts/a.sakura", "# A part of the root, in a folder of its own.\nmodule shelf;\npub def two(): i64 { return 2; }\n");
write_text($"{folder}/alone.sakura", "# A module of its own, in the root's folder.\nmodule shelf::alone;\npub def three(): i64 { return 3; }\n");
var finder := test_finder();
finder.add_library("shelf", folder.span);
val root := files_read(&mut finder, "import shelf;\ndef main(): void { _ = one() + two(); }");
t.expects(root.length == 2 && reads_any(&root, "shelf/shelf.sakura") && reads_any(&root, "shelf/parts/a.sakura"));
# A file of a module's folder of its own is no part of the module around it,
# and is read where it is referred to.
t.expects(!reads_any(&root, "shelf/alone.sakura"));
var alone := test_finder();
alone.add_library("shelf", folder.span);
val own := files_read(&mut alone, "import shelf::alone;\ndef main(): void { _ = three(); }");
t.expects(reads_any(&own, "shelf/alone.sakura"));
var again := test_finder();
again.add_library("shelf", folder.span);
val broken := files_read(&mut again, "import shelf::broken;\ndef main(): void {}");
t.expects(broken.length == 3 && reads_any(&broken, "shelf/broken/broken.sakura"));
# Completion finds the modules within one by their folders, without reading
# them.
var segments := vector::<string>::new();
segments.append(string::from("shelf", nothing {}));
var shelves := vector::<library>::new();
shelves.append(library { name = string::from("shelf", nothing {}), folder = string::copy_of(&folder) });
val within := modules_within(shelves.span, segments.span);
t.expects(within.length == 2 && within.iter().any(def(x) => x.span == "broken") && within.iter().any(def(x) => x.span == "alone"));
# What the test wrote goes, deepest first.
for &each in ["shelf/broken/broken.sakura", "shelf/parts/a.sakura", "shelf/alone.sakura", "shelf/shelf.sakura", "shelf/broken", "shelf/parts", "shelf", ""] {
val at := each.length == 0 ? scratch_folder("libraries") : $"{scratch_folder("libraries")}/{each}";
var gone := path::parse(at.span) else (_) { continue; };
_ = io::remove(&gone);
}
}
@test def a_library_may_be_a_module_within_another(t: &mut tester): void {
# A library named for a module within another, as `std::yaml` is, holds that
# module and what is within it, and the library around it the rest.
val root := scratch_folder("within");
write_text($"{root}/inner/inner.sakura", "module std::inner;\npub def one(): i64 { return 1; }\n");
write_text($"{root}/inner/deep/deep.sakura", "module std::inner::deep;\npub def two(): i64 { return 2; }\n");
write_text($"{root}/a/a.sakura", "module two::a;\npub def three(): i64 { return 3; }\n");
write_text($"{root}/b/b.sakura", "module two::b;\npub def four(): i64 { return 4; }\n");
val inner := $"{root}/inner";
val first := $"{root}/a";
val second := $"{root}/b";
var finder := test_finder();
finder.add_library("std::inner", inner.span);
val nested := files_read(&mut finder, "import std::inner::deep;\nimport std::io;\ndef main(): void {}");
t.expects(reads_any(&nested, "inner/inner.sakura") && reads_any(&nested, "inner/deep/deep.sakura"));
t.expects(reads_any(&nested, "std/io/path.sakura"));
# Two libraries within one module that is in none, as `sakura::analyzer` and
# `sakura::machine` are, are each found, and the module holding them has no
# files.
var siblings := test_finder();
siblings.add_library("two::a", first.span);
siblings.add_library("two::b", second.span);
val both := files_read(&mut siblings, "import two::a;\nimport two::b;\ndef main(): void { _ = two::a::three() + two::b::four(); }");
t.expects(both.length == 2 && reads_any(&both, "a/a.sakura") && reads_any(&both, "b/b.sakura"));
# Completion offers the libraries within a module beside its own modules.
var shelves := vector::<library>::new();
shelves.append(library { name = string::from("std", nothing {}), folder = string::from(std_folder(), nothing {}) });
shelves.append(library { name = string::from("std::inner", nothing {}), folder = string::copy_of(&inner) });
shelves.append(library { name = string::from("two::a", nothing {}), folder = string::copy_of(&first) });
shelves.append(library { name = string::from("two::b", nothing {}), folder = string::copy_of(&second) });
var std_path := vector::<string>::new();
std_path.append(string::from("std", nothing {}));
val in_std := modules_within(shelves.span, std_path.span);
t.expects(in_std.iter().any(def(x) => x.span == "inner") && in_std.iter().any(def(x) => x.span == "io"));
var two_path := vector::<string>::new();
two_path.append(string::from("two", nothing {}));
val in_two := modules_within(shelves.span, two_path.span);
t.expects(in_two.length == 2 && in_two[0].span == "a" && in_two[1].span == "b");
std_path.append(string::from("inner", nothing {}));
val in_inner := modules_within(shelves.span, std_path.span);
t.expects(in_inner.length == 1 && in_inner[0].span == "deep");
# What the test wrote goes, deepest first.
for &each in ["inner/deep/deep.sakura", "inner/inner.sakura", "a/a.sakura", "b/b.sakura", "inner/deep", "inner", "a", "b", ""] {
val at := each.length == 0 ? string::copy_of(&root) : $"{root}/{each}";
var gone := path::parse(at.span) else (_) { continue; };
_ = io::remove(&gone);
}
}
@test def a_folder_is_of_each_module_a_file_within_it_is_of(t: &mut tester): void {
# A module's folder may hold the one file of a module within it, as
# `nest/deeper.sakura` of `lib::nest::deeper` beside `nest/nest.sakura` of
# `lib::nest`, whichever name comes first.
val root := scratch_folder("deeper");
write_text($"{root}/lib/lib.sakura", "module lib;\npub def zero(): i64 { return 0; }\n");
write_text($"{root}/lib/nest/nest.sakura", "module lib::nest;\npub def one(): i64 { return 1; }\n");
write_text($"{root}/lib/nest/deeper.sakura", "module lib::nest::deeper;\npub def two(): i64 { return 2; }\n");
val folder := $"{root}/lib";
var finder := test_finder();
finder.add_library("lib", folder.span);
val read := files_read(&mut finder, "import lib::nest;\nimport lib::nest::deeper;\ndef main(): void {}");
t.expects(reads_any(&read, "nest/nest.sakura") && reads_any(&read, "nest/deeper.sakura"));
# Completion offers each.
var shelves := vector::<library>::new();
shelves.append(library { name = string::from("lib", nothing {}), folder = string::copy_of(&folder) });
var lib_path := vector::<string>::new();
lib_path.append(string::from("lib", nothing {}));
val in_lib := modules_within(shelves.span, lib_path.span);
t.expects(in_lib.length == 1 && in_lib[0].span == "nest");
lib_path.append(string::from("nest", nothing {}));
val in_nest := modules_within(shelves.span, lib_path.span);
t.expects(in_nest.length == 1 && in_nest[0].span == "deeper");
# What the test wrote goes, deepest first.
for &each in ["lib/nest/deeper.sakura", "lib/nest/nest.sakura", "lib/lib.sakura", "lib/nest", "lib", ""] {
val at := each.length == 0 ? string::copy_of(&root) : $"{root}/{each}";
var gone := path::parse(at.span) else (_) { continue; };
_ = io::remove(&gone);
}
}
@test def a_files_module_is_its_first_module_line(t: &mut tester): void {
t.expects(module_named("#! A file.\nimport std;\n\nmodule std::io;\n").span == "std::io");
t.expects(module_named("extern import \"kernel32.dll\" when os == windows;\n\nextern module system::windows when os == windows;").span == "system::windows");
t.expects(module_named("module system when os == linux && arch == x86_64;").span == "system");
t.expects(module_named("import std;\ndef main(): void {}\nmodule late;").length == 0);
}
@test def a_section_of_another_target_is_read_to_its_line(t: &mut tester): void {
val folder := $"{scratch_folder("targets")}/shelf";
val windows := "# Windows only.\nimport shelf::common;\n\nmodule shelf when os == windows;\npub def oops( {\n";
write_text($"{folder}/on_windows.sakura", windows);
write_text($"{folder}/on_linux.sakura", "module shelf when os == linux;\npub def one(): i64 { return 1; }\n");
write_text($"{folder}/both.sakura", "module shelf when os == windows;\npub def two(): i64 { return 2; }\nmodule shelf when os == linux;\npub def three(): i64 { return 3; }\n");
write_text($"{folder}/common.sakura", "module shelf::common;\npub def four(): i64 { return 4; }\n");
var finder := test_finder();
finder.read_for("linux", "x86_64", "vm");
finder.add_library("shelf", folder.span);
val read := files_read(&mut finder, "import shelf;\ndef main(): void { _ = one() + three(); }");
# The file for Windows is read up to its section's line, with the import
# before it, which is still found; the others are read whole.
t.expects(reads_any(&read, "shelf/on_windows.sakura") && reads_any(&read, "shelf/common.sakura"));
val cut := finder.cut_of($"{folder}/on_windows.sakura".span);
t.expects(cut != 0 && windows.substr(0, cut) == "# Windows only.\nimport shelf::common;\n\nmodule shelf when os == windows;");
t.expects(finder.cut_of($"{folder}/on_linux.sakura".span) == 0);
t.expects(finder.cut_of($"{folder}/both.sakura".span) == 0);
# A resolver given no target reads every file whole.
var whole := test_finder();
whole.add_library("shelf", folder.span);
_ = files_read(&mut whole, "import shelf;\ndef main(): void {}");
t.expects(whole.cut_of($"{folder}/on_windows.sakura".span) == 0);
for &each in ["shelf/on_windows.sakura", "shelf/on_linux.sakura", "shelf/both.sakura", "shelf/common.sakura", "shelf", ""] {
val at := each.length == 0 ? scratch_folder("targets") : $"{scratch_folder("targets")}/{each}";
var gone := path::parse(at.span) else (_) { continue; };
_ = io::remove(&gone);
}
}
@test def a_sections_condition_is_read_from_its_text(t: &mut tester): void {
var target := when_target {
os = string::from("linux", nothing {}),
arch = string::from("x86_64", nothing {}),
backend = string::from("vm", nothing {})
};
t.expects(reads_as(" os == linux", &target) == 1);
t.expects(reads_as(" os == windows", &target) == 0);
t.expects(reads_as(" os == linux && (arch == aarch64 || backend != native)", &target) == 1);
t.expects(reads_as(" !(os == linux) || arch != x86_64", &target) == 0);
# What a reading doesn't know leaves the condition undecided.
t.expects(reads_as(" os == linux && answer == 42", &target) == 2);
t.expects(reads_as(" os == ", &target) == 2);
t.expects(reads_as(" (os == linux", &target) == 2);
}
# What a condition's reading says: 0 where it does not hold, 1 where it does,
# and 2 where it is not decided.
def reads_as(text: strview, target: &when_target): u32 {
var reading := when_reading { text = text };
val holds := reading.holds(target);
return !reading.decided() ? 2 : holds ? 1 : 0;
}
#! A folder of its own for a test, under the system's for such files.
#! @param name The test's name.
#! @returns The folder's path.
def scratch_folder(name: strview): string {
var base: strview = "/tmp";
if process::variable("TMPDIR") is some(found) {
base = found;
} else if process::variable("TEMP") is some(found) {
base = found;
}
return $"{base}/sakura-libraries-{system::getpid()}-{name}";
}
#! Writes a file, making the folders it is in.
#! @param at The file's path.
#! @param text What it holds.
def write_text(at: string, text: strview): void {
var end := at.length;
while end > 0 && at[end - 1] != '/' {
end--;
}
var folder := path::parse(at.span.substr(0, end - 1)) else (_) { return; };
_ = io::create_directories(&folder);
var file := path::parse(at.span) else (_) { return; };
_ = io::write_file(&file, text);
}