| 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 |
use fixed_decimal::Decimal; |
| 6 |
#[cfg(feature = "unstable")] |
| 7 |
use icu_plurals::PluralOperands; |
| 8 |
use icu_plurals::{PluralCategory, PluralRuleType, PluralRulesOptions}; |
| 9 |
use serde::Deserialize; |
| 10 |
|
| 11 |
/// Defines the data-driven test sets for the operands. |
| 12 |
#[derive(Deserialize)] |
| 13 |
#[allow(dead_code)] |
| 14 |
pub struct OperandsTestSet { |
| 15 |
pub string: Vec<OperandsTest<String>>, |
| 16 |
pub int: Vec<OperandsTest<isize>>, |
| 17 |
pub floats: Vec<OperandsTest<f64>>, |
| 18 |
pub from_test: Vec<FromTestCase>, |
| 19 |
} |
| 20 |
|
| 21 |
/// A single test case verifying the conversion from [`FixedDecimal`] into |
| 22 |
/// [`PluralOperands`]. |
| 23 |
#[derive(Debug, Deserialize)] |
| 24 |
#[allow(dead_code)] |
| 25 |
pub struct FromTestCase { |
| 26 |
/// The [`FixedDecimal`] input |
| 27 |
pub input: FixedDecimalInput, |
| 28 |
/// The expected value after conversion. |
| 29 |
pub expected: PluralOperandsInput, |
| 30 |
} |
| 31 |
|
| 32 |
/// A serialized representation of [`FixedDecimal`] in the data driven tests. |
| 33 |
/// |
| 34 |
/// Use the `From` trait to convert into [`FixedDecimal`] in tests. |
| 35 |
#[derive(Debug, Deserialize)] |
| 36 |
pub struct FixedDecimalInput { |
| 37 |
/// Value supplied to [`FixedDecimal::from`] when constructing. |
| 38 |
from: i64, |
| 39 |
/// Value supplied to [`FixedDecimal::multiplied_pow10`] when constructing. |
| 40 |
pow10: i16, |
| 41 |
} |
| 42 |
|
| 43 |
impl From<&FixedDecimalInput> for Decimal { |
| 44 |
fn from(f: &FixedDecimalInput) -> Self { |
| 45 |
let mut dec = Decimal::from(f.from); |
| 46 |
dec.multiply_pow10(f.pow10); |
| 47 |
dec |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#[derive(Debug, Clone, Deserialize)] |
| 52 |
#[serde(untagged)] |
| 53 |
pub enum PluralOperandsInput { |
| 54 |
List((u64, usize, usize, u64, u64, usize)), |
| 55 |
Struct { |
| 56 |
i: Option<u64>, |
| 57 |
v: Option<usize>, |
| 58 |
w: Option<usize>, |
| 59 |
f: Option<u64>, |
| 60 |
t: Option<u64>, |
| 61 |
c: Option<usize>, |
| 62 |
}, |
| 63 |
String(String), |
| 64 |
Number(isize), |
| 65 |
} |
| 66 |
|
| 67 |
#[cfg(feature = "unstable")] |
| 68 |
impl From<PluralOperandsInput> for PluralOperands { |
| 69 |
fn from(input: PluralOperandsInput) -> Self { |
| 70 |
use icu_plurals::RawPluralOperands; |
| 71 |
match input { |
| 72 |
PluralOperandsInput::List((i, v, w, f, t, c)) => { |
| 73 |
PluralOperands::from(RawPluralOperands { i, v, w, f, t, c }) |
| 74 |
} |
| 75 |
PluralOperandsInput::Struct { i, v, w, f, t, c } => { |
| 76 |
PluralOperands::from(RawPluralOperands { |
| 77 |
i: i.unwrap_or(0), |
| 78 |
v: v.unwrap_or(0), |
| 79 |
w: w.unwrap_or(0), |
| 80 |
f: f.unwrap_or(0), |
| 81 |
t: t.unwrap_or(0), |
| 82 |
c: c.unwrap_or(0), |
| 83 |
}) |
| 84 |
} |
| 85 |
PluralOperandsInput::String(num) => num |
| 86 |
.parse() |
| 87 |
.expect("Failed to parse a number into operands."), |
| 88 |
PluralOperandsInput::Number(num) => num.into(), |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
#[derive(Debug, Clone, Deserialize)] |
| 94 |
#[allow(dead_code)] |
| 95 |
pub struct OperandsTest<T> { |
| 96 |
pub input: T, |
| 97 |
pub output: PluralOperandsInput, |
| 98 |
} |
| 99 |
|
| 100 |
#[derive(Deserialize)] |
| 101 |
#[allow(dead_code)] |
| 102 |
pub struct RuleTest { |
| 103 |
pub rule: String, |
| 104 |
pub input: PluralOperandsInput, |
| 105 |
pub output: RuleTestOutput, |
| 106 |
} |
| 107 |
|
| 108 |
#[derive(Deserialize)] |
| 109 |
#[serde(untagged)] |
| 110 |
#[allow(dead_code)] |
| 111 |
pub enum RuleTestOutput { |
| 112 |
Value(bool), |
| 113 |
Error(String), |
| 114 |
} |
| 115 |
|
| 116 |
#[derive(Deserialize)] |
| 117 |
#[allow(dead_code)] |
| 118 |
pub struct RuleTestSet(pub Vec<RuleTest>); |
| 119 |
|
| 120 |
#[derive(Clone, Copy, Debug, Deserialize)] |
| 121 |
pub enum PluralRuleTypeInput { |
| 122 |
Cardinal, |
| 123 |
Ordinal, |
| 124 |
} |
| 125 |
|
| 126 |
impl From<PluralRuleTypeInput> for PluralRulesOptions { |
| 127 |
fn from(other: PluralRuleTypeInput) -> Self { |
| 128 |
match other { |
| 129 |
PluralRuleTypeInput::Cardinal => PluralRuleType::Cardinal.into(), |
| 130 |
PluralRuleTypeInput::Ordinal => PluralRuleType::Ordinal.into(), |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
#[derive(Debug, Deserialize)] |
| 136 |
#[serde(rename_all = "lowercase")] |
| 137 |
pub enum PluralCategoryInput { |
| 138 |
Zero, |
| 139 |
One, |
| 140 |
Two, |
| 141 |
Few, |
| 142 |
Many, |
| 143 |
Other, |
| 144 |
} |
| 145 |
|
| 146 |
impl PartialEq<PluralCategory> for &PluralCategoryInput { |
| 147 |
fn eq(&self, other: &PluralCategory) -> bool { |
| 148 |
matches!( |
| 149 |
(self, other), |
| 150 |
(PluralCategoryInput::Zero, PluralCategory::Zero) |
| 151 |
| (PluralCategoryInput::One, PluralCategory::One) |
| 152 |
| (PluralCategoryInput::Two, PluralCategory::Two) |
| 153 |
| (PluralCategoryInput::Few, PluralCategory::Few) |
| 154 |
| (PluralCategoryInput::Many, PluralCategory::Many) |
| 155 |
| (PluralCategoryInput::Other, PluralCategory::Other) |
| 156 |
) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
#[derive(Deserialize)] |
| 161 |
#[allow(dead_code)] |
| 162 |
pub struct CategoriesTest { |
| 163 |
pub langid: String, |
| 164 |
pub plural_type: PluralRuleTypeInput, |
| 165 |
pub categories: Vec<PluralCategoryInput>, |
| 166 |
} |