From 65e72a39e0dea7cf09804c8783fef18575d8ceaa Mon Sep 17 00:00:00 2001 From: Tobias Schmitz Date: Tue, 20 May 2025 14:26:42 +0200 Subject: [PATCH] test: update test runner code to lookup spans --- crates/typst-cli/src/world.rs | 2 +- crates/typst-syntax/src/lines.rs | 2 +- tests/src/run.rs | 14 ++++---------- tests/src/world.rs | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs index be3526ad2..e7ba0dcf1 100644 --- a/crates/typst-cli/src/world.rs +++ b/crates/typst-cli/src/world.rs @@ -181,7 +181,7 @@ impl SystemWorld { } } - /// Lookup a source file by id. + /// Lookup line metadata for a file by id. #[track_caller] pub fn lookup(&self, id: FileId) -> Lines { self.slot(id, |slot| { diff --git a/crates/typst-syntax/src/lines.rs b/crates/typst-syntax/src/lines.rs index 99275ad28..6604339dd 100644 --- a/crates/typst-syntax/src/lines.rs +++ b/crates/typst-syntax/src/lines.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use crate::is_newline; -/// Metadata about lines. +/// A text buffer and metadata about lines. #[derive(Clone)] pub struct Lines(Arc>); diff --git a/tests/src/run.rs b/tests/src/run.rs index 2f35c16f4..f2e83e067 100644 --- a/tests/src/run.rs +++ b/tests/src/run.rs @@ -10,7 +10,7 @@ use typst::layout::{Abs, Frame, FrameItem, PagedDocument, Transform}; use typst::visualize::Color; use typst::{Document, World, WorldExt}; use typst_pdf::PdfOptions; -use typst_syntax::{FileId, Lines}; +use typst_syntax::FileId; use crate::collect::{Attr, FileSize, NoteKind, Test}; use crate::logger::TestResult; @@ -326,15 +326,9 @@ impl<'a> Runner<'a> { /// Display a position as a line:column pair. fn format_pos(&self, file: FileId, pos: usize) -> String { - let res = if file != self.test.source.id() { - let bytes = self.world.file(file).unwrap(); - let lines = Lines::from_bytes(&bytes).unwrap(); - lines.byte_to_line_column(pos).map(|(line, col)| (line + 1, col + 1)) - } else { - (self.test.source.lines()) - .byte_to_line_column(pos) - .map(|(line, col)| (line + 1, col + 1)) - }; + let lines = self.world.lookup(file); + + let res = lines.byte_to_line_column(pos).map(|(line, col)| (line + 1, col + 1)); let Some((line, col)) = res else { return "oob".into(); }; diff --git a/tests/src/world.rs b/tests/src/world.rs index 4ed26e977..c37d21dee 100644 --- a/tests/src/world.rs +++ b/tests/src/world.rs @@ -20,6 +20,7 @@ use typst::text::{Font, FontBook, TextElem, TextSize}; use typst::utils::{singleton, LazyHash}; use typst::visualize::Color; use typst::{Feature, Library, World}; +use typst_syntax::Lines; /// A world that provides access to the tests environment. #[derive(Clone)] @@ -84,6 +85,22 @@ impl TestWorld { let mut map = self.base.slots.lock(); f(map.entry(id).or_insert_with(|| FileSlot::new(id))) } + + /// Lookup line metadata for a file by id. + #[track_caller] + pub fn lookup(&self, id: FileId) -> Lines { + self.slot(id, |slot| { + if let Some(source) = slot.source.get() { + let source = source.as_ref().expect("file is not valid"); + source.lines() + } else if let Some(bytes) = slot.file.get() { + let bytes = bytes.as_ref().expect("file is not valid"); + Lines::from_bytes(bytes.as_slice()).expect("file is not valid utf-8") + } else { + panic!("file id does not point to any source file"); + } + }) + } } /// Shared foundation of all test worlds.