| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
#![cfg(all(test, feature = "datagen"))] |
| 6 |
|
| 7 |
use icu_datetime::provider::pattern::reference; |
| 8 |
use std::{fs::File, io::BufReader}; |
| 9 |
|
| 10 |
#[derive(serde::Serialize, serde::Deserialize)] |
| 11 |
struct InvalidPatternFixture { |
| 12 |
pub pattern: String, |
| 13 |
pub error: String, |
| 14 |
} |
| 15 |
|
| 16 |
#[derive(serde::Serialize, serde::Deserialize)] |
| 17 |
struct PatternFixtures { |
| 18 |
pub valid_patterns: Vec<String>, |
| 19 |
pub invalid_patterns: Vec<InvalidPatternFixture>, |
| 20 |
} |
| 21 |
|
| 22 |
fn get_pattern_fixtures() -> PatternFixtures { |
| 23 |
let file = File::open("./tests/fixtures/tests/patterns.json") |
| 24 |
.expect("Unable to open ./tests/fixtures/tests/patterns.json"); |
| 25 |
let reader = BufReader::new(file); |
| 26 |
serde_json::from_reader(reader).expect("Unable to deserialize pattern fixtures.") |
| 27 |
} |
| 28 |
|
| 29 |
fn get_pattern_strings() -> Vec<String> { |
| 30 |
get_pattern_fixtures().valid_patterns |
| 31 |
} |
| 32 |
|
| 33 |
fn get_invalid_pattern_strings() -> Vec<InvalidPatternFixture> { |
| 34 |
get_pattern_fixtures().invalid_patterns |
| 35 |
} |
| 36 |
|
| 37 |
fn get_pattern_bincode_write_handle() -> File { |
| 38 |
File::create("./tests/fixtures/tests/patterns.bin") |
| 39 |
.expect("Unable to create ./tests/fixtures/tests/patterns.bin") |
| 40 |
} |
| 41 |
|
| 42 |
fn get_pattern_bincode_from_file() -> Vec<Vec<u8>> { |
| 43 |
bincode::deserialize_from( |
| 44 |
File::open("./tests/fixtures/tests/patterns.bin") |
| 45 |
.expect("Unable to ./tests/fixtures/tests/patterns.bin"), |
| 46 |
) |
| 47 |
.expect("Unable to deserialize bytes.") |
| 48 |
} |
| 49 |
|
| 50 |
#[test] |
| 51 |
fn test_pattern_json_serialization_roundtrip() { |
| 52 |
for pattern_string in &get_pattern_strings() { |
| 53 |
// Wrap the string in quotes so it's a JSON string. |
| 54 |
let json_in: String = serde_json::to_string(pattern_string).unwrap(); |
| 55 |
|
| 56 |
let pattern: reference::Pattern = match serde_json::from_str(&json_in) { |
| 57 |
Ok(p) => p, |
| 58 |
Err(err) => { |
| 59 |
panic!("Unable to parse the pattern {pattern_string:?}. {err:?}"); |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
let json_out = match serde_json::to_string(&pattern) { |
| 64 |
Ok(s) => s, |
| 65 |
Err(err) => { |
| 66 |
panic!("Unable to re-serialize the pattern {pattern_string:?}. {err:?}",); |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
assert_eq!( |
| 71 |
json_in, json_out, |
| 72 |
"The roundtrip serialization for the pattern matched." |
| 73 |
); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/// Bincode representation of patterns need to be stable across time. This test checks the |
| 78 |
/// current serialization against historic serialization to ensure that this remains stable. |
| 79 |
#[test] |
| 80 |
fn test_pattern_bincode_serialization_roundtrip() { |
| 81 |
let patterns = get_pattern_strings(); |
| 82 |
let update_bincode = std::env::var_os("ICU4X_REGEN_FIXTURE").is_some(); |
| 83 |
let mut result_vec = Vec::new(); |
| 84 |
let expect_vec = if update_bincode { |
| 85 |
None |
| 86 |
} else { |
| 87 |
Some(get_pattern_bincode_from_file()) |
| 88 |
}; |
| 89 |
|
| 90 |
if let Some(ref expect_vec) = expect_vec |
| 91 |
&& expect_vec.len() != patterns.len() |
| 92 |
{ |
| 93 |
panic!( |
| 94 |
"Expected the bincode to have the same number of entries as the string patterns. \ |
| 95 |
The bincode can be re-generated by re-running the test with the environment |
| 96 |
variable ICU4X_REGEN_FIXTURE set." |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
for (i, pattern_string) in patterns.iter().enumerate() { |
| 101 |
// Wrap the string in quotes so it's a JSON string. |
| 102 |
let json_in: String = serde_json::to_string(pattern_string).unwrap(); |
| 103 |
|
| 104 |
let pattern: reference::Pattern = match serde_json::from_str(&json_in) { |
| 105 |
Ok(p) => p, |
| 106 |
Err(err) => { |
| 107 |
panic!("Unable to parse the pattern {pattern_string:?}. {err:?}"); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
let bincode: Vec<u8> = bincode::serialize(&pattern).unwrap(); |
| 112 |
|
| 113 |
if let Some(ref expect_vec) = expect_vec |
| 114 |
&& bincode != *expect_vec.get(i).unwrap() |
| 115 |
{ |
| 116 |
panic!( |
| 117 |
"The bincode representations of the pattern {json_in:?} did not match the stored \ |
| 118 |
representation. Patterns are supposed to have stable bincode representations. \ |
| 119 |
Something changed to make it different than what it was in the past. If this is \ |
| 120 |
expected, then the bincode can be updated by re-running the test with the \ |
| 121 |
environment variable ICU4X_REGEN_FIXTURE set." |
| 122 |
) |
| 123 |
} |
| 124 |
result_vec.push(bincode); |
| 125 |
} |
| 126 |
if update_bincode { |
| 127 |
eprintln!("Writing the bincode into a file"); |
| 128 |
bincode::serialize_into(&mut get_pattern_bincode_write_handle(), &result_vec).unwrap(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/// Test that pattern serialization produces sensible error messages given the Serde |
| 133 |
/// [`serde::de::Unexpected`] type and the use of [`core::fmt::Display`] traits on the Error objects. |
| 134 |
#[test] |
| 135 |
fn test_pattern_json_errors() { |
| 136 |
for InvalidPatternFixture { pattern, error } in &get_invalid_pattern_strings() { |
| 137 |
// Wrap the string in quotes so it's a JSON string. |
| 138 |
let json_in: String = serde_json::to_string(pattern).unwrap(); |
| 139 |
|
| 140 |
// Wrap the string in quotes so it's a JSON string. |
| 141 |
match serde_json::from_str::<reference::Pattern>(&json_in) { |
| 142 |
Ok(_) => panic!("Expected an invalid pattern. {json_in}"), |
| 143 |
Err(serde_err) => { |
| 144 |
assert_eq!(format!("{serde_err}"), *error); |
| 145 |
} |
| 146 |
}; |
| 147 |
} |
| 148 |
} |