summaryrefslogtreecommitdiffstats
path: root/intl/l10n/rust/l10nregistry-rs/src/testing.rs
blob: f1ff47e2dbc036f435af12f2eae2ea737a2a316e (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use crate::env::ErrorReporter;
use crate::errors::L10nRegistryError;
use crate::fluent::FluentBundle;
use crate::registry::BundleAdapter;
use crate::registry::L10nRegistry;
use crate::source::FileFetcher;
use async_trait::async_trait;
use fluent_fallback::{env::LocalesProvider, types::ResourceId};
use fluent_testing::MockFileSystem;
use std::cell::RefCell;
use std::rc::Rc;
use unic_langid::LanguageIdentifier;

pub struct RegistrySetup {
    pub name: String,
    pub file_sources: Vec<FileSource>,
    pub locales: Vec<LanguageIdentifier>,
}

pub struct FileSource {
    pub name: String,
    pub metasource: String,
    pub locales: Vec<LanguageIdentifier>,
    pub path_scheme: String,
}

#[derive(Clone)]
pub struct MockBundleAdapter;

impl BundleAdapter for MockBundleAdapter {
    fn adapt_bundle(&self, _bundle: &mut FluentBundle) {}
}

impl FileSource {
    pub fn new<S>(
        name: S,
        metasource: Option<S>,
        locales: Vec<LanguageIdentifier>,
        path_scheme: S,
    ) -> Self
    where
        S: ToString,
    {
        let metasource = match metasource {
            Some(s) => s.to_string(),
            None => String::default(),
        };

        Self {
            name: name.to_string(),
            metasource,
            locales,
            path_scheme: path_scheme.to_string(),
        }
    }
}

impl RegistrySetup {
    pub fn new(
        name: &str,
        file_sources: Vec<FileSource>,
        locales: Vec<LanguageIdentifier>,
    ) -> Self {
        Self {
            name: name.to_string(),
            file_sources,
            locales,
        }
    }
}

impl From<fluent_testing::scenarios::structs::Scenario> for RegistrySetup {
    fn from(s: fluent_testing::scenarios::structs::Scenario) -> Self {
        Self {
            name: s.name,
            file_sources: s
                .file_sources
                .into_iter()
                .map(|source| {
                    FileSource::new(
                        source.name,
                        None,
                        source
                            .locales
                            .into_iter()
                            .map(|l| l.parse().unwrap())
                            .collect(),
                        source.path_scheme,
                    )
                })
                .collect(),
            locales: s
                .locales
                .into_iter()
                .map(|loc| loc.parse().unwrap())
                .collect(),
        }
    }
}

impl From<&fluent_testing::scenarios::structs::Scenario> for RegistrySetup {
    fn from(s: &fluent_testing::scenarios::structs::Scenario) -> Self {
        Self {
            name: s.name.clone(),
            file_sources: s
                .file_sources
                .iter()
                .map(|source| {
                    FileSource::new(
                        source.name.clone(),
                        None,
                        source.locales.iter().map(|l| l.parse().unwrap()).collect(),
                        source.path_scheme.clone(),
                    )
                })
                .collect(),
            locales: s.locales.iter().map(|loc| loc.parse().unwrap()).collect(),
        }
    }
}

#[derive(Default)]
struct InnerFileFetcher {
    fs: MockFileSystem,
}

#[derive(Clone)]
pub struct TestFileFetcher {
    inner: Rc<InnerFileFetcher>,
}

impl TestFileFetcher {
    pub fn new() -> Self {
        Self {
            inner: Rc::new(InnerFileFetcher::default()),
        }
    }

    pub fn get_test_file_source(
        &self,
        name: &str,
        metasource: Option<String>,
        locales: Vec<LanguageIdentifier>,
        path: &str,
    ) -> crate::source::FileSource {
        crate::source::FileSource::new(
            name.to_string(),
            metasource,
            locales,
            path.to_string(),
            Default::default(),
            self.clone(),
        )
    }

    pub fn get_test_file_source_with_index(
        &self,
        name: &str,
        metasource: Option<String>,
        locales: Vec<LanguageIdentifier>,
        path: &str,
        index: Vec<&str>,
    ) -> crate::source::FileSource {
        crate::source::FileSource::new_with_index(
            name.to_string(),
            metasource,
            locales,
            path.to_string(),
            Default::default(),
            self.clone(),
            index.into_iter().map(|s| s.to_string()).collect(),
        )
    }

    pub fn get_registry<S>(&self, setup: S) -> L10nRegistry<TestEnvironment, MockBundleAdapter>
    where
        S: Into<RegistrySetup>,
    {
        self.get_registry_and_environment(setup).1
    }

    pub fn get_registry_and_environment<S>(
        &self,
        setup: S,
    ) -> (
        TestEnvironment,
        L10nRegistry<TestEnvironment, MockBundleAdapter>,
    )
    where
        S: Into<RegistrySetup>,
    {
        let setup: RegistrySetup = setup.into();
        let provider = TestEnvironment::new(setup.locales);

        let reg = L10nRegistry::with_provider(provider.clone());
        let sources = setup
            .file_sources
            .into_iter()
            .map(|source| {
                let mut s = self.get_test_file_source(
                    &source.name,
                    Some(source.metasource),
                    source.locales,
                    &source.path_scheme,
                );
                s.set_reporter(provider.clone());
                s
            })
            .collect();
        reg.register_sources(sources).unwrap();
        (provider, reg)
    }

    pub fn get_registry_and_environment_with_adapter<S, B>(
        &self,
        setup: S,
        bundle_adapter: B,
    ) -> (TestEnvironment, L10nRegistry<TestEnvironment, B>)
    where
        S: Into<RegistrySetup>,
        B: BundleAdapter,
    {
        let setup: RegistrySetup = setup.into();
        let provider = TestEnvironment::new(setup.locales);

        let mut reg = L10nRegistry::with_provider(provider.clone());
        let sources = setup
            .file_sources
            .into_iter()
            .map(|source| {
                let mut s = self.get_test_file_source(
                    &source.name,
                    None,
                    source.locales,
                    &source.path_scheme,
                );
                s.set_reporter(provider.clone());
                s
            })
            .collect();
        reg.register_sources(sources).unwrap();
        reg.set_bundle_adapter(bundle_adapter)
            .expect("Failed to set bundle adapter.");
        (provider, reg)
    }
}

#[async_trait(?Send)]
impl FileFetcher for TestFileFetcher {
    fn fetch_sync(&self, resource_id: &ResourceId) -> std::io::Result<String> {
        self.inner.fs.get_test_file_sync(&resource_id.value)
    }

    async fn fetch(&self, resource_id: &ResourceId) -> std::io::Result<String> {
        self.inner.fs.get_test_file_async(&resource_id.value).await
    }
}

pub enum ErrorStrategy {
    Panic,
    Report,
    Nothing,
}

pub struct InnerTestEnvironment {
    locales: Vec<LanguageIdentifier>,
    errors: Vec<L10nRegistryError>,
    error_strategy: ErrorStrategy,
}

#[derive(Clone)]
pub struct TestEnvironment {
    inner: Rc<RefCell<InnerTestEnvironment>>,
}

impl TestEnvironment {
    pub fn new(locales: Vec<LanguageIdentifier>) -> Self {
        Self {
            inner: Rc::new(RefCell::new(InnerTestEnvironment {
                locales,
                errors: vec![],
                error_strategy: ErrorStrategy::Report,
            })),
        }
    }

    pub fn set_locales(&self, locales: Vec<LanguageIdentifier>) {
        self.inner.borrow_mut().locales = locales;
    }

    pub fn errors(&self) -> Vec<L10nRegistryError> {
        self.inner.borrow().errors.clone()
    }

    pub fn clear_errors(&self) {
        self.inner.borrow_mut().errors.clear()
    }
}

impl LocalesProvider for TestEnvironment {
    type Iter = std::vec::IntoIter<LanguageIdentifier>;

    fn locales(&self) -> Self::Iter {
        self.inner.borrow().locales.clone().into_iter()
    }
}

impl ErrorReporter for TestEnvironment {
    fn report_errors(&self, errors: Vec<L10nRegistryError>) {
        match self.inner.borrow().error_strategy {
            ErrorStrategy::Panic => {
                panic!("Errors: {:#?}", errors);
            }
            ErrorStrategy::Report => {
                #[cfg(test)] // Don't let printing affect benchmarks
                eprintln!("Errors: {:#?}", errors);
            }
            ErrorStrategy::Nothing => {}
        }
        self.inner.borrow_mut().errors.extend(errors);
    }
}