From 47a9b88c90c0ec09472016b476c78d01b0029562 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Mon, 11 Mar 2024 20:54:19 +0800 Subject: [PATCH] feat: add tests for inlay hints (#8) --- .../src/fixtures/inlay_hints/base.typ | 1 + .../inlay_hints/imcomplete-expression.typ | 1 + .../fixtures/inlay_hints/test@base.typ.snap | 15 +++++++++++ .../test@imcomplete-expression.typ.snap | 6 +++++ crates/tinymist-query/src/inlay_hint.rs | 25 +++++++++++++++++++ crates/tinymist-query/src/lib.rs | 20 ++++++++++++--- 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 crates/tinymist-query/src/fixtures/inlay_hints/base.typ create mode 100644 crates/tinymist-query/src/fixtures/inlay_hints/imcomplete-expression.typ create mode 100644 crates/tinymist-query/src/fixtures/inlay_hints/test@base.typ.snap create mode 100644 crates/tinymist-query/src/fixtures/inlay_hints/test@imcomplete-expression.typ.snap diff --git a/crates/tinymist-query/src/fixtures/inlay_hints/base.typ b/crates/tinymist-query/src/fixtures/inlay_hints/base.typ new file mode 100644 index 000000000..516b0bff8 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/inlay_hints/base.typ @@ -0,0 +1 @@ +#text("") diff --git a/crates/tinymist-query/src/fixtures/inlay_hints/imcomplete-expression.typ b/crates/tinymist-query/src/fixtures/inlay_hints/imcomplete-expression.typ new file mode 100644 index 000000000..d26bef09f --- /dev/null +++ b/crates/tinymist-query/src/fixtures/inlay_hints/imcomplete-expression.typ @@ -0,0 +1 @@ +#context diff --git a/crates/tinymist-query/src/fixtures/inlay_hints/test@base.typ.snap b/crates/tinymist-query/src/fixtures/inlay_hints/test@base.typ.snap new file mode 100644 index 000000000..dcdde7866 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/inlay_hints/test@base.typ.snap @@ -0,0 +1,15 @@ +--- +source: crates/tinymist-query/src/inlay_hint.rs +expression: "JsonRepr::new_redacted(result, &REDACT_LOC)" +input_file: crates/tinymist-query/src/fixtures/inlay_hints/base.typ +--- +[ + { + "kind": 2, + "label": ": body", + "position": { + "character": 8, + "line": 0 + } + } +] diff --git a/crates/tinymist-query/src/fixtures/inlay_hints/test@imcomplete-expression.typ.snap b/crates/tinymist-query/src/fixtures/inlay_hints/test@imcomplete-expression.typ.snap new file mode 100644 index 000000000..1b89d0799 --- /dev/null +++ b/crates/tinymist-query/src/fixtures/inlay_hints/test@imcomplete-expression.typ.snap @@ -0,0 +1,6 @@ +--- +source: crates/tinymist-query/src/inlay_hint.rs +expression: "JsonRepr::new_redacted(result, &REDACT_LOC)" +input_file: crates/tinymist-query/src/fixtures/inlay_hints/imcomplete-expression.typ +--- +[] diff --git a/crates/tinymist-query/src/inlay_hint.rs b/crates/tinymist-query/src/inlay_hint.rs index 25ff2d222..79d511663 100644 --- a/crates/tinymist-query/src/inlay_hint.rs +++ b/crates/tinymist-query/src/inlay_hint.rs @@ -495,3 +495,28 @@ fn analyze_closure_signature(c: Arc>) -> Vec> { params } + +#[cfg(test)] +mod tests { + use super::*; + use crate::tests::*; + + #[test] + fn test() { + snapshot_testing("inlay_hints", &|world, path| { + let source = get_suitable_source_in_workspace(world, &path).unwrap(); + + let request = InlayHintRequest { + path: path.clone(), + range: typst_to_lsp::range( + 0..source.text().len(), + &source, + PositionEncoding::Utf16, + ), + }; + + let result = request.request(world, PositionEncoding::Utf16); + assert_snapshot!(JsonRepr::new_redacted(result, &REDACT_LOC)); + }); + } +} diff --git a/crates/tinymist-query/src/lib.rs b/crates/tinymist-query/src/lib.rs index 87e5b29f3..022478bc3 100644 --- a/crates/tinymist-query/src/lib.rs +++ b/crates/tinymist-query/src/lib.rs @@ -161,7 +161,10 @@ mod tests { use serde::Serialize; use serde_json::{ser::PrettyFormatter, Serializer, Value}; use typst::syntax::{LinkedNode, Source, VirtualPath}; - use typst_ts_compiler::{service::WorkspaceProvider, ShadowApi}; + use typst_ts_compiler::{ + service::{CompileDriver, Compiler, WorkspaceProvider}, + ShadowApi, + }; use typst_ts_core::{config::CompileOpts, Bytes, TypstFileId}; pub use insta::assert_snapshot; @@ -199,6 +202,9 @@ mod tests { .unwrap(); let sources = source.split("-----"); + let pw = root.join(Path::new("/main.typ")); + world.map_shadow(&pw, Bytes::from_static(b"")).unwrap(); + let mut last_pw = None; for (i, source) in sources.enumerate() { // find prelude @@ -220,18 +226,24 @@ mod tests { last_pw = Some(pw); } + world.set_main_id(TypstFileId::new(None, VirtualPath::new("/main.typ"))); + let mut driver = CompileDriver::new(world); + let _ = driver.compile(&mut Default::default()); + let pw = last_pw.unwrap(); - world.set_main_id(TypstFileId::new( + driver.world_mut().set_main_id(TypstFileId::new( None, VirtualPath::new(pw.strip_prefix(root).unwrap()), )); - f(&mut world, pw) + f(driver.world_mut(), pw) } pub fn find_test_position(s: &Source) -> LspPosition { let re = s.text().find("/* position */").map(|e| (e, true)); let re = re.or_else(|| s.text().find("/* position after */").zip(Some(false))); - let (re, prev) = re.unwrap(); + let (re, prev) = re + .ok_or_else(|| panic!("No position marker found in source:\n{}", s.text())) + .unwrap(); let n = LinkedNode::new(s.root()); let mut n = n.leaf_at(re + 1).unwrap();