typos/crates/varcon/tests/codegen.rs

87 lines
3.2 KiB
Rust
Raw Normal View History

2024-04-26 22:14:01 -04:00
#![allow(clippy::self_named_module_files)] // false positive
2022-08-01 15:45:58 -04:00
const DICT: &[u8] = include_bytes!("../assets/varcon.txt");
2020-04-07 20:50:06 -04:00
2022-08-01 15:45:58 -04:00
#[test]
fn codegen() {
let mut content = vec![];
generate(&mut content);
let content = String::from_utf8(content).unwrap();
let content = codegenrs::rustfmt(&content, None).unwrap();
2024-05-27 23:09:20 -04:00
snapbox::assert_data_eq!(content, snapbox::file!["../src/codegen.rs"].raw());
2022-08-01 15:45:58 -04:00
}
2020-04-07 20:50:06 -04:00
fn generate<W: std::io::Write>(file: &mut W) {
let dict = String::from_utf8_lossy(DICT);
2020-05-27 21:51:04 -04:00
let clusters = varcon_core::ClusterIter::new(&dict);
2020-04-07 20:50:06 -04:00
writeln!(
file,
2022-08-01 15:45:58 -04:00
"// This file is @generated by {}",
file!().replace('\\', "/")
2020-04-07 20:50:06 -04:00
)
.unwrap();
writeln!(file, "#![allow(clippy::unreadable_literal)]",).unwrap();
writeln!(file).unwrap();
2024-04-26 22:14:01 -04:00
writeln!(
file,
"use crate::{{Category, Cluster, Entry, Pos, Tag, Type, Variant}};"
)
.unwrap();
2020-04-07 20:50:06 -04:00
writeln!(file).unwrap();
2020-07-04 21:41:32 -04:00
writeln!(file, "pub static VARCON: &[Cluster] = &[").unwrap();
2020-04-07 20:50:06 -04:00
for mut cluster in clusters {
cluster.infer();
writeln!(file, "Cluster {{").unwrap();
writeln!(file, " header: {:?},", cluster.header).unwrap();
writeln!(file, " entries: &[").unwrap();
for entry in &cluster.entries {
writeln!(file, " Entry {{").unwrap();
writeln!(file, " variants: &[").unwrap();
for variant in &entry.variants {
writeln!(file, " Variant {{").unwrap();
writeln!(file, " word: {:?},", variant.word).unwrap();
writeln!(file, " types: &[").unwrap();
for t in &variant.types {
write!(file, " Type {{").unwrap();
write!(file, "category: Category::{:?}, ", t.category).unwrap();
if let Some(tag) = t.tag {
2024-07-26 17:08:02 -04:00
write!(file, "tag: Some(Tag::{tag:?}), ").unwrap();
2020-04-07 20:50:06 -04:00
} else {
write!(file, "tag: {:?}, ", t.tag).unwrap();
}
write!(file, "num: {:?},", t.num).unwrap();
writeln!(file, "}},").unwrap();
}
writeln!(file, " ],").unwrap();
writeln!(file, " }},").unwrap();
}
writeln!(file, " ],").unwrap();
if let Some(pos) = entry.pos {
2024-07-26 17:08:02 -04:00
write!(file, " pos: Some(Pos::{pos:?}),").unwrap();
2020-04-07 20:50:06 -04:00
} else {
write!(file, " pos: {:?},", entry.pos).unwrap();
}
writeln!(
file,
" archaic: {:?}, note: {:?},",
entry.archaic, entry.note
)
.unwrap();
writeln!(file, " description: {:?},", entry.description).unwrap();
writeln!(file, " comment: {:?},", entry.comment).unwrap();
writeln!(file, " }},").unwrap();
}
writeln!(file, " ],").unwrap();
writeln!(file, " notes: &[").unwrap();
for note in &cluster.notes {
2024-07-26 17:08:02 -04:00
writeln!(file, " {note:?},").unwrap();
2020-04-07 20:50:06 -04:00
}
writeln!(file, " ],").unwrap();
writeln!(file, " }},").unwrap();
}
writeln!(file, "];").unwrap();
}