Skip to content

Commit

Permalink
feat: add tests for inlay hints (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Mar 11, 2024
1 parent 9823dfa commit 47a9b88
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/tinymist-query/src/fixtures/inlay_hints/base.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#text("")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#context
15 changes: 15 additions & 0 deletions crates/tinymist-query/src/fixtures/inlay_hints/[email protected]
Original file line number Diff line number Diff line change
@@ -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
}
}
]
Original file line number Diff line number Diff line change
@@ -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
---
[]
25 changes: 25 additions & 0 deletions crates/tinymist-query/src/inlay_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,28 @@ fn analyze_closure_signature(c: Arc<LazyHash<Closure>>) -> Vec<Arc<ParamSpec>> {

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));
});
}
}
20 changes: 16 additions & 4 deletions crates/tinymist-query/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 47a9b88

Please sign in to comment.