| 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 crate::data_struct_impl; |
| 6 |
use proc_macro2::TokenStream as TokenStream2; |
| 7 |
use quote::quote; |
| 8 |
|
| 9 |
fn check(attr: TokenStream2, item: TokenStream2, expected: TokenStream2) { |
| 10 |
let actual = data_struct_impl(syn::parse2(attr).unwrap(), syn::parse2(item).unwrap()); |
| 11 |
assert_eq!(expected.to_string(), actual.to_string()); |
| 12 |
} |
| 13 |
|
| 14 |
#[test] |
| 15 |
#[rustfmt::skip] // inserts a comma |
| 16 |
fn test_basic() { |
| 17 |
// #[data_struct] |
| 18 |
check( |
| 19 |
quote!(), |
| 20 |
quote!( |
| 21 |
pub struct FooV1; |
| 22 |
), |
| 23 |
quote!( |
| 24 |
#[derive(icu_provider::prelude::yoke::Yokeable, icu_provider::prelude::zerofrom::ZeroFrom)] |
| 25 |
pub struct FooV1; |
| 26 |
), |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn test_data_marker() { |
| 32 |
// #[data_struct(FooV1Marker)] |
| 33 |
check( |
| 34 |
quote!(FooV1Marker), |
| 35 |
quote!( |
| 36 |
pub struct FooV1; |
| 37 |
), |
| 38 |
quote!( |
| 39 |
#[doc = "Marker type for [`FooV1`]"] |
| 40 |
pub struct FooV1Marker; |
| 41 |
impl icu_provider::DataMarker for FooV1Marker { |
| 42 |
type Yokeable = FooV1; |
| 43 |
} |
| 44 |
#[derive(icu_provider::prelude::yoke::Yokeable, icu_provider::prelude::zerofrom::ZeroFrom)] |
| 45 |
pub struct FooV1; |
| 46 |
), |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
#[test] |
| 51 |
fn test_keyed_data_marker() { |
| 52 |
// #[data_struct(BarV1Marker = "demo/bar@1")] |
| 53 |
check( |
| 54 |
quote!(BarV1Marker = "demo/bar@1"), |
| 55 |
quote!( |
| 56 |
pub struct FooV1; |
| 57 |
), |
| 58 |
quote!( |
| 59 |
#[doc = "Marker type for [`FooV1`]: \"demo/bar@1\"\n\n- Fallback priority: language (default)\n- Extension keyword: none (default)"] |
| 60 |
pub struct BarV1Marker; |
| 61 |
impl icu_provider::DataMarker for BarV1Marker { |
| 62 |
type Yokeable = FooV1; |
| 63 |
} |
| 64 |
impl icu_provider::KeyedDataMarker for BarV1Marker { |
| 65 |
const KEY: icu_provider::DataKey = icu_provider::data_key!( |
| 66 |
"demo/bar@1", |
| 67 |
icu_provider::DataKeyMetadata::construct_internal( |
| 68 |
icu_provider::_internal::LocaleFallbackPriority::const_default(), |
| 69 |
None, |
| 70 |
None, |
| 71 |
false, |
| 72 |
)); |
| 73 |
} |
| 74 |
#[derive(icu_provider::prelude::yoke::Yokeable, icu_provider::prelude::zerofrom::ZeroFrom)] |
| 75 |
pub struct FooV1; |
| 76 |
), |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn test_multi_named_keyed_data_marker() { |
| 82 |
// #[data_struct(FooV1Marker, BarV1Marker = "demo/bar@1", BazV1Marker = "demo/baz@1")] |
| 83 |
check( |
| 84 |
quote![ |
| 85 |
FooV1Marker, |
| 86 |
BarV1Marker = "demo/bar@1", |
| 87 |
BazV1Marker = "demo/baz@1", |
| 88 |
], |
| 89 |
quote!( |
| 90 |
pub struct FooV1<'data>; |
| 91 |
), |
| 92 |
quote!( |
| 93 |
#[doc = "Marker type for [`FooV1`]"] |
| 94 |
pub struct FooV1Marker; |
| 95 |
impl icu_provider::DataMarker for FooV1Marker { |
| 96 |
type Yokeable = FooV1<'static>; |
| 97 |
} |
| 98 |
#[doc = "Marker type for [`FooV1`]: \"demo/bar@1\"\n\n- Fallback priority: language (default)\n- Extension keyword: none (default)"] |
| 99 |
pub struct BarV1Marker; |
| 100 |
impl icu_provider::DataMarker for BarV1Marker { |
| 101 |
type Yokeable = FooV1<'static>; |
| 102 |
} |
| 103 |
impl icu_provider::KeyedDataMarker for BarV1Marker { |
| 104 |
const KEY: icu_provider::DataKey = icu_provider::data_key!( |
| 105 |
"demo/bar@1", |
| 106 |
icu_provider::DataKeyMetadata::construct_internal( |
| 107 |
icu_provider::_internal::LocaleFallbackPriority::const_default(), |
| 108 |
None, |
| 109 |
None, |
| 110 |
false, |
| 111 |
)); |
| 112 |
} |
| 113 |
#[doc = "Marker type for [`FooV1`]: \"demo/baz@1\"\n\n- Fallback priority: language (default)\n- Extension keyword: none (default)"] |
| 114 |
pub struct BazV1Marker; |
| 115 |
impl icu_provider::DataMarker for BazV1Marker { |
| 116 |
type Yokeable = FooV1<'static>; |
| 117 |
} |
| 118 |
impl icu_provider::KeyedDataMarker for BazV1Marker { |
| 119 |
const KEY: icu_provider::DataKey = icu_provider::data_key!( |
| 120 |
"demo/baz@1", |
| 121 |
icu_provider::DataKeyMetadata::construct_internal( |
| 122 |
icu_provider::_internal::LocaleFallbackPriority::const_default(), |
| 123 |
None, |
| 124 |
None, |
| 125 |
false, |
| 126 |
)); |
| 127 |
} |
| 128 |
#[derive(icu_provider::prelude::yoke::Yokeable, icu_provider::prelude::zerofrom::ZeroFrom)] |
| 129 |
pub struct FooV1<'data>; |
| 130 |
), |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn test_databake() { |
| 136 |
check( |
| 137 |
quote!(BarV1Marker = "demo/bar@1"), |
| 138 |
quote!( |
| 139 |
#[databake(path = test::path)] |
| 140 |
pub struct FooV1; |
| 141 |
), |
| 142 |
quote!( |
| 143 |
#[doc = "Marker type for [`FooV1`]: \"demo/bar@1\"\n\n- Fallback priority: language (default)\n- Extension keyword: none (default)"] |
| 144 |
#[derive(databake::Bake)] |
| 145 |
#[databake(path = test::path)] |
| 146 |
pub struct BarV1Marker; |
| 147 |
impl icu_provider::DataMarker for BarV1Marker { |
| 148 |
type Yokeable = FooV1; |
| 149 |
} |
| 150 |
impl icu_provider::KeyedDataMarker for BarV1Marker { |
| 151 |
const KEY: icu_provider::DataKey = icu_provider::data_key!( |
| 152 |
"demo/bar@1", |
| 153 |
icu_provider::DataKeyMetadata::construct_internal( |
| 154 |
icu_provider::_internal::LocaleFallbackPriority::const_default(), |
| 155 |
None, |
| 156 |
None, |
| 157 |
false, |
| 158 |
)); |
| 159 |
} |
| 160 |
#[derive(icu_provider::prelude::yoke::Yokeable, icu_provider::prelude::zerofrom::ZeroFrom)] |
| 161 |
#[databake(path = test::path)] |
| 162 |
pub struct FooV1; |
| 163 |
), |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
fn test_attributes() { |
| 169 |
// #[data_struct(FooV1Marker, marker(BarV1Marker, "demo/bar@1", fallback_by = "region", extension_kw = "ca"))] |
| 170 |
check( |
| 171 |
quote![ |
| 172 |
FooV1Marker, |
| 173 |
marker( |
| 174 |
BarV1Marker, |
| 175 |
"demo/bar@1", |
| 176 |
fallback_by = "region", |
| 177 |
extension_key = "ca", |
| 178 |
fallback_supplement = "collation", |
| 179 |
singleton, |
| 180 |
), |
| 181 |
], |
| 182 |
quote!( |
| 183 |
pub struct FooV1<'data>; |
| 184 |
), |
| 185 |
quote!( |
| 186 |
#[doc = "Marker type for [`FooV1`]"] |
| 187 |
pub struct FooV1Marker; |
| 188 |
impl icu_provider::DataMarker for FooV1Marker { |
| 189 |
type Yokeable = FooV1<'static>; |
| 190 |
} |
| 191 |
#[doc = "Marker type for [`FooV1`]: \"demo/bar@1\"\n\n- Fallback priority: region\n- Extension keyword: ca"] |
| 192 |
pub struct BarV1Marker; |
| 193 |
impl icu_provider::DataMarker for BarV1Marker { |
| 194 |
type Yokeable = FooV1<'static>; |
| 195 |
} |
| 196 |
impl icu_provider::KeyedDataMarker for BarV1Marker { |
| 197 |
const KEY: icu_provider::DataKey = icu_provider::data_key!( |
| 198 |
"demo/bar@1", |
| 199 |
icu_provider::DataKeyMetadata::construct_internal( |
| 200 |
icu_provider::_internal::LocaleFallbackPriority::Region, |
| 201 |
Some(icu_provider::_internal::locid::extensions::unicode::key!("ca")), |
| 202 |
Some(icu_provider::_internal::LocaleFallbackSupplement::Collation), |
| 203 |
true, |
| 204 |
)); |
| 205 |
} |
| 206 |
#[derive(icu_provider::prelude::yoke::Yokeable, icu_provider::prelude::zerofrom::ZeroFrom)] |
| 207 |
pub struct FooV1<'data>; |
| 208 |
), |
| 209 |
); |
| 210 |
} |