typos/crates/typos-dict/tests/codegen.rs

47 lines
1.4 KiB
Rust
Raw Permalink Normal View History

2022-08-01 15:45:58 -04:00
#[test]
fn codegen() {
let mut content = vec![];
const DICT: &[u8] = include_bytes!("../assets/words.csv");
generate(&mut content, "WORD", DICT);
2022-08-01 15:45:58 -04:00
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/word_codegen.rs"].raw());
2022-08-01 15:45:58 -04:00
}
2019-10-28 12:09:29 -04:00
fn generate<W: std::io::Write>(file: &mut W, prefix: &str, dict: &[u8]) {
2022-09-01 08:15:42 -04:00
writeln!(
file,
"// This file is @generated by {}",
file!().replace('\\', "/")
)
.unwrap();
writeln!(file, "#![allow(clippy::unreadable_literal)]",).unwrap();
2024-04-26 22:14:01 -04:00
writeln!(file, "#![allow(unreachable_pub)]",).unwrap();
writeln!(file).unwrap();
2020-08-15 22:03:00 -04:00
let records: Vec<_> = csv::ReaderBuilder::new()
.has_headers(false)
.flexible(true)
.from_reader(dict)
.records()
.map(|r| r.unwrap())
.collect();
dictgen::generate_trie(
file,
prefix,
"&'static [&'static str]",
records.iter().map(|record| {
let mut record_fields = record.iter();
let key = record_fields.next().unwrap();
let value = format!(
"&[{}]",
2024-07-26 17:08:02 -04:00
itertools::join(record_fields.map(|field| format!(r#""{field}""#)), ", ")
);
(key, value)
}),
64,
)
.unwrap();
}