| 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(feature = "serde")] |
| 6 |
|
| 7 |
use icu_datetime::fieldsets::builder::FieldSetBuilder; |
| 8 |
use icu_datetime::provider::fields::components; |
| 9 |
use serde::{Deserialize, Serialize}; |
| 10 |
use std::collections::HashMap; |
| 11 |
|
| 12 |
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 13 |
pub struct Fixture(pub Vec<Test>); |
| 14 |
|
| 15 |
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 16 |
pub struct Test { |
| 17 |
pub description: Option<String>, |
| 18 |
pub input: TestInput, |
| 19 |
pub output: TestOutput, |
| 20 |
} |
| 21 |
|
| 22 |
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 23 |
pub struct TestInput { |
| 24 |
pub value: String, |
| 25 |
pub options: TestOptions, |
| 26 |
} |
| 27 |
|
| 28 |
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 29 |
pub struct TestOptions { |
| 30 |
pub length: Option<TestOptionsLength>, |
| 31 |
pub components: Option<TestComponentsBag>, |
| 32 |
pub semantic: Option<FieldSetBuilder>, |
| 33 |
} |
| 34 |
|
| 35 |
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 36 |
pub struct TestOptionsLength { |
| 37 |
pub date: Option<TestLength>, |
| 38 |
pub time: Option<TestLength>, |
| 39 |
} |
| 40 |
|
| 41 |
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 42 |
pub enum TestLength { |
| 43 |
#[serde(rename = "short")] |
| 44 |
Short, |
| 45 |
#[serde(rename = "medium")] |
| 46 |
Medium, |
| 47 |
#[serde(rename = "long")] |
| 48 |
Long, |
| 49 |
#[serde(rename = "full")] |
| 50 |
Full, |
| 51 |
} |
| 52 |
|
| 53 |
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 54 |
pub struct TestComponentsBag { |
| 55 |
pub era: Option<components::Text>, |
| 56 |
pub year: Option<components::Year>, |
| 57 |
pub month: Option<components::Month>, |
| 58 |
pub week: Option<components::Week>, |
| 59 |
pub day: Option<components::Day>, |
| 60 |
pub weekday: Option<components::Text>, |
| 61 |
|
| 62 |
pub hour: Option<components::Numeric>, |
| 63 |
pub minute: Option<components::Numeric>, |
| 64 |
pub second: Option<components::Numeric>, |
| 65 |
pub subsecond: Option<u8>, |
| 66 |
|
| 67 |
pub time_zone_name: Option<components::TimeZoneName>, |
| 68 |
} |
| 69 |
|
| 70 |
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 71 |
pub struct TestOutput { |
| 72 |
// Key is locale, and value is expected test output. |
| 73 |
pub values: HashMap<String, TestOutputItem>, |
| 74 |
} |
| 75 |
|
| 76 |
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 77 |
#[serde(untagged)] |
| 78 |
pub enum TestOutputItem { |
| 79 |
ExpectedString(String), |
| 80 |
ExpectedStringAndPattern { formatted: String, pattern: String }, |
| 81 |
} |
| 82 |
|
| 83 |
impl TestOutputItem { |
| 84 |
pub fn expectation(&self) -> &str { |
| 85 |
match self { |
| 86 |
Self::ExpectedString(s) => s, |
| 87 |
Self::ExpectedStringAndPattern { formatted, .. } => formatted, |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
pub fn pattern(&self) -> Option<&str> { |
| 92 |
match self { |
| 93 |
Self::ExpectedString(_) => None, |
| 94 |
Self::ExpectedStringAndPattern { pattern, .. } => Some(pattern), |
| 95 |
} |
| 96 |
} |
| 97 |
} |