oxedyne/fe2o3/fe2o3_austenite/tests/eval_contract.rs
3.9 KiB, 13 runs
created by r1870400018:63576, 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 | //! U0's contract checks: the shared tables the parallel units build on are self-consistent. |
| 2 | |
| 3 | use oxedyne_fe2o3_austenite::eval::args::Args; |
| 4 | use oxedyne_fe2o3_austenite::eval::content::{ |
| 5 | construct, |
| 6 | Content, |
| 7 | ElemKind, |
| 8 | FieldId, |
| 9 | }; |
| 10 | use oxedyne_fe2o3_austenite::eval::lib::library; |
| 11 | use oxedyne_fe2o3_austenite::eval::locate::Locator; |
| 12 | use oxedyne_fe2o3_austenite::eval::value::{ |
| 13 | Dict, |
| 14 | Value, |
| 15 | }; |
| 16 | use oxedyne_fe2o3_austenite::eval::{ |
| 17 | Engine, |
| 18 | World, |
| 19 | }; |
| 20 | use oxedyne_fe2o3_austenite::ledger::{ |
| 21 | AnchorId, |
| 22 | AnchorKind, |
| 23 | }; |
| 24 | use oxedyne_fe2o3_austenite::syntax::Span; |
| 25 | |
| 26 | use oxedyne_fe2o3_core::prelude::*; |
| 27 | use oxedyne_fe2o3_hash::fingerprint::Fingerprint; |
| 28 | use oxedyne_fe2o3_jdat::prelude::*; |
| 29 | |
| 30 | use std::collections::HashSet; |
| 31 | use std::path::PathBuf; |
| 32 | |
| 33 | #[test] |
| 34 | fn element_paths_are_unique_and_scoped() { |
| 35 | let mut seen = HashSet::new(); |
| 36 | for k in ElemKind::ALL { |
| 37 | assert!(seen.insert(k.path()), "duplicate element path {}", k.path()); |
| 38 | } |
| 39 | assert_eq!(ElemKind::List.scoped("item"), Some(ElemKind::ListItem)); |
| 40 | assert_eq!(ElemKind::Table.scoped("cell"), Some(ElemKind::TableCell)); |
| 41 | assert_eq!(ElemKind::Heading.scoped("item"), None); |
| 42 | assert_eq!(ElemKind::ListItem.name(), "item"); |
| 43 | } |
| 44 | |
| 45 | // What `#context query(<elem>)` does under typst 0.15.1 for the elements whose flag changed from U0's |
| 46 | // first table: true where it answers, false where it says "<elem> is not locatable". |
| 47 | const QUERYABLE_IN_TYPST: [(ElemKind, bool); 16] = [ |
| 48 | (ElemKind::Par, true), |
| 49 | (ElemKind::ParLine, true), |
| 50 | (ElemKind::Strong, true), |
| 51 | (ElemKind::Raw, true), |
| 52 | (ElemKind::List, true), |
| 53 | (ElemKind::ListItem, false), |
| 54 | (ElemKind::Link, true), |
| 55 | (ElemKind::FigureCaption, true), |
| 56 | (ElemKind::Underline, true), |
| 57 | (ElemKind::Table, true), |
| 58 | (ElemKind::TableCell, false), |
| 59 | (ElemKind::Image, true), |
| 60 | (ElemKind::Place, false), |
| 61 | (ElemKind::Divider, false), |
| 62 | (ElemKind::Text, false), |
| 63 | (ElemKind::Block, false), |
| 64 | ]; |
| 65 | |
| 66 | #[test] |
| 67 | fn queryable_elements_match_typst_and_are_located() { |
| 68 | for (kind, want) in QUERYABLE_IN_TYPST { |
| 69 | assert_eq!(kind.queryable(), want, "query({}) under typst 0.15.1", kind.path()); |
| 70 | } |
| 71 | for k in ElemKind::ALL { |
| 72 | assert!(!k.queryable() || k.locatable(), "{} is queryable but never located", k.path()); |
| 73 | } |
| 74 | // Located for float bookkeeping, as Typst's `PlaceElem` is, though no query may name it. |
| 75 | assert!(ElemKind::Place.locatable()); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn library_defines_global_elements_only() { |
| 80 | let lib = library(); |
| 81 | assert!(matches!(lib.get("heading"), Some(Value::Func(_)))); |
| 82 | assert!(lib.get("item").is_none()); |
| 83 | assert!(lib.get("frac").is_none()); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn text_constructs_through_the_generic_schema_walk() -> Outcome<()> { |
| 88 | let mut engine = Engine::new(World::new(PathBuf::from("/"))); |
| 89 | let mut args = Args::new(Span::detached()); |
| 90 | args.push(Span::detached(), Value::str("hello")); |
| 91 | let c = res!(construct(&mut engine, ElemKind::Text, &mut args)); |
| 92 | assert_eq!(c.plain_text(), "hello"); |
| 93 | assert!(matches!(c.get(FieldId(0)), Some(Value::Str(_)))); |
| 94 | let seq = Content::sequence(vec![Content::text("a"), Content::marker(ElemKind::Space, Span::detached()), c]); |
| 95 | assert_eq!(seq.plain_text(), "a hello"); |
| 96 | Ok(()) |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn dict_keeps_insertion_order() { |
| 101 | let mut d = Dict::new(); |
| 102 | d.insert("b", Value::Int(1)); |
| 103 | d.insert("a", Value::Int(2)); |
| 104 | d.insert("b", Value::Int(3)); |
| 105 | assert_eq!(d.keys().collect::<Vec<_>>(), vec!["b", "a"]); |
| 106 | assert!(matches!(d.remove("b"), Some(Value::Int(3)))); |
| 107 | assert!(matches!(d.get("a"), Some(Value::Int(2)))); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn locations_are_stable_and_distinct() { |
| 112 | let fp = Fingerprint::of(b"a heading"); |
| 113 | let mut a = Locator::default(); |
| 114 | let mut b = Locator::default(); |
| 115 | let a1 = a.locate(ElemKind::Heading, fp); |
| 116 | let a2 = a.locate(ElemKind::Heading, fp); |
| 117 | assert_ne!(a1, a2); |
| 118 | assert_eq!(a1, b.locate(ElemKind::Heading, fp)); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn location_anchor_round_trips_the_ledger_encoding() -> Outcome<()> { |
| 123 | let id = AnchorId::new(AnchorKind::Location, "00000000deadbeef"); |
| 124 | let back = res!(AnchorId::from_dat(res!(id.to_dat()))); |
| 125 | assert_eq!(back, id); |
| 126 | assert_eq!(back.kind.name(), "location"); |
| 127 | Ok(()) |
| 128 | } |