summaryrefslogtreecommitdiffstats
path: root/intl/l10n/rust/l10nregistry-rs/tests/scenarios_sync.rs
blob: 280b32e927c6e2b0168a1fa143108882e74605df (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
use fluent_bundle::FluentArgs;
use fluent_fallback::Localization;
use fluent_testing::get_scenarios;
use l10nregistry::fluent::FluentBundle;
use l10nregistry::registry::BundleAdapter;
use l10nregistry::testing::{RegistrySetup, TestFileFetcher};

#[derive(Clone)]
struct ScenarioBundleAdapter {}

impl BundleAdapter for ScenarioBundleAdapter {
    fn adapt_bundle(&self, bundle: &mut FluentBundle) {
        bundle.set_use_isolating(false);
        bundle
            .add_function("PLATFORM", |_positional, _named| "linux".into())
            .expect("Failed to add a function to the bundle.");
    }
}

#[test]
fn scenarios_sync() {
    use fluent_testing::scenarios::structs::Scenario;
    let fetcher = TestFileFetcher::new();

    let scenarios = get_scenarios();

    let adapter = ScenarioBundleAdapter {};

    let cannot_produce_bundle = |scenario: &Scenario| {
        scenario
            .queries
            .iter()
            .any(|query| query.exceptional_context.blocks_bundle_generation())
    };

    for scenario in scenarios {
        println!("scenario: {}", scenario.name);
        let setup: RegistrySetup = (&scenario).into();
        let (env, reg) = fetcher.get_registry_and_environment_with_adapter(setup, adapter.clone());

        let loc = Localization::with_env(scenario.res_ids.clone(), true, env.clone(), reg);
        let bundles = loc.bundles();
        let no_bundles = cannot_produce_bundle(&scenario);

        let mut errors = vec![];

        for query in scenario.queries.iter() {
            let errors_start_len = errors.len();

            let args = query.input.args.as_ref().map(|args| {
                let mut result = FluentArgs::new();
                for arg in args.as_slice() {
                    result.set(arg.id.clone(), arg.value.clone());
                }
                result
            });

            if let Some(output) = &query.output {
                if let Some(value) = &output.value {
                    let v = bundles.format_value_sync(&query.input.id, args.as_ref(), &mut errors);
                    if no_bundles || query.exceptional_context.causes_failed_value_lookup() {
                        assert!(v.is_err() || v.unwrap().is_none());
                        if no_bundles {
                            continue;
                        }
                    } else {
                        assert_eq!(v.unwrap().unwrap(), value.as_str())
                    }
                }
            }

            if query.exceptional_context.causes_reported_format_error() {
                assert!(
                    errors.len() > errors_start_len,
                    "expected reported errors for query {:#?}",
                    query
                );
            } else {
                assert_eq!(
                    errors.len(),
                    errors_start_len,
                    "expected no reported errors for query {:#?}",
                    query
                );
            }
        }

        if scenario
            .queries
            .iter()
            .any(|query| query.exceptional_context.missing_required_resource())
        {
            assert!(
                !env.errors().is_empty(),
                "expected errors for scenario {{ {} }}, but found none",
                scenario.name
            );
        } else {
            assert!(
                env.errors().is_empty(),
                "expected no errors for scenario {{ {} }}, but found {:#?}",
                scenario.name,
                env.errors()
            );
        }
    }
}