feat: add tests for inlay hints (#8)

This commit is contained in:
Myriad-Dreamin 2024-03-11 20:54:19 +08:00 committed by GitHub
parent 9823dfa4f5
commit 47a9b88c90
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 64 additions and 4 deletions

View file

@ -0,0 +1 @@
#text("")

View file

@ -0,0 +1 @@
#context

View file

@ -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
}
}
]

View file

@ -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
---
[]

View file

@ -495,3 +495,28 @@ fn analyze_closure_signature(c: Arc<LazyHash<Closure>>) -> Vec<Arc<ParamSpec>> {
params 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));
});
}
}

View file

@ -161,7 +161,10 @@ mod tests {
use serde::Serialize; use serde::Serialize;
use serde_json::{ser::PrettyFormatter, Serializer, Value}; use serde_json::{ser::PrettyFormatter, Serializer, Value};
use typst::syntax::{LinkedNode, Source, VirtualPath}; 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}; use typst_ts_core::{config::CompileOpts, Bytes, TypstFileId};
pub use insta::assert_snapshot; pub use insta::assert_snapshot;
@ -199,6 +202,9 @@ mod tests {
.unwrap(); .unwrap();
let sources = source.split("-----"); 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; let mut last_pw = None;
for (i, source) in sources.enumerate() { for (i, source) in sources.enumerate() {
// find prelude // find prelude
@ -220,18 +226,24 @@ mod tests {
last_pw = Some(pw); 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(); let pw = last_pw.unwrap();
world.set_main_id(TypstFileId::new( driver.world_mut().set_main_id(TypstFileId::new(
None, None,
VirtualPath::new(pw.strip_prefix(root).unwrap()), VirtualPath::new(pw.strip_prefix(root).unwrap()),
)); ));
f(&mut world, pw) f(driver.world_mut(), pw)
} }
pub fn find_test_position(s: &Source) -> LspPosition { pub fn find_test_position(s: &Source) -> LspPosition {
let re = s.text().find("/* position */").map(|e| (e, true)); 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 = 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 n = LinkedNode::new(s.root());
let mut n = n.leaf_at(re + 1).unwrap(); let mut n = n.leaf_at(re + 1).unwrap();