Files
sakurac/tests/analyzer/queries.sakura
T
2026-10-28 22:13:49 -05:00

478 lines
17 KiB
Plaintext

#! @file queries.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the tests of the queries an editor asks of a checked program: what
#! hovering a name shows, what it refers to and where that is written, what
#! may be written at a place, what a call being written calls, and what each
#! name of a file is.
import std::testing;
module sakura::analyzer;
@test def hovering_a_declaration_shows_how_it_is_declared(t: &mut tester): void {
val text := """
#! A point on the plane.
pub struct point {
pub var x: i64; #! Across.
var y: i64 = 7;
}
#! How far across.
#! @param p The point.
pub def across(p: &point): i64 { return p.x; }
def point::moved(&this, by: i64): point { return point { x = this.x + by, y = this.y }; }
enum color: u8 { red, green = 4 }
const limit: u32 = 9;
def f(): void {
var p := point { x = 1 };
val q := p.moved(2);
val c := color::green;
val n := across(&q) + limit as i64;
}
""";
t.expects(hover_of(text, "point {", 0).view() == "pub struct point {\n pub var x: int;\n var y: int;\n}\n\nA point on the plane.");
t.expects(hover_of(text, "x: i64").view() == "pub var x: int\n\nAcross.");
t.expects(hover_of(text, "across(p").view() == "pub def across(p: &point): int\n\nHow far across.\n@param p The point.");
t.expects(hover_of(text, "p: &point").view() == "p: &point\n\nThe point.");
t.expects(hover_of(text, "moved(2)").view() == "def point::moved(this: &point, by: int): point");
t.expects(hover_of(text, "color: u8").view() == "enum color: u8 {\n red = 0,\n green = 4,\n}");
t.expects(hover_of(text, "green;").view() == "color::green = 4");
t.expects(hover_of(text, "limit as").view() == "const limit: u32");
t.expects(hover_of(text, "p := point").view() == "var p: point");
t.expects(hover_of(text, "q := p").view() == "val q: point");
t.expects(hover_of(text, "y = this", 0).view() == "var y: int");
t.expects(hover_of(text, "i64; #!").view() == "type int");
t.expects(hover_of(text, "return p.x").view() == "");
}
@test def hovering_names_through_modules_and_generics(t: &mut tester): void {
val lib := """
module shapes;
pub struct box<T> { pub var item: T; }
pub def box<T>::get(&this): T { return this.item; }
""";
val text := """
import shapes;
def f(b: shapes::box<u8>): u8 { return b.item + b.get(); }
""";
t.expects(asked(def(c: &mut checker): string {
var made := string::new(nothing {});
var at := c.hover(0, place_in(text, "shapes::box", 0)) else { return made; };
made.append(at.signature.view());
made.append("|");
var box := c.hover(0, place_in(text, "box<u8>")) else { return made; };
made.append(box.signature.view());
made.append("|");
var item := c.hover(0, place_in(text, "item +")) else { return made; };
made.append(item.signature.view());
return made;
}, text, lib).view() == "module shapes|pub struct box<T> {\n pub var item: T;\n}|pub var item: u8");
}
@test def hovering_a_call_writes_a_pack_as_the_types_it_gives(t: &mut tester): void {
val text := """
def count<Ts...>(xs: Ts...): i64 { return 0; }
def f(): i64 { return count(1, true); }
""";
# The pack given for `Ts` is written as notes write it, rather than as `(int, bool)`.
val shown := hover_of(text, "count(1");
t.expects(shown.view().starts_with("def count<int, bool>("));
}
#! Where each place a query gives is, as `line:column` of the file it is in,
#! each file named by its index, joined by spaces.
#! @param c The checker.
#! @param places The places.
#! @returns The text.
def places_text(c: &mut checker, places: &vector<spot>): string {
var made := string::new(nothing {});
for &each in places {
val at := c.units[each.file].source.lines.place_of(each.offset);
val text := $"{each.file}:{at.line}:{at.column}";
if made.length != 0 {
made.append(" ");
}
made.append(text.view());
}
return made;
}
@test def names_go_to_where_they_are_declared(t: &mut tester): void {
val lib := """
module shapes;
pub struct box { pub var w: i64; }
pub def area(b: &box): i64 { return b.w; }
""";
val text := """
import shapes;
def f(n: i64): i64 {
val b := box { w = n };
return area(&b) + b.w + n;
}
""";
t.expects(asked(def(c: &mut checker): string {
var made := string::new(nothing {});
for &needle in ["area(&b)", "w + n", "b.w", "n;", "box {", "w = n", "shapes;", "i64 {"] {
var named := c.name_at(0, place_in(text, needle)) else {
made.append("none|");
continue;
};
var places := vector::<spot>::new();
if named.declared {
places.append(named.declaration);
}
var shown := places_text(c, &places);
made.append(shown.view());
if named.refusal.length != 0 {
made.append("!");
}
made.append("|");
}
return made;
}, text, lib).view() == "1:3:9|1:2:26|0:3:7|0:2:7|1:2:12|1:2:26|1:1:8!|!|");
}
@test def references_are_every_place_a_name_is_written(t: &mut tester): void {
val lib := """
module shapes;
pub struct box { pub var w: i64; }
pub def area(b: &box): i64 { return b.w; }
""";
val text := """
import shapes;
def f(n: i64): i64 {
val b := box { w = n };
return area(&b) + b.w + n;
}
def g(): i64 { return area(&box { w = 2 }); }
""";
t.expects(asked(def(c: &mut checker): string {
var made := string::new(nothing {});
for &needle in ["area(&b)", "w + n", "n;", "b :="] {
var found := c.references(0, place_in(text, needle));
var shown := places_text(c, &found);
made.append(shown.view());
made.append("|");
}
return made;
}, text, lib).view() == "1:3:9 0:4:10 0:6:23|1:2:26 1:3:39 0:3:18 0:4:23 0:6:35|0:2:7 0:3:22 0:4:27|0:3:7 0:4:16 0:4:21|");
}
@test def a_trait_method_is_renamed_with_those_implementing_it(t: &mut tester): void {
val text := """
trait shape { def area(&this): i64; }
struct square : shape { var w: i64; }
def square::area(&this): i64 { return this.w * this.w; }
struct dot {}
def dot::area(&this): i64 { return 0; }
def f(s: &square): i64 { return s.area(); }
""";
t.expects(asked(def(c: &mut checker): string {
var found := c.references(0, place_in(text, "area(&this): i64;"));
return places_text(c, &found);
}, text).view() == "0:1:19 0:3:13 0:6:35");
}
@test def renaming_refuses_what_the_compiler_finds_by_name(t: &mut tester): void {
val text := """
def main(): void { val n: i64 = 1; }
struct box {}
def box::get(&this): i64 { return 0; }
""";
t.expects(asked(def(c: &mut checker): string {
var made := string::new(nothing {});
for &needle in ["main", "i64", "this)", "box {}"] {
var named := c.name_at(0, place_in(text, needle)) else {
made.append("none|");
continue;
};
made.append(named.refusal);
made.append("|");
}
return made;
}, text).view() == "A program begins at the function named `main`.|It is built into the language.|`this` is what every method calls its receiver.||");
}
@test def accessors_name_this_and_value_where_they_are_used(t: &mut tester): void {
val text := """
struct r { var w: i64; }
var r::width: i64 {
get => this.w;
set => this.w = value;
}
""";
t.expects(hover_of(text, "value;").view() == "value: int");
t.expects(hover_of(text, "this.w;").view() == "this: &r");
t.expects(asked(def(c: &mut checker): string {
var made := string::new(nothing {});
for &needle in ["value;", "set =>"] {
var named := c.name_at(0, place_in(text, needle)) else {
made.append("none|");
continue;
};
made.append(named.refusal);
made.append("|");
}
return made;
}, text).view() == "`value` is what every `set` calls what it is given.|none|");
}
@test def renaming_refuses_the_methods_operators_call(t: &mut tester): void {
# A type has `+` by its `add`, so renaming the method would lose the operator,
# but one of the name taking something else than an operand is only a method.
val text := """
struct box { var w: i64; }
def box::add(&this, other: &box): box { return box { w = this.w + other.w }; }
def box::negate(&this): box { return box { w = -this.w }; }
def box::mul(&this, by: &i64, more: &i64): box { return box { w = this.w * by * more }; }
def box::grown(&this): box { return this + this; }
""";
t.expects(asked(def(c: &mut checker): string {
var made := string::new(nothing {});
for &needle in ["add(&this", "negate(&this", "mul(&this", "grown(&this"] {
var named := c.name_at(0, place_in(text, needle)) else {
made.append("none|");
continue;
};
made.append(named.refusal.length != 0 ? "refused|" : "renamed|");
}
return made;
}, text).view() == "refused|refused|renamed|renamed|");
}
@test def hovering_a_function_shows_the_contract_its_body_begins_with(t: &mut tester): void {
val text := """
#! Divides.
def divide(a: i64, b: i64): i64 {
expects(b != 0,
"Nothing is divided by zero.");
ensures(q: q * b + a % b == a);
asserts(b > 0);
return a / b;
}
def f(): void { val q := divide(7, 2); }
""";
t.expects(hover_of(text, "divide(7").view() ==
"def divide(a: int, b: int): int\nexpects(b != 0, \"Nothing is divided by zero.\")\nensures(q: q * b + a % b == a)\n\nDivides.");
}
@test def contracts_are_coloured_as_words_of_the_language(t: &mut tester): void {
val text := "def f(n: i64): i64 { expects(n > 0); ensures(r: r > old(n)); return n + 1; }";
t.expects(asked(def(c: &mut checker): string {
var made := string::new(nothing {});
for &each in c.highlights(0) {
continue if each.kind != symbol::keyword;
made.append(text.substr(each.offset as u64, each.length as u64));
made.append(" ");
}
return made;
}, text).view() == "expects ensures old ");
}
@test def undefined_is_coloured_as_a_word_of_the_language_where_it_is_one(t: &mut tester): void {
val text := "def f(): u64 { var b: u64[2] = undefined; val undefined: u64 = 1; return undefined; }";
t.expects(asked(def(c: &mut checker): string {
var made := string::new(nothing {});
for &each in c.highlights(0) {
continue if text.substr(each.offset as u64, each.length as u64) != "undefined";
made.append($"{each.offset}:{each.kind == symbol::keyword} ");
}
return made;
}, text).view() == "31:true 46:false 73:false ");
}
@test def names_are_coloured_by_what_they_are(t: &mut tester): void {
val text := "struct p { var x: i64; }\ndef f(a: p): i64 { val y := a.x; return y; }";
t.expects(asked(def(c: &mut checker): string {
var made := string::new(nothing {});
val found := c.highlights(0);
for &each in found {
val name := text.substr(each.offset as u64, each.length as u64);
val line := $"{name}:{each.kind as u32}{each.declared ? "+" : ""}{each.readonly ? "r" : ""} ";
made.append(line.view());
}
return made;
}, text).view() == "p:8+ x:7+ i64:12r f:2+ a:6+ p:8r i64:12r y:4+r a:6 x:7 y:4r ");
}
#! The names completion offers at a place, joined by spaces.
#! @param text The program's text, of one file.
#! @param needle Where in it the place is.
#! @param skip How far into the needle the place is.
#! @param others The other files.
#! @returns The names.
def completed(text: strview, needle: strview, skip: u32, others: strview...): string {
var texts := vector::<strview>::new();
texts.append(text);
for &each in others {
texts.append(each);
}
return asked(def(c: &mut checker): string {
var made := string::new(nothing {});
val found := c.complete(0, place_in(text, needle, skip));
for &each in found {
break if each.kind == symbol::keyword && each.name.view() != "else";
if made.length != 0 {
made.append(" ");
}
made.append(each.name.view());
if each.insert.length != 0 {
made.append("=");
made.append(each.insert.view());
}
}
return made;
}, ...texts.view());
}
@test def completion_offers_what_the_place_takes(t: &mut tester): void {
val text := """
struct point { var x: i64; var y: i64; }
def point::norm(&this): i64 { return this.x; }
enum color { red, green }
def f(p: point, c: color): i64 {
val q := point { x = 1, };
val n := p.sakura__completion;
match c { red => {} }
return 0;
}
""";
t.expects(completed(text, ".sakura__completion", 1).view() == "x y norm");
t.expects(completed(text, "x = 1, ", 7).view().starts_with("y=y = p c point color f "));
t.expects(completed(text, "red => {} }", 10).view() == "green=green => else=else => ");
val locals := completed(text, "return 0", 7);
t.expects(locals.view().starts_with("q n p c point color f "));
}
@test def completion_offers_the_names_a_pattern_binds(t: &mut tester): void {
val text := """
struct lanes<L[N]> { var items: L[N]; }
def lanes<L[N]>::count(&this): u64 {
return N;
}
""";
t.expects(completed(text, "return N", 7).view().starts_with("this L N "));
}
@test def signature_help_shows_what_is_called(t: &mut tester): void {
val text := """
#! Adds.
#! @param a The first.
def add(a: i64, b: i64): i64 { return a + b; }
def add(a: i64): i64 { return a; }
def f(): i64 { return add(1, 2); }
""";
t.expects(asked(def(c: &mut checker): string {
var made := string::new(nothing {});
var help := c.calls_at(0, place_in(text, "2);")) else { return made; };
for &each in help.overloads {
made.append(each.signature.view());
made.append("|");
}
val line := $"{help.parameter}";
made.append(line.view());
return made;
}, text).view() == "def add(a: int, b: int): int|def add(a: int): int|1");
}
@test def an_edit_parsing_a_file_whole_again_checks_it_anew(t: &mut tester): void {
# Taking away the `}` of the first function has it swallow the last, which has
# the file parsed whole again and every node made anew, so what was kept of
# the old ones, as the nodes of the functions taken away, is let go of.
val before := """
def a(): i64 { return 1; }
def b(): i64 { return a() + 2; }
def c(): i64 { return b() + 3; }
def d(): i64 { return c() + 4; }
def e(): i64 { return 5; }
""";
t.expects(hovers_after_edits(before, "def a(): i64 { return 1;\ndef e(): i64 { return 5; }\n").view() == "");
t.expects(hovers_after_edits(before, "def a(): i64 { return 1;\ndef e(): i64 { return 5; }\n", before).view() == "");
}
@test def an_edit_keeps_what_was_worked_out_of_each_file_apart(t: &mut tester): void {
# Another file whose top-level declaration has the same node as one the edit
# keeps, but whose nodes begin elsewhere, leaves what is kept of this one as
# it was.
val before := """
def f(n: i64): i64 { return n; }
def g(n: i64): i64 { val m := n + 1; return m * f(m); }
""";
val after := """
def f(n: i64): i64 { return n + 0; }
def g(n: i64): i64 { val m := n + 1; return m * f(m); }
""";
val kept: u64 = top_nodes(before)[1];
var other := string::new(nothing {});
for pad in 0u..24u {
var text := string::from("def p(): void { ", nothing {});
for _ in 0..(pad / 3) {
text.append("val x := 1; ");
}
for _ in 0..(pad % 3) {
text.append("1; ");
}
text.append("}\n");
for _ in 0..8 {
text.append("def q(): void {}\n");
}
for &each in top_nodes(text.view()) {
if each >> 32 == kept >> 32 && (each & 0xFFFFFFFF) > (kept & 0xFFFFFFFF) {
other = move(&mut text);
break;
}
}
break if other.length != 0;
}
t.expects(other.length != 0);
var others := vector::<strview>::new();
others.append(other.view());
t.expects(hovers_after_edits_with(others.view(), before, after).view() == "");
}
@test def queries_after_edits_answer_as_a_whole_check_does(t: &mut tester): void {
val before := """
struct adder { var calls: i64; }
def adder::add(&mut this, a: i64, b: i64): i64 { this.calls++; return a + b; }
def make(): adder { return adder {}; }
def main(): void {
var a := adder {};
val n := a.add(1, 2);
}
""";
val typing := """
struct adder { var calls: i64; }
def adder::add(&mut this, a: i64, b: i64): i64 { this.calls++; return a + b; }
def make(): adder { return adder {}; }
def main(): void {
var a := adder {};
val q := a.
val n := a.add(1, 2);
}
""";
val completing := """
struct adder { var calls: i64; }
def adder::add(&mut this, a: i64, b: i64): i64 { this.calls++; return a + b; }
def make(): adder { return adder {}; }
def main(): void {
var a := adder {};
val q := a.sakura__completion
val n := a.add(1, 2);
}
""";
val wrong := """
struct adder { var calls: i64; }
def adder::add(&mut this, a: i64, b: i64): i64 { this.calls++; return a + b; }
def make(): u8 { return adder {}; }
def main(): void {
var a := adder {};
val q := a.calls;
val r: bool = 5;
val n := a.add(1, 2) + make();
}
""";
t.expects(hovers_after_edits(before, typing, completing, wrong, before).view() == "");
}