summaryrefslogtreecommitdiffstats
path: root/vendor/icu_provider_macros/src/tests.rs
blob: 51411852b562cb383d32dd146e762e47a2c909b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::data_struct_impl;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{DeriveInput, NestedMeta};

fn check(attr: Vec<TokenStream2>, item: TokenStream2, expected: TokenStream2) {
    let actual = data_struct_impl(
        attr.into_iter()
            .map(syn::parse2)
            .collect::<syn::parse::Result<Vec<NestedMeta>>>()
            .unwrap(),
        syn::parse2::<DeriveInput>(item).unwrap(),
    );
    assert_eq!(expected.to_string(), actual.to_string());
}

#[test]
fn test_basic() {
    // #[data_struct]
    check(
        vec![],
        quote!(
            pub struct FooV1;
        ),
        quote!(
            #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
            pub struct FooV1;
        ),
    );
}

#[test]
fn test_data_marker() {
    // #[data_struct(FooV1Marker)]
    check(
        vec![quote!(FooV1Marker)],
        quote!(
            pub struct FooV1;
        ),
        quote!(
            #[doc = "Marker type for [`FooV1`]"]
            pub struct FooV1Marker;
            impl icu_provider::DataMarker for FooV1Marker {
                type Yokeable = FooV1;
            }
            #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
            pub struct FooV1;
        ),
    );
}

#[test]
fn test_keyed_data_marker() {
    // #[data_struct(BarV1Marker = "demo/bar@1")]
    check(
        vec![quote!(BarV1Marker = "demo/bar@1")],
        quote!(
            pub struct FooV1;
        ),
        quote!(
            #[doc = "Marker type for [`FooV1`]: \"demo/bar@1\"\n\n- Fallback priority: language (default)\n- Extension keyword: none (default)"]
            pub struct BarV1Marker;
            impl icu_provider::DataMarker for BarV1Marker {
                type Yokeable = FooV1;
            }
            impl icu_provider::KeyedDataMarker for BarV1Marker {
                const KEY: icu_provider::DataKey = icu_provider::data_key!(
                    "demo/bar@1",
                    icu_provider::DataKeyMetadata::construct_internal(
                        icu_provider::FallbackPriority::const_default(),
                        None,
                        None
                    ));
            }
            #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
            pub struct FooV1;
        ),
    );
}

#[test]
fn test_multi_named_keyed_data_marker() {
    // #[data_struct(FooV1Marker, BarV1Marker = "demo/bar@1", BazV1Marker = "demo/baz@1")]
    check(
        vec![
            quote!(FooV1Marker),
            quote!(BarV1Marker = "demo/bar@1"),
            quote!(BazV1Marker = "demo/baz@1"),
        ],
        quote!(
            pub struct FooV1<'data>;
        ),
        quote!(
            #[doc = "Marker type for [`FooV1`]"]
            pub struct FooV1Marker;
            impl icu_provider::DataMarker for FooV1Marker {
                type Yokeable = FooV1<'static>;
            }
            #[doc = "Marker type for [`FooV1`]: \"demo/bar@1\"\n\n- Fallback priority: language (default)\n- Extension keyword: none (default)"]
            pub struct BarV1Marker;
            impl icu_provider::DataMarker for BarV1Marker {
                type Yokeable = FooV1<'static>;
            }
            impl icu_provider::KeyedDataMarker for BarV1Marker {
                const KEY: icu_provider::DataKey = icu_provider::data_key!(
                    "demo/bar@1",
                    icu_provider::DataKeyMetadata::construct_internal(
                        icu_provider::FallbackPriority::const_default(),
                        None,
                        None
                    ));
            }
            #[doc = "Marker type for [`FooV1`]: \"demo/baz@1\"\n\n- Fallback priority: language (default)\n- Extension keyword: none (default)"]
            pub struct BazV1Marker;
            impl icu_provider::DataMarker for BazV1Marker {
                type Yokeable = FooV1<'static>;
            }
            impl icu_provider::KeyedDataMarker for BazV1Marker {
                const KEY: icu_provider::DataKey = icu_provider::data_key!(
                    "demo/baz@1",
                    icu_provider::DataKeyMetadata::construct_internal(
                        icu_provider::FallbackPriority::const_default(),
                        None,
                        None
                    ));
            }
            #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
            pub struct FooV1<'data>;
        ),
    );
}

#[test]
fn test_databake() {
    check(
        vec![quote!(BarV1Marker = "demo/bar@1")],
        quote!(
            #[databake(path = test::path)]
            pub struct FooV1;
        ),
        quote!(
            #[doc = "Marker type for [`FooV1`]: \"demo/bar@1\"\n\n- Fallback priority: language (default)\n- Extension keyword: none (default)"]
            #[derive(Default, databake::Bake)]
            #[databake(path = test::path)]
            pub struct BarV1Marker;
            impl icu_provider::DataMarker for BarV1Marker {
                type Yokeable = FooV1;
            }
            impl icu_provider::KeyedDataMarker for BarV1Marker {
                const KEY: icu_provider::DataKey = icu_provider::data_key!(
                    "demo/bar@1",
                    icu_provider::DataKeyMetadata::construct_internal(
                        icu_provider::FallbackPriority::const_default(),
                        None,
                        None
                    ));
            }
            #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
            #[databake(path = test::path)]
            pub struct FooV1;
        ),
    );
}

#[test]
fn test_attributes() {
    // #[data_struct(FooV1Marker, marker(BarV1Marker, "demo/bar@1", fallback_by = "region", extension_kw = "ca"))]
    check(
        vec![
            quote!(FooV1Marker),
            quote!(marker(
                BarV1Marker,
                "demo/bar@1",
                fallback_by = "region",
                extension_key = "ca",
                fallback_supplement = "collation"
            )),
        ],
        quote!(
            pub struct FooV1<'data>;
        ),
        quote!(
            #[doc = "Marker type for [`FooV1`]"]
            pub struct FooV1Marker;
            impl icu_provider::DataMarker for FooV1Marker {
                type Yokeable = FooV1<'static>;
            }
            #[doc = "Marker type for [`FooV1`]: \"demo/bar@1\"\n\n- Fallback priority: region\n- Extension keyword: ca"]
            pub struct BarV1Marker;
            impl icu_provider::DataMarker for BarV1Marker {
                type Yokeable = FooV1<'static>;
            }
            impl icu_provider::KeyedDataMarker for BarV1Marker {
                const KEY: icu_provider::DataKey = icu_provider::data_key!(
                    "demo/bar@1",
                    icu_provider::DataKeyMetadata::construct_internal(
                        icu_provider::FallbackPriority::Region,
                        Some(icu_provider::_internal::extensions_unicode_key!("ca")),
                        Some(icu_provider::FallbackSupplement::Collation)
                    ));
            }
            #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
            pub struct FooV1<'data>;
        ),
    );
}