test: update test runner code to lookup spans

This commit is contained in:
Tobias Schmitz 2025-05-20 14:26:42 +02:00
parent fc9697fd46
commit 65e72a39e0
No known key found for this signature in database
4 changed files with 23 additions and 12 deletions

View File

@ -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<String> {
self.slot(id, |slot| {

View File

@ -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<S>(Arc<Repr<S>>);

View File

@ -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();
};

View File

@ -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<String> {
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.