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

46 lines
1.3 KiB
Rust
Raw 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();
snapbox::assert_eq_path("./src/word_codegen.rs", content);
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();
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!(
"&[{}]",
itertools::join(record_fields.map(|field| format!(r#""{}""#, field)), ", ")
);
(key, value)
}),
64,
)
.unwrap();
}