//! Tests of the language server: feed it editor messages, read what it answers. use rex::json::{self, Json}; use rex::lsp; fn frame(v: &Json) -> Vec { let body = v.to_string(); format!("Content-Length: {}\r\n\r\n{}", body.len(), body).into_bytes() } fn request(id: i64, method: &str, params: Json) -> Json { Json::obj(vec![("jsonrpc", Json::str("2.0")), ("id", Json::Num(id as f64)), ("method", Json::str(method)), ("params", params)]) } fn notify(method: &str, params: Json) -> Json { Json::obj(vec![("jsonrpc", Json::str("2.0")), ("method", Json::str(method)), ("params", params)]) } fn open(uri: &str, text: &str) -> Json { notify( "textDocument/didOpen", Json::obj(vec![("textDocument", Json::obj(vec![("uri", Json::str(uri)), ("text", Json::str(text))]))]), ) } /// Run a whole conversation and return every message the server sent. fn talk(messages: Vec) -> (Vec, i32) { let mut input = Vec::new(); for m in &messages { input.extend(frame(m)); } let mut output = Vec::new(); let code = lsp::run(std::io::Cursor::new(input), &mut output); let text = String::from_utf8(output).unwrap(); let mut out = Vec::new(); let mut rest = text.as_str(); while let Some(i) = rest.find("\r\n\r\n") { let len: usize = rest[..i].trim().strip_prefix("Content-Length: ").unwrap().parse().unwrap(); out.push(json::parse(&rest[i + 4..i + 4 + len]).unwrap()); rest = &rest[i + 4 + len..]; } (out, code) } fn exit_messages() -> Vec { vec![request(99, "shutdown", Json::Null), notify("exit", Json::Null)] } #[test] fn json_round_trip() { let src = r#"{"a":[1,2.5,"x\n\"y\" é 😀"],"b":null,"c":true}"#; let v = json::parse(src).unwrap(); assert_eq!(json::parse(&v.to_string()).unwrap(), v); assert_eq!(v.get("a"), Some(&Json::Arr(vec![Json::Num(1.0), Json::Num(2.5), Json::str("x\n\"y\" é 😀")]))); } #[test] fn initialize_announces_features() { let mut msgs = vec![request(1, "initialize", Json::obj(vec![]))]; msgs.extend(exit_messages()); let (out, code) = talk(msgs); assert_eq!(code, 0); let caps = out[0].path(&["result", "capabilities"]).unwrap(); assert_eq!(caps.get("documentFormattingProvider"), Some(&Json::Bool(true))); assert_eq!(out[0].get("id"), Some(&Json::Num(1.0))); } #[test] fn errors_show_up_as_diagnostics() { let src = "func main() {\n var x: int = \"no\"\n}\n"; let mut msgs = vec![open("file:///tmp/rex-lsp-test/a.rex", src)]; msgs.extend(exit_messages()); let (out, _) = talk(msgs); let d = match out[0].path(&["params", "diagnostics"]) { Some(Json::Arr(items)) => items.clone(), other => panic!("{other:?}"), }; assert_eq!(d.len(), 1); let line = d[0].path(&["range", "start", "line"]).and_then(Json::as_i64).unwrap(); assert_eq!(line, 1); assert!(d[0].get("message").and_then(Json::as_str).unwrap().contains("string"), "{:?}", d[0]); } #[test] fn a_module_without_main_has_no_errors() { // a file that other files import doesn't need `main`; its other errors still show let module = "pub func double(n: int): int {\n return n * 2\n}\n"; let broken = "pub func double(n: int): int {\n return \"no\"\n}\n"; let mut msgs = vec![open("file:///tmp/rex-lsp-test/m.rex", module), open("file:///tmp/rex-lsp-test/n.rex", broken)]; msgs.extend(exit_messages()); let (out, _) = talk(msgs); assert_eq!(out[0].path(&["params", "diagnostics"]), Some(&Json::Arr(vec![])), "{:?}", out[0]); assert_eq!(out[1].path(&["params", "diagnostics"]).map(|d| matches!(d, Json::Arr(a) if a.len() == 1)), Some(true)); } #[test] fn fixing_the_code_clears_the_diagnostics() { let uri = "file:///tmp/rex-lsp-test/b.rex"; let change = notify( "textDocument/didChange", Json::obj(vec![ ("textDocument", Json::obj(vec![("uri", Json::str(uri))])), ("contentChanges", Json::Arr(vec![Json::obj(vec![("text", Json::str("func main() {\n print(1)\n}\n"))])])), ]), ); let mut msgs = vec![open(uri, "func main() {\n print(nope)\n}\n"), change]; msgs.extend(exit_messages()); let (out, _) = talk(msgs); assert_eq!(out[0].path(&["params", "diagnostics"]).map(|d| matches!(d, Json::Arr(a) if a.len() == 1)), Some(true)); assert_eq!(out[1].path(&["params", "diagnostics"]), Some(&Json::Arr(vec![]))); } #[test] fn error_columns_count_utf16_units() { // the emoji takes two units in the editor's counting let src = "func main() {\n print(\"😀\", nope)\n}\n"; let mut msgs = vec![open("file:///tmp/rex-lsp-test/c.rex", src)]; msgs.extend(exit_messages()); let (out, _) = talk(msgs); let d = match out[0].path(&["params", "diagnostics"]) { Some(Json::Arr(items)) => items[0].clone(), other => panic!("{other:?}"), }; let start = d.path(&["range", "start", "character"]).and_then(Json::as_i64).unwrap(); let end = d.path(&["range", "end", "character"]).and_then(Json::as_i64).unwrap(); // ` print("😀", ` is 4 + 6 + 4 + 2 = 16 units before `nope` assert_eq!((start, end), (16, 20)); } #[test] fn formatting_returns_the_tidy_text() { let uri = "file:///tmp/rex-lsp-test/d.rex"; let fmt_req = request( 2, "textDocument/formatting", Json::obj(vec![("textDocument", Json::obj(vec![("uri", Json::str(uri))]))]), ); let mut msgs = vec![open(uri, "func main(){\nprint( 1 )\n}\n"), fmt_req]; msgs.extend(exit_messages()); let (out, _) = talk(msgs); let reply = out.iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap(); let edit = match reply.get("result") { Some(Json::Arr(e)) => e[0].clone(), other => panic!("{other:?}"), }; assert_eq!(edit.get("newText").and_then(Json::as_str), Some("func main() {\n print(1)\n}\n")); } #[test] fn imports_are_read_next_to_the_file() { let dir = std::env::temp_dir().join(format!("rex-lsp-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); std::fs::write(dir.join("util.rex"), "pub func twice(x: int): int { return x * 2 }\n").unwrap(); let uri = format!("file://{}/main.rex", dir.display()); let src = "import util\nfunc main() {\n print(util.twice(2))\n}\n"; let mut msgs = vec![open(&uri, src)]; msgs.extend(exit_messages()); let (out, _) = talk(msgs); let _ = std::fs::remove_dir_all(&dir); assert_eq!(out[0].path(&["params", "diagnostics"]), Some(&Json::Arr(vec![])), "{:?}", out[0]); } #[test] fn packages_are_seen_by_the_editor() { let dir = std::env::temp_dir().join(format!("rex-lsp-pkg-{}", std::process::id())); std::fs::create_dir_all(dir.join("app")).unwrap(); std::fs::create_dir_all(dir.join("mylib")).unwrap(); std::fs::write(dir.join("app/rex.toml"), "[deps]\nmylib = \"../mylib\"\n").unwrap(); std::fs::write(dir.join("mylib/mylib.rex"), "pub func n(): int { return 1 }\n").unwrap(); let uri = format!("file://{}/app/main.rex", dir.display()); let good = "import mylib\nfunc main() {\n print(mylib.n())\n}\n"; let bad = "import mylib\nfunc main() {\n print(mylib.n(5))\n}\n"; let mut msgs = vec![request(1, "initialize", Json::obj(vec![])), open(&uri, good), request(2, "textDocument/definition", at(&uri, 2, 17))]; let other = format!("file://{}/app/other.rex", dir.display()); msgs.push(open(&other, bad)); msgs.extend(exit_messages()); let (out, _) = talk(msgs); let _ = std::fs::remove_dir_all(&dir); let diags: Vec<&Json> = out.iter().filter_map(|m| m.path(&["params", "diagnostics"])).collect(); assert_eq!(diags[0], &Json::Arr(vec![]), "{out:?}"); assert!(matches!(diags[1], Json::Arr(a) if a.len() == 1), "{out:?}"); let def = out.iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap(); let target = def.path(&["result", "uri"]).map(|u| u.to_string()).unwrap_or_default(); assert!(target.contains("mylib/mylib.rex"), "{def:?}"); } #[test] fn unknown_requests_get_an_error() { let mut msgs = vec![request(5, "textDocument/codeLens", Json::obj(vec![]))]; msgs.extend(exit_messages()); let (out, _) = talk(msgs); assert!(out[0].path(&["error", "message"]).is_some()); } #[test] fn editor_files_are_valid_json() { let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("editors/vscode"); for f in ["package.json", "language-configuration.json", "syntaxes/rex.tmLanguage.json"] { let text = std::fs::read_to_string(dir.join(f)).unwrap(); json::parse(&text).unwrap_or_else(|e| panic!("{f}: {e}")); } } // ---------- code intelligence ---------- const SRC: &str = "// Adds two numbers.\nfunc add(a: int, b: int): int {\n var sum = a + b\n return sum\n}\n\nstruct P {\n x: int\n}\n\nfunc main() {\n var t = add(1, 2)\n print(t)\n}\n"; fn at(uri: &str, line: i64, ch: i64) -> Json { Json::obj(vec![ ("textDocument", Json::obj(vec![("uri", Json::str(uri))])), ("position", Json::obj(vec![("line", Json::Num(line as f64)), ("character", Json::Num(ch as f64))])), ]) } fn ask(method: &str, params: Json) -> Json { let mut msgs = vec![request(1, "initialize", Json::obj(vec![])), open("file:///x/a.rex", SRC), request(2, method, params)]; msgs.extend(exit_messages()); let (out, _) = talk(msgs); out.into_iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap().get("result").cloned().unwrap_or(Json::Null) } #[test] fn outline_lists_top_level_names() { let r = ask("textDocument/documentSymbol", Json::obj(vec![("textDocument", Json::obj(vec![("uri", Json::str("file:///x/a.rex"))]))])); let text = r.to_string(); assert!(text.contains("\"add\"") && text.contains("\"P\"") && text.contains("\"main\"") && text.contains("\"x\""), "{text}"); } #[test] fn definition_jumps_to_the_function() { let r = ask("textDocument/definition", at("file:///x/a.rex", 11, 14)); assert_eq!(r.path(&["range", "start", "line"]).and_then(Json::as_i64), Some(1), "{r:?}"); } #[test] fn definition_finds_a_local() { let r = ask("textDocument/definition", at("file:///x/a.rex", 3, 12)); assert_eq!(r.path(&["range", "start", "line"]).and_then(Json::as_i64), Some(2), "{r:?}"); } #[test] fn hover_shows_the_signature_and_comment() { let r = ask("textDocument/hover", at("file:///x/a.rex", 11, 14)); let v = r.path(&["contents", "value"]).and_then(Json::as_str).unwrap_or("").to_string(); assert!(v.contains("func add(a: int, b: int): int") && v.contains("Adds two numbers."), "{v}"); } #[test] fn completion_offers_names_and_keywords() { let r = ask("textDocument/completion", at("file:///x/a.rex", 12, 4)).to_string(); assert!(r.contains("\"add\"") && r.contains("\"t\"") && r.contains("\"func\"") && r.contains("\"string\""), "{r:?}"); } #[test] fn signature_help_marks_the_parameter() { let src = "func add(a: int, b: int): int {\n return a + b\n}\nfunc main() {\n add(1, \n}\n"; let mut msgs = vec![request(1, "initialize", Json::obj(vec![])), open("file:///x/b.rex", src), request(2, "textDocument/signatureHelp", at("file:///x/b.rex", 4, 11))]; msgs.extend(exit_messages()); let (out, _) = talk(msgs); let r = out.into_iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap().get("result").cloned().unwrap(); assert_eq!(r.get("activeParameter").and_then(Json::as_i64), Some(1), "{r:?}"); assert!(r.to_string().contains("add(a: int, b: int): int"), "{r:?}"); } // ---------- C headers ---------- fn with_header(src: &str, method: &str, line: i64, ch: i64) -> Json { let dir = std::env::temp_dir().join(format!("rex-lsp-c-{}-{}", std::process::id(), method.replace('/', "_"))); std::fs::create_dir_all(&dir).unwrap(); std::fs::write(dir.join("geo.h"), "typedef struct { double x, y; } Point;\n#define ORIGIN_NAME \"origin\"\n/* distance */\ndouble dist(Point a, Point b);\nint clamp(int v, int lo, int hi);\n").unwrap(); let uri = format!("file://{}/main.rex", dir.display()); let mut msgs = vec![request(1, "initialize", Json::obj(vec![])), open(&uri, src), request(2, method, at(&uri, line, ch))]; msgs.extend(exit_messages()); let (out, _) = talk(msgs); let _ = std::fs::remove_dir_all(&dir); out.into_iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap().get("result").cloned().unwrap_or(Json::Null) } #[test] fn completion_lists_what_a_c_header_has() { let r = with_header("import \"geo.h\"\n\nfunc main() {\n geo.\n}\n", "textDocument/completion", 3, 8).to_string(); assert!(r.contains("\"dist\"") && r.contains("\"Point\"") && r.contains("\"ORIGIN_NAME\"") && r.contains("dist(a: geo.Point, b: geo.Point): float"), "{r}"); } #[test] fn hover_and_hints_for_c_functions() { let src = "import \"geo.h\" { clamp }\n\nfunc main() {\n print(clamp(5, 0, \n}\n"; let h = with_header(src, "textDocument/hover", 3, 11); let v = h.path(&["contents", "value"]).and_then(Json::as_str).unwrap_or("").to_string(); assert!(v.contains("func clamp(v: i32, lo: i32, hi: i32): i32") && v.contains("geo.h"), "{v}"); let s = with_header(src, "textDocument/signatureHelp", 3, 22); assert_eq!(s.get("activeParameter").and_then(Json::as_i64), Some(2), "{s:?}"); } #[test] fn imports_from_a_folder_above_are_found() { let dir = std::env::temp_dir().join(format!("rex-lsp-up-{}", std::process::id())); std::fs::create_dir_all(dir.join("engine")).unwrap(); std::fs::create_dir_all(dir.join("game")).unwrap(); std::fs::write(dir.join("engine/engine.rex"), "pub func twice(x: int): int { return x * 2 }\n").unwrap(); let uri = format!("file://{}/game/main.rex", dir.display()); let src = "import ..engine.engine\nfunc main() {\n print(engine.twice(2))\n}\n"; // hover on `twice` reads the signature from the file above let mut msgs = vec![request(1, "initialize", Json::obj(vec![])), open(&uri, src)]; msgs.push(request(2, "textDocument/hover", at(&uri, 2, 17))); msgs.extend(exit_messages()); let (out, _) = talk(msgs); let _ = std::fs::remove_dir_all(&dir); let diags = out.iter().find(|m| m.get("method").is_some()).unwrap(); assert_eq!(diags.path(&["params", "diagnostics"]), Some(&Json::Arr(vec![])), "{diags:?}"); let hover = out.iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap(); assert!(format!("{hover:?}").contains("twice"), "{hover:?}"); } #[test] fn the_editor_counts_imports_from_the_project_root() { let dir = std::env::temp_dir().join(format!("rex-lsp-root-{}", std::process::id())); std::fs::create_dir_all(dir.join("engine")).unwrap(); std::fs::create_dir_all(dir.join("game/app")).unwrap(); std::fs::write(dir.join("rex.toml"), "").unwrap(); std::fs::write(dir.join("engine/engine.rex"), "import engine.util\npub func twice(x: int): int { return util.dbl(x) }\n").unwrap(); std::fs::write(dir.join("engine/util.rex"), "pub func dbl(x: int): int { return x * 2 }\n").unwrap(); let main = format!("file://{}/game/app/main.rex", dir.display()); let module = format!("file://{}/engine/engine.rex", dir.display()); let src = "import engine.engine\nfunc main() {\n print(engine.twice(2))\n}\n"; let mod_src = "import engine.util\npub func twice(x: int): int { return util.dbl(x) }\n"; // both the main file and a module of the project are checked with the same root let mut msgs = vec![open(&main, src), open(&module, mod_src)]; msgs.extend(exit_messages()); let (out, _) = talk(msgs); let _ = std::fs::remove_dir_all(&dir); assert_eq!(out[0].path(&["params", "diagnostics"]), Some(&Json::Arr(vec![])), "{:?}", out[0]); assert_eq!(out[1].path(&["params", "diagnostics"]), Some(&Json::Arr(vec![])), "{:?}", out[1]); }