Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/sys_inputs.rs

6.7 KiB, 1 run

created by r1870400018:74537, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! `sys.inputs` is the dictionary of strings a host gives a compile, as Typst's `--input key=value` gives
2//! it (D-20261018-03). A [`Session`] carries the inputs; each compile's library presents them, so two compiles
3//! with different inputs each see their own, and a change of inputs moves the caches' configuration. Each
4//! source asserts what it should see, and a failed assertion fails the compile.
5
6use oxedyne_fe2o3_austenite::caches;
7use oxedyne_fe2o3_austenite::compile::{
8 self,
9 Session,
10};
11use oxedyne_fe2o3_austenite::emit::sinks::PdfSink;
12use oxedyne_fe2o3_austenite::flow::text::FontStore;
13use oxedyne_fe2o3_austenite::watch;
14
15use std::path::PathBuf;
16
17fn dir(name: &str) -> PathBuf {
18 let d = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("sys_inputs").join(name);
19 let _ = std::fs::remove_dir_all(&d);
20 std::fs::create_dir_all(&d).expect("the case's directory");
21 std::fs::canonicalize(&d).expect("the case's canonical path")
22}
23
24fn pairs(kv: &[(&str, &str)]) -> Vec<(String, String)> {
25 kv.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect()
26}
27
28// Compiles `src` under `session`, giving the error messages the compile recorded, empty when it laid cleanly.
29fn errors(session: &mut Session, name: &str, src: &str) -> Vec<String> {
30 let d = dir(name);
31 let main = d.join("main.typ");
32 std::fs::write(&main, src).expect("the case's source");
33 let mut sink = PdfSink::new().expect("a PDF sink");
34 let done = session.compile(&main, &d, &mut sink, None, None, true).expect("the main is readable");
35 let mut out: Vec<String> = done.engine.diags.iter().filter(|d| d.is_error()).map(|d| d.message.clone()).collect();
36 if let Err(e) = &done.laid {
37 out.push(e.plain());
38 }
39 out
40}
41
42fn with(kv: &[(&str, &str)]) -> Session {
43 let mut s = Session::new(FontStore::default());
44 s.set_inputs(pairs(kv));
45 s
46}
47
48#[test]
49fn no_input_gives_an_empty_dictionary() {
50 let mut s = Session::new(FontStore::default());
51 let e = errors(&mut s, "none", "#assert.eq(type(sys.inputs), dictionary)\n#assert.eq(sys.inputs, (:))\n\
52 #assert.eq(sys.inputs.len(), 0)\nText.\n");
53 assert!(e.is_empty(), "{:?}", e);
54}
55
56#[test]
57fn one_input_is_a_string_under_its_key() {
58 let mut s = with(&[("engine", "austenite")]);
59 let e = errors(&mut s, "one", "#assert.eq(sys.inputs, (engine: \"austenite\"))\n\
60 #assert.eq(type(sys.inputs.engine), str)\nText.\n");
61 assert!(e.is_empty(), "{:?}", e);
62}
63
64#[test]
65fn several_inputs_keep_their_order_and_a_repeated_key_its_last_value() {
66 // As Typst's dictionary collects them: the first place a key took, the last value given it.
67 let mut s = with(&[("a", "1"), ("b", "2"), ("a", "3")]);
68 let e = errors(&mut s, "several", "#assert.eq(sys.inputs.keys(), (\"a\", \"b\"))\n\
69 #assert.eq(sys.inputs.values(), (\"3\", \"2\"))\n#assert.eq(repr(sys.inputs), \"(a: \\\"3\\\", b: \\\"2\\\")\")\nText.\n");
70 assert!(e.is_empty(), "{:?}", e);
71}
72
73#[test]
74fn at_with_a_default_reads_a_given_key_and_falls_back_for_a_missing_one() {
75 let src = "#let e = sys.inputs.at(\"engine\", default: none)\n#assert.eq(e, EXPECT)\nText.\n";
76 let mut s = with(&[("engine", "austenite")]);
77 let e = errors(&mut s, "at_given", &src.replace("EXPECT", "\"austenite\""));
78 assert!(e.is_empty(), "{:?}", e);
79 let mut s = Session::new(FontStore::default());
80 let e = errors(&mut s, "at_missing", &src.replace("EXPECT", "none"));
81 assert!(e.is_empty(), "{:?}", e);
82}
83
84#[test]
85fn a_wrong_expectation_fails_the_compile() {
86 // The control: a success above means the document saw what it asserted.
87 let mut s = with(&[("engine", "typst")]);
88 let e = errors(&mut s, "control", "#assert.eq(sys.inputs.engine, \"austenite\")\nText.\n");
89 assert!(!e.is_empty(), "a document that sees another engine fails");
90}
91
92#[test]
93fn sys_version_is_unchanged_by_inputs() {
94 let mut s = with(&[("engine", "austenite")]);
95 let e = errors(&mut s, "version", "#assert.eq(sys.version, version(0, 15, 1))\nText.\n");
96 assert!(e.is_empty(), "{:?}", e);
97}
98
99#[test]
100fn a_session_sees_its_new_inputs_and_they_move_the_configuration() {
101 let src = "#assert.eq(sys.inputs.at(\"engine\", default: none), EXPECT)\nText.\n";
102 let mut s = with(&[("engine", "typst")]);
103 assert!(errors(&mut s, "first", &src.replace("EXPECT", "\"typst\"")).is_empty());
104 let first = s.caches().config().expect("a compile sets the configuration");
105 let faces = s.fonts().clone().book().expect("a book").fingerprint();
106 assert_eq!(first, caches::configuration(faces, s.inputs(), compile::engine_git_hash()));
107 s.set_inputs(pairs(&[("engine", "austenite")]));
108 assert!(errors(&mut s, "second", &src.replace("EXPECT", "\"austenite\"")).is_empty());
109 assert_ne!(s.caches().config(), Some(first), "new inputs move the configuration");
110 // A fresh session in the same thread is not left the last session's inputs.
111 let mut s = Session::new(FontStore::default());
112 assert!(errors(&mut s, "third", &src.replace("EXPECT", "none")).is_empty());
113}
114
115#[test]
116fn an_input_argument_splits_on_its_first_equals_sign_as_typst_does() {
117 let ok = |a: &str| compile::parse_input(a).expect("a well-formed input");
118 assert_eq!(ok("engine=austenite"), ("engine".to_string(), "austenite".to_string()));
119 assert_eq!(ok("k=v=w"), ("k".to_string(), "v=w".to_string()));
120 assert_eq!(ok(" k = v=w "), ("k".to_string(), "v=w".to_string()));
121 assert_eq!(ok("k="), ("k".to_string(), String::new()));
122 for (arg, why) in [("=x", "key"), (" =x", "key"), ("noeq", "equals sign"), ("", "equals sign")] {
123 let m = match compile::parse_input(arg) {
124 Ok(p) => panic!("{:?} parsed as {:?}", arg, p),
125 Err(e) => e.plain(),
126 };
127 assert!(m.contains("--input") && m.contains(why) && m.contains(&format!("'{}'", arg)), "{:?}: {}", arg, m);
128 }
129}
130
131#[test]
132fn watch_compiles_with_its_inputs() {
133 let d = dir("watch");
134 let main = d.join("main.typ");
135 std::fs::write(&main, "#assert.eq(sys.inputs.engine, \"austenite\")\nText.\n").expect("the source");
136 let sets = vec!["view.open=false".to_string()];
137 let mut run = watch::Run::new(Some(main.clone()), sets, pairs(&[("engine", "austenite")]), true, None)
138 .expect("a watch run");
139 match run.tick().expect("a tick") {
140 watch::Tick::Built(_) => (),
141 other => panic!("the watch did not build: {:?}", other),
142 }
143 // `austenite SOURCE --watch`, the run with no settings file.
144 let bare = watch::Bare {
145 source: main.clone(),
146 out_dir: d.join("bare"),
147 root: None,
148 fonts: Vec::new(),
149 strict: false,
150 diag_summary: false,
151 timings: None,
152 timings_fine: false,
153 inputs: pairs(&[("engine", "austenite")]),
154 };
155 let mut run = watch::Run::bare(&d, &bare).expect("a bare watch run");
156 match run.tick().expect("a tick") {
157 watch::Tick::Built(_) => (),
158 other => panic!("the bare watch did not build: {:?}", other),
159 }
160 let (_, report) = watch::build(Some(&main), &["view.open=false".to_string()], &pairs(&[("engine", "austenite")]), None)
161 .expect("a build with its inputs");
162 assert!(report.pages > 0);
163}