Debian Codegraph @

debian/rust-vendor/icu_datetime/tests/skeleton_serialization.rs

raw
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::skeleton::reference::Skeleton;
8 use std::{fs::File, io::BufReader};
9
10 /// Note that this file tests only valid skeleton cases for the stability of the serialization
11 /// pipeline. For tests failure cases see the file where skeletons are defined.
12
13 #[derive(serde::Serialize, serde::Deserialize)]
14 struct SkeletonFixtures {
15 pub skeletons: Vec<String>,
16 }
17
18 fn get_skeleton_fixtures() -> Vec<String> {
19 let file = File::open("./tests/fixtures/tests/skeletons.json")
20 .expect("Unable to open ./tests/fixtures/tests/skeletons.json");
21 let reader = BufReader::new(file);
22 let fixtures: SkeletonFixtures =
23 serde_json::from_reader(reader).expect("Unable to deserialize skeleton fixtures.");
24 fixtures.skeletons
25 }
26
27 fn get_skeleton_bincode_write_handle() -> File {
28 File::create("./tests/fixtures/tests/skeletons.bin")
29 .expect("Unable to create ./tests/fixtures/tests/skeletons.bin")
30 }
31
32 fn get_skeleton_bincode_from_file() -> Vec<Vec<u8>> {
33 bincode::deserialize_from(
34 File::open("./tests/fixtures/tests/skeletons.bin")
35 .expect("Unable to ./tests/fixtures/tests/skeletons.bin"),
36 )
37 .expect("Unable to deserialize bytes.")
38 }
39
40 #[test]
41 fn test_skeleton_json_serialization_roundtrip() {
42 for skeleton_string in &get_skeleton_fixtures() {
43 if skeleton_string == "yw" {
44 // TODO(#5643)
45 continue;
46 }
47
48 // Wrap the string in quotes so it's a JSON string.
49 let json_in: String = serde_json::to_string(skeleton_string).unwrap();
50
51 let skeleton: Skeleton = match serde_json::from_str(&json_in) {
52 Ok(p) => p,
53 Err(err) => {
54 panic!("Unable to parse the skeleton {skeleton_string:?}. {err:?}");
55 }
56 };
57
58 let json_out = match serde_json::to_string(&skeleton) {
59 Ok(s) => s,
60 Err(err) => {
61 panic!("Unable to re-serialize the skeleton {skeleton_string:?}. {err:?}");
62 }
63 };
64
65 assert_eq!(
66 json_in, json_out,
67 "The roundtrip serialization for the skeleton matched."
68 );
69 }
70 }
71
72 /// Bincode representation of skeletons need to be stable across time. This test checks the
73 /// current serialization against historic serialization to ensure that this remains stable.
74 #[test]
75 fn test_skeleton_bincode_serialization_roundtrip() {
76 let skeletons = get_skeleton_fixtures();
77 let update_bincode = std::env::var_os("ICU4X_REGEN_FIXTURE").is_some();
78 let mut result_vec = Vec::new();
79 let expect_vec = if update_bincode {
80 None
81 } else {
82 Some(get_skeleton_bincode_from_file())
83 };
84
85 // TODO(#5643)
86 let skeletons = skeletons
87 .into_iter()
88 .filter(|s| s != "yw")
89 .collect::<Vec<_>>();
90
91 if let Some(ref expect_vec) = expect_vec
92 && expect_vec.len() != skeletons.len()
93 {
94 panic!(
95 "Expected the bincode to have the same number of entries as the string skeletons. \
96 The bincode can be re-generated by re-running the test with the environment
97 variable ICU4X_REGEN_FIXTURE set."
98 );
99 }
100
101 for (i, skeleton_string) in skeletons.iter().enumerate() {
102 // Wrap the string in quotes so it's a JSON string.
103 let json_in: String = serde_json::to_string(skeleton_string).unwrap();
104
105 let skeleton: Skeleton = match serde_json::from_str(&json_in) {
106 Ok(p) => p,
107 Err(err) => {
108 panic!("Unable to parse the skeleton {skeleton_string:?}. {err:?}");
109 }
110 };
111
112 let bincode: Vec<u8> = bincode::serialize(&skeleton).unwrap();
113
114 if let Some(ref expect_vec) = expect_vec
115 && bincode != *expect_vec.get(i).unwrap()
116 {
117 panic!(
118 "The bincode representations of the skeleton {json_in:?} did not match the stored \
119 representation. Skeletons are supposed to have stable bincode representations. \
120 Something changed to make it different than what it was in the past. If this is \
121 expected, then the bincode can be updated by re-running the test with the \
122 environment variable ICU4X_REGEN_FIXTURE set."
123 )
124 }
125 result_vec.push(bincode);
126 }
127 if update_bincode {
128 eprintln!("Writing the bincode into a file");
129 bincode::serialize_into(&mut get_skeleton_bincode_write_handle(), &result_vec).unwrap();
130 }
131 }