129 lines
5.7 KiB
Plaintext
129 lines
5.7 KiB
Plaintext
#! @file reparse.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the tests of parsing a file again after an edit, which must give the
|
|
#! tree and notes a whole parse of the edited text gives.
|
|
|
|
import std::testing;
|
|
module sakura::analyzer;
|
|
|
|
|
|
@test def an_edit_within_a_declaration(t: &mut tester): void {
|
|
t.expects(reparses_as_whole(
|
|
"val a := 1;\ndef f(): void { g(1); }\nval b := 2;",
|
|
"val a := 1;\ndef f(): void { g(1, 2 + 3); h(); }\nval b := 2;"));
|
|
t.expects(reparses_as_whole(
|
|
"def f(): void {}\nval b := 2;",
|
|
"def f(): i64 { return 1; }\nval b := 2;"));
|
|
}
|
|
|
|
@test def an_edit_of_only_spacing_and_comments(t: &mut tester): void {
|
|
t.expects(reparses_as_whole(
|
|
"val a := 1;\nval b := 2;",
|
|
"val a := 1;\n\n# a comment\n#! documentation\nval b := 2;"));
|
|
}
|
|
|
|
@test def declarations_added_and_taken_away(t: &mut tester): void {
|
|
t.expects(reparses_as_whole("val a := 1;\nval c := 3;", "val a := 1;\nval b := 2;\nval c := 3;"));
|
|
t.expects(reparses_as_whole("val a := 1;\nval b := 2;\nval c := 3;", "val a := 1;\nval c := 3;"));
|
|
t.expects(reparses_as_whole("val a := 1;", "val a := 1;\nstruct s { var x: i64; }"));
|
|
t.expects(reparses_as_whole("val a := 1;\nval b := 2;", "val b := 2;"));
|
|
t.expects(reparses_as_whole("val a := 1;", ""));
|
|
}
|
|
|
|
@test def edits_that_make_and_mend_errors(t: &mut tester): void {
|
|
t.expects(reparses_as_whole("val a := 1;\nval b := 2;\nval c := 3;", "val a := 1;\nval b := ;\nval c := 3;"));
|
|
t.expects(reparses_as_whole("val a := 1;\nval b := ;\nval c := 3;", "val a := 1;\nval b := 2;\nval c := 3;"));
|
|
t.expects(reparses_as_whole("val a := 0x;\nval b := 2;", "val a := 0x1;\nval b := 2;"));
|
|
}
|
|
|
|
@test def an_edit_that_opens_what_runs_on(t: &mut tester): void {
|
|
# Taking away a `}` makes the function take in what follows it, which only
|
|
# parsing the whole file again finds.
|
|
t.expects(reparses_as_whole(
|
|
"def f(): void { g(); }\nval b := 2;\nval c := 3;",
|
|
"def f(): void { g();\nval b := 2;\nval c := 3;"));
|
|
}
|
|
|
|
@test def what_the_edit_did_not_touch_is_kept(t: &mut tester): void {
|
|
var made := parse("val a := 1;\nval b := 2;\nval c := 3;");
|
|
val decls := made.syntax.node_lefts.at(made.root);
|
|
val first := made.syntax.run_at(decls, 0);
|
|
val last := made.syntax.run_at(decls, 2);
|
|
|
|
var fresh := lex("val a := 1;\nval b := 20 + 2;\nval c := 3;");
|
|
made.root = made.syntax.reparse(&mut fresh.syntax, &fresh.names, made.root, &mut made.notes, &mut fresh.notes, 0);
|
|
|
|
val after := made.syntax.node_lefts.at(made.root);
|
|
t.expects(made.syntax.run_at(after, 0) == first);
|
|
t.expects(made.syntax.run_at(after, 2) == last);
|
|
|
|
# What moved along with the edit says where it is now.
|
|
t.expects(made.syntax.token(made.syntax.node_tokens.at(last), &fresh.names).offset == 33);
|
|
}
|
|
|
|
@test def what_follows_an_edit_is_kept_as_the_tree_grows(t: &mut tester): void {
|
|
# A longer comment above the first declaration moves every token, so all up
|
|
# to the edit is parsed again, and the nodes that adds move the tables the
|
|
# declarations after it are kept from, which are still read once it is done.
|
|
for count in 1..60 {
|
|
var before := string::from("# A file.\nimport std;\nmodule m;\n", nothing {});
|
|
var after := string::from("# A file, said at more length.\nimport std;\nmodule m;\n", nothing {});
|
|
for i in 0..count {
|
|
var line := $"def f{i}(x: u32): u32 ";
|
|
before.append(line.view());
|
|
before.append("{ return x + 1; }\n");
|
|
after.append(line.view());
|
|
after.append(i == count / 2 ? "{ return x * 2; }\n" : "{ return x + 1; }\n");
|
|
}
|
|
t.expects(reparses_as_whole(before.view(), after.view()));
|
|
}
|
|
}
|
|
|
|
@test def type_arguments_closed_by_one_token_are_kept(t: &mut tester): void {
|
|
# A `>>` closing type arguments is the same tokens after the edit as before,
|
|
# so the declaration it is in is kept.
|
|
var made := parse("val a: x<y<i64>> = 1;\nval b := 2;\nval c := 3;");
|
|
val first := made.syntax.run_at(made.syntax.node_lefts.at(made.root), 0);
|
|
|
|
var fresh := lex("val a: x<y<i64>> = 1;\nval b := 2;\nval c := 30;");
|
|
made.root = made.syntax.reparse(&mut fresh.syntax, &fresh.names, made.root, &mut made.notes, &mut fresh.notes, 0);
|
|
t.expects(made.syntax.run_at(made.syntax.node_lefts.at(made.root), 0) == first);
|
|
t.expects(reparses_as_whole(
|
|
"val a: x<y<i64>> = 1;\nval b := 2;\nval c := 3;",
|
|
"val a: x<y<i64>> = 1;\nval b := 2;\nval c := 30;"));
|
|
}
|
|
|
|
@test def notes_keep_the_order_a_whole_parse_gives(t: &mut tester): void {
|
|
# Parser notes before, within and after what is parsed again, and a lexer note.
|
|
t.expects(reparses_as_whole(
|
|
"val a := ;\nval b := 2;\nval c := ;",
|
|
"val a := ;\nval b := 2 +;\nval c := 0x;"));
|
|
t.expects(reparses_as_whole(
|
|
"val a := ;\nval b := 2 +;\nval c := ;",
|
|
"val a := ;\nval b := 2;\nval c := ;"));
|
|
}
|
|
|
|
@test def notes_past_the_cap_parse_whole(t: &mut tester): void {
|
|
var before := string::new(nothing {});
|
|
var after := string::new(nothing {});
|
|
for i in 0..(parser::max_notes + 20) {
|
|
before.append(i == 50 ? "val b := 2;\n" : "val a := ;\n");
|
|
after.append(i == 50 ? "val b := ;\n" : "val a := ;\n");
|
|
}
|
|
t.expects(reparses_as_whole(before.view(), after.view()));
|
|
}
|
|
|
|
@test def edits_over_time_do_not_keep_growing(t: &mut tester): void {
|
|
var made := parse("val a := 1;\nval b := 2;\nval c := 3;");
|
|
val whole := made.syntax.node_count;
|
|
for i in 0..50 {
|
|
var fresh := lex(i % 2 == 0 ? "val a := 1;\nval b := 2 + 3 * 4;\nval c := 3;" : "val a := 1;\nval b := 2;\nval c := 3;");
|
|
made.root = made.syntax.reparse(&mut fresh.syntax, &fresh.names, made.root, &mut made.notes, &mut fresh.notes, 0);
|
|
}
|
|
t.expects(made.syntax.node_count <= made.syntax.whole_nodes * 2);
|
|
t.expects(made.syntax.whole_nodes <= whole * 2);
|
|
}
|