File: lib.rs

package info (click to toggle)
rust-gettext 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 208 kB
  • sloc: makefile: 11
file content (121 lines) | stat: -rw-r--r-- 3,615 bytes parent folder | download
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
use encoding::label::encoding_from_whatwg_label;
use gettext::{Catalog, ParseOptions};

use std::fs::File;

#[test]
fn test_integration() {
    let f = File::open("test_cases/integration.mo").unwrap();
    let catalog = Catalog::parse(f).unwrap();

    assert_eq!(catalog.gettext("non-existent"), "non-existent");
    assert_eq!(catalog.gettext("existent"), "egzistuojantis");

    assert_eq!(
        catalog.ngettext("a bad string", "bad strings", 1),
        "a bad string"
    );
    assert_eq!(
        catalog.ngettext("a bad string", "bad strings", 2),
        "bad strings"
    );
    assert_eq!(
        catalog.ngettext("a good string", "good strings", 1),
        "gera eilute"
    );
    assert_eq!(
        catalog.ngettext("a good string", "good strings", 2),
        "geros eilutes"
    );

    assert_eq!(catalog.pgettext("ctxt", "non-existent"), "non-existent");
    assert_eq!(
        catalog.pgettext("ctxt", "existent"),
        "egzistuojantis kontekste"
    );

    assert_eq!(
        catalog.npgettext("ctxt", "a bad string", "bad strings", 1),
        "a bad string"
    );
    assert_eq!(
        catalog.npgettext("ctxt", "a bad string", "bad strings", 2),
        "bad strings"
    );
    assert_eq!(
        catalog.npgettext("ctxt", "a good string", "good strings", 1),
        "gera eilute kontekste"
    );
    assert_eq!(
        catalog.npgettext("ctxt", "a good string", "good strings", 2),
        "geros eilutes kontekste"
    );
}

#[test]
fn test_cp1257() {
    // cp1257_meta
    {
        let reader: &[u8] = include_bytes!("../test_cases/cp1257_meta.mo");
        let catalog = ParseOptions::new().parse(reader).unwrap();
        assert_eq!(catalog.gettext("Garlic"), "Česnakas");
    }

    // cp1257_forced
    {
        let reader: &[u8] = include_bytes!("../test_cases/cp1257_forced.mo");
        for enc_name in &["cp1257", "windows-1257", "x-cp1257"] {
            let encoding = encoding_from_whatwg_label(enc_name).unwrap();
            let catalog = ParseOptions::new()
                .force_encoding(encoding)
                .parse(reader)
                .unwrap();
            assert_eq!(catalog.gettext("Garlic"), "Česnakas");
        }
    }
}

#[test]
fn test_lt_plural() {
    fn lithuanian_plural(n: u64) -> usize {
        if (n % 10) == 1 && (n % 100) != 11 {
            0
        } else if ((n % 10) >= 2) && ((n % 100) < 10 || (n % 100) >= 20) {
            1
        } else {
            2
        }
    }

    // lt_plural_forced
    {
        let reader: &[u8] = include_bytes!("../test_cases/lt_plural_forced.mo");
        let cat = ParseOptions::new()
            .force_plural(lithuanian_plural)
            .parse(reader)
            .unwrap();

        assert_eq!(cat.ngettext("Garlic", "Garlics", 0), "Česnakų");
        assert_eq!(cat.ngettext("Garlic", "Garlics", 1), "Česnakas");
        for i in 2..9 {
            assert_eq!(cat.ngettext("Garlic", "Garlics", i), "Česnakai");
        }
        for i in 10..20 {
            assert_eq!(cat.ngettext("Garlic", "Garlics", i), "Česnakų");
        }
        assert_eq!(cat.ngettext("Garlic", "Garlics", 21), "Česnakas");
    }
}

#[test]
fn test_complex_plural() {
    let reader: &[u8] = include_bytes!("../test_cases/complex_plural.mo");
    let cat = ParseOptions::new().parse(reader).unwrap();

    assert_eq!(cat.ngettext("Test", "Tests", 0), "Plural 2");
    assert_eq!(cat.ngettext("Test", "Tests", 1), "Singular");
    assert_eq!(cat.ngettext("Test", "Tests", 2), "Plural 1");
    for i in 3..20 {
        assert_eq!(cat.ngettext("Test", "Tests", i), "Plural 2");
    }
}