From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- .../rust/unic-langid-impl/tests/fixtures.rs | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 third_party/rust/unic-langid-impl/tests/fixtures.rs (limited to 'third_party/rust/unic-langid-impl/tests/fixtures.rs') diff --git a/third_party/rust/unic-langid-impl/tests/fixtures.rs b/third_party/rust/unic-langid-impl/tests/fixtures.rs new file mode 100644 index 0000000000..27cd29b33a --- /dev/null +++ b/third_party/rust/unic-langid-impl/tests/fixtures.rs @@ -0,0 +1,75 @@ +use std::convert::TryInto; +use std::error::Error; +use std::fs::File; +use std::path::Path; + +use unic_langid_impl::LanguageIdentifier; + +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +struct LangIdTestInputData { + string: String, +} + +#[derive(Serialize, Deserialize, Debug)] +struct LangIdTestOutputObject { + language: Option, + script: Option, + region: Option, + #[serde(default)] + variants: Vec, +} + +#[derive(Serialize, Deserialize, Debug)] +#[serde(untagged)] +enum LangIdTestOutput { + String(String), + Object(LangIdTestOutputObject), +} + +#[derive(Serialize, Deserialize)] +struct LangIdTestSet { + input: LangIdTestInputData, + output: LangIdTestOutput, +} + +fn read_langid_testsets>(path: P) -> Result, Box> { + let file = File::open(path)?; + let sets = serde_json::from_reader(file)?; + Ok(sets) +} + +fn test_langid_fixtures(path: &str) { + let tests = read_langid_testsets(path).unwrap(); + + for test in tests { + let s = test.input.string; + + let langid: LanguageIdentifier = s.parse().expect("Parsing failed."); + + match test.output { + LangIdTestOutput::Object(o) => { + let expected = LanguageIdentifier::from_parts( + o.language.try_into().unwrap(), + o.script.as_ref().map(|s| s.parse().unwrap()), + o.region.as_ref().map(|r| r.parse().unwrap()), + o.variants + .iter() + .map(|s| s.parse().unwrap()) + .collect::>() + .as_ref(), + ); + assert_eq!(langid, expected); + } + LangIdTestOutput::String(s) => { + assert_eq!(langid.to_string(), s); + } + } + } +} + +#[test] +fn parse() { + test_langid_fixtures("./tests/fixtures/parsing.json"); +} -- cgit v1.2.3