summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/rust/mozprofile/src/lib.rs
blob: 29aee0bf2ae8b5c6f2b330cbbe5c58c47caf7141 (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
#![forbid(unsafe_code)]
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

extern crate tempfile;

pub mod preferences;
pub mod prefreader;
pub mod profile;

#[cfg(test)]
mod test {
    //    use std::fs::File;
    //    use profile::Profile;
    use crate::preferences::Pref;
    use crate::prefreader::{parse, serialize, tokenize};
    use crate::prefreader::{Position, PrefToken};
    use std::collections::BTreeMap;
    use std::io::Cursor;
    use std::str;

    #[test]
    fn tokenize_simple() {
        let prefs = "  user_pref ( 'example.pref.string', 'value' )   ;\n \
                     pref(\"example.pref.int\", -123); sticky_pref('example.pref.bool',false);";

        let p = Position::new();

        let expected = vec![
            PrefToken::UserPrefFunction(p),
            PrefToken::Paren('(', p),
            PrefToken::String("example.pref.string".into(), p),
            PrefToken::Comma(p),
            PrefToken::String("value".into(), p),
            PrefToken::Paren(')', p),
            PrefToken::Semicolon(p),
            PrefToken::PrefFunction(p),
            PrefToken::Paren('(', p),
            PrefToken::String("example.pref.int".into(), p),
            PrefToken::Comma(p),
            PrefToken::Int(-123, p),
            PrefToken::Paren(')', p),
            PrefToken::Semicolon(p),
            PrefToken::StickyPrefFunction(p),
            PrefToken::Paren('(', p),
            PrefToken::String("example.pref.bool".into(), p),
            PrefToken::Comma(p),
            PrefToken::Bool(false, p),
            PrefToken::Paren(')', p),
            PrefToken::Semicolon(p),
        ];

        tokenize_test(prefs, &expected);
    }

    #[test]
    fn tokenize_comments() {
        let prefs = "# bash style comment\n /*block comment*/ user_pref/*block comment*/(/*block \
                     comment*/ 'example.pref.string'  /*block comment*/,/*block comment*/ \
                     'value'/*block comment*/ )// line comment";

        let p = Position::new();

        let expected = vec![
            PrefToken::CommentBashLine(" bash style comment".into(), p),
            PrefToken::CommentBlock("block comment".into(), p),
            PrefToken::UserPrefFunction(p),
            PrefToken::CommentBlock("block comment".into(), p),
            PrefToken::Paren('(', p),
            PrefToken::CommentBlock("block comment".into(), p),
            PrefToken::String("example.pref.string".into(), p),
            PrefToken::CommentBlock("block comment".into(), p),
            PrefToken::Comma(p),
            PrefToken::CommentBlock("block comment".into(), p),
            PrefToken::String("value".into(), p),
            PrefToken::CommentBlock("block comment".into(), p),
            PrefToken::Paren(')', p),
            PrefToken::CommentLine(" line comment".into(), p),
        ];

        tokenize_test(prefs, &expected);
    }

    #[test]
    fn tokenize_escapes() {
        let prefs = r#"user_pref('example\x20pref', "\u0020\u2603\uD800\uDC96\"\'\n\r\\\w)"#;

        let p = Position::new();

        let expected = vec![
            PrefToken::UserPrefFunction(p),
            PrefToken::Paren('(', p),
            PrefToken::String("example pref".into(), p),
            PrefToken::Comma(p),
            PrefToken::String(" ☃𐂖\"'\n\r\\\\w".into(), p),
            PrefToken::Paren(')', p),
        ];

        tokenize_test(prefs, &expected);
    }

    fn tokenize_test(prefs: &str, expected: &[PrefToken]) {
        println!("{}\n", prefs);

        for (e, a) in expected.iter().zip(tokenize(prefs.as_bytes())) {
            let success = match (e, &a) {
                (&PrefToken::PrefFunction(_), &PrefToken::PrefFunction(_)) => true,
                (&PrefToken::UserPrefFunction(_), &PrefToken::UserPrefFunction(_)) => true,
                (&PrefToken::StickyPrefFunction(_), &PrefToken::StickyPrefFunction(_)) => true,
                (
                    &PrefToken::CommentBlock(ref data_e, _),
                    &PrefToken::CommentBlock(ref data_a, _),
                ) => data_e == data_a,
                (
                    &PrefToken::CommentLine(ref data_e, _),
                    &PrefToken::CommentLine(ref data_a, _),
                ) => data_e == data_a,
                (
                    &PrefToken::CommentBashLine(ref data_e, _),
                    &PrefToken::CommentBashLine(ref data_a, _),
                ) => data_e == data_a,
                (&PrefToken::Paren(data_e, _), &PrefToken::Paren(data_a, _)) => data_e == data_a,
                (&PrefToken::Semicolon(_), &PrefToken::Semicolon(_)) => true,
                (&PrefToken::Comma(_), &PrefToken::Comma(_)) => true,
                (&PrefToken::String(ref data_e, _), &PrefToken::String(ref data_a, _)) => {
                    data_e == data_a
                }
                (&PrefToken::Int(data_e, _), &PrefToken::Int(data_a, _)) => data_e == data_a,
                (&PrefToken::Bool(data_e, _), &PrefToken::Bool(data_a, _)) => data_e == data_a,
                (&PrefToken::Error(data_e, _), &PrefToken::Error(data_a, _)) => data_e == data_a,
                (_, _) => false,
            };
            if !success {
                println!("Expected {:?}, got {:?}", e, a);
            }
            assert!(success);
        }
    }

    #[test]
    fn parse_simple() {
        let input = "  user_pref /* block comment */ ( 'example.pref.string', 'value' )   ;\n \
                     pref(\"example.pref.int\", -123); sticky_pref('example.pref.bool',false)";

        let mut expected: BTreeMap<String, Pref> = BTreeMap::new();
        expected.insert("example.pref.string".into(), Pref::new("value"));
        expected.insert("example.pref.int".into(), Pref::new(-123));
        expected.insert("example.pref.bool".into(), Pref::new_sticky(false));

        parse_test(input, expected);
    }

    #[test]
    fn parse_escape() {
        let input = r#"user_pref('example\\pref\"string', 'val\x20ue' )"#;

        let mut expected: BTreeMap<String, Pref> = BTreeMap::new();
        expected.insert("example\\pref\"string".into(), Pref::new("val ue"));

        parse_test(input, expected);
    }

    fn parse_test(input: &str, expected: BTreeMap<String, Pref>) {
        match parse(input.as_bytes()) {
            Ok(ref actual) => {
                println!("Expected:\n{:?}\nActual\n{:?}", expected, actual);
                assert_eq!(actual, &expected);
            }
            Err(e) => {
                println!("{}", e);
                assert!(false)
            }
        }
    }

    #[test]
    fn serialize_simple() {
        let input = "  user_pref /* block comment */ ( 'example.pref.string', 'value' )   ;\n \
                     pref(\"example.pref.int\", -123); sticky_pref('example.pref.bool',false)";
        let expected = "sticky_pref(\"example.pref.bool\", false);
user_pref(\"example.pref.int\", -123);
user_pref(\"example.pref.string\", \"value\");\n";

        serialize_test(input, expected);
    }

    #[test]
    fn serialize_quotes() {
        let input = r#"user_pref('example\\with"quotes"', '"Value"')"#;
        let expected = r#"user_pref("example\\with\"quotes\"", "\"Value\"");
"#;

        serialize_test(input, expected);
    }

    fn serialize_test(input: &str, expected: &str) {
        let buf = Vec::with_capacity(expected.len());
        let mut out = Cursor::new(buf);
        serialize(&parse(input.as_bytes()).unwrap(), &mut out).unwrap();
        let data = out.into_inner();
        let actual = str::from_utf8(&*data).unwrap();
        println!("Expected:\n{:?}\nActual\n{:?}", expected, actual);
        assert_eq!(actual, expected);
    }
}