Files
2026-11-19 04:54:17 -06:00

128 lines
5.2 KiB
Plaintext

#! @file server.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the tests of what the language server reads and writes of the
#! protocol itself: positions in a file whose lines end in CR LF, which its
#! source holds with the LF alone, and the path a `file:` URI names and the URI
#! of a path, as each system spells them.
import std::io;
import std::testing;
import sakura::analyzer;
module sakura::server;
@test def positions_in_text_with_cr_lf_are_those_of_its_lines(t: &mut tester): void {
var files := vector::<gathered>::new();
files.append(gathered {
path = string::from("a.sakura", nothing {}),
text = string::from("def f(): void {\r\n val label := \"日🌸\"; val x := 1;\r\n}\r\n", nothing {})
});
var roots := vector::<string>::new();
var documents := vector::<document>::new();
var held := program::parse(&files, false, false, "windows", &gathering { roots = &roots, documents = &documents });
val text := held.source_text(0);
t.expects(text.length + 3 == held.texts[0].length);
# `x` follows a character of three bytes and one of four, which is two units
# of UTF-16, and is a byte further into the text the editor has, past the CR
# ending the line before it.
val offset := locate(text, 1, 26);
t.expects(offset == 46 && text[46] == 'x');
t.expects(locate(held.texts[0].span, 1, 26) == 47);
val at := position_of(text, &held.sources[0].lines, offset);
t.expects(at.line == 1 && at.character == 26);
}
@test def messages_are_json_whatever_a_file_holds(t: &mut tester): void {
# Each byte that breaks UTF-8 is U+FFFD, and the rest is escaped as JSON is.
var made := message_writer();
made.begin_object();
made.member("message");
made.text("ok \xFF\"\n\xE2\x82");
made.member("range");
var starts := vector::<u32>::new();
starts.append(0);
starts.append(4);
write_range(&mut made, "abc\ndé🌸f", &starts, 5, 11);
made.end_object();
var out := string::new(nothing {});
append_written(&mut out, &mut made);
t.expects(out.span == "{\"message\":\"ok \\ufffd\\\"\\n\\ufffd\\ufffd\",\"range\":{\"start\":{\"line\":1,\"character\":1},\"end\":{\"line\":1,\"character\":4}}}");
}
# The path a URI names, or `none` where it names none.
def path_of(uri: strview): string {
var found := uri_path(uri);
if found is some(&mut named) {
return move(named);
}
return string::from("none", nothing {});
}
# The URI of a path.
def uri_of(path: strview): string {
var made := string::new(nothing {});
append_uri(&mut made, path);
return made;
}
when os == linux {
@test def uris_name_paths_as_linux_spells_them(t: &mut tester): void {
t.expects(path_of("file:///home/a%20b/").span == "/home/a b");
t.expects(path_of("file://localhost/x").span == "/x");
t.expects(path_of("untitled:Untitled-1").span == "none");
t.expects(uri_of("/home/a b").span == "file:///home/a%20b");
}
}
when os == windows {
# Text with the letter of a drive between two others.
def with_drive(before: strview, letter: u8, after: strview): string {
var made := string::from(before, nothing {});
made.push(letter);
made.append(after);
return made;
}
@test def uris_name_paths_as_windows_spells_them(t: &mut tester): void {
val here := io::working_directory() else (_) { t.expects(false); return; };
val whole := here.span;
return if whole.length < 2 || whole[1] != ':';
val letter: u8 = whole[0] >= 'a' ? whole[0] - 32 : whole[0];
val lower: u8 = letter + 32;
# Past a folder that is not there, the path is kept as it is given, made
# absolute, with the drive's letter upper case and no `/` at its end.
val named := path_of(with_drive("file:///", lower, "%3A/sakura-test-gone/a%20b.sakura").span);
t.expects(named.span == with_drive("", letter, ":/sakura-test-gone/a b.sakura").span);
val ended := path_of(with_drive("file:///", letter, ":/sakura-test-gone/").span);
t.expects(ended.span == with_drive("", letter, ":/sakura-test-gone").span);
val local := path_of(with_drive("file://localhost/", lower, ":/sakura-test-gone/x/./y/../z").span);
t.expects(local.span == with_drive("", letter, ":/sakura-test-gone/x/z").span);
val root := path_of(with_drive("file:///", lower, "%3A").span);
t.expects(root.span == with_drive("", letter, ":/").span);
t.expects(path_of("untitled:Untitled-1").span == "none");
# The working directory, spelled in upper case, is spelled as on disk.
var slashed := string::new(nothing {});
var shouted := string::new(nothing {});
for i in 0..whole.length {
val byte: u8 = whole[i] == '\\' ? '/' : whole[i];
slashed.push(byte);
shouted.push(byte >= 'a' && byte <= 'z' ? byte - 32 : byte);
}
val spelled := path_of(uri_of(shouted.span).span);
t.expects(spelled.span == path_of(uri_of(slashed.span).span).span);
t.expects(spelled.length != 0 && spelled[0] == letter && spelled[1] == ':');
}
@test def paths_are_named_by_uris_as_vs_code_spells_them(t: &mut tester): void {
t.expects(uri_of("C:/Users/a b/日.sakura").span == "file:///c%3A/Users/a%20b/%E6%97%A5.sakura");
t.expects(uri_of("C:/").span == "file:///c%3A/");
t.expects(uri_of("//server/share/x").span == "file://server/share/x");
}
}