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

47 lines
1.2 KiB
Rust
Raw Permalink Normal View History

2022-08-01 15:45:58 -04:00
const DICT: &[u8] = include_bytes!("../assets/words.csv");
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();
snapbox::assert_eq_path("./src/dict_codegen.rs", content);
}
2019-10-28 12:09:29 -04:00
fn generate<W: std::io::Write>(file: &mut W) {
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)
2020-08-15 22:03:00 -04:00
.from_reader(DICT)
.records()
.map(|r| r.unwrap())
.collect();
dictgen::generate_trie(
file,
"WORD",
"&'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();
}