summaryrefslogtreecommitdiffstats
path: root/tests/unit/jsParse.js
blob: 468138b12abfb415b40b3ef9e41be4ba7e64707d (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
/* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */

// Test cases for MessageTray URLification

const JsUnit = imports.jsUnit;

const Environment = imports.ui.environment;
Environment.init();

const JsParse = imports.misc.jsParse;

const HARNESS_COMMAND_HEADER = "let imports = obj;" +
                               "let global = obj;" +
                               "let Main = obj;" +
                               "let foo = obj;" +
                               "let r = obj;";

const testsFindMatchingQuote = [
    { input: '"double quotes"',
      output: 0 },
    { input: '\'single quotes\'',
      output: 0 },
    { input: 'some unquoted "some quoted"',
      output: 14 },
    { input: '"mixed \' quotes\'"',
      output: 0 },
    { input: '"escaped \\" quote"',
      output: 0 }
];
const testsFindMatchingSlash = [
    { input: '/slash/',
      output: 0 },
    { input: '/slash " with $ funny ^\' stuff/',
      output: 0  },
    { input: 'some unslashed /some slashed/',
      output: 15 },
    { input: '/escaped \\/ slash/',
      output: 0 }
];
const testsFindMatchingBrace = [
    { input: '[square brace]',
      output: 0 },
    { input: '(round brace)',
      output: 0  },
    { input: '([()][nesting!])',
      output: 0 },
    { input: '[we have "quoted [" braces]',
      output: 0 },
    { input: '[we have /regex [/ braces]',
      output: 0 },
    { input: '([[])[] mismatched braces ]',
      output: 1 }
];
const testsGetExpressionOffset = [
    { input: 'abc.123',
      output: 0 },
    { input: 'foo().bar',
      output: 0  },
    { input: 'foo(bar',
      output: 4 },
    { input: 'foo[abc.match(/"/)]',
      output: 0 }
];
const testsGetDeclaredConstants = [
    { input: 'const foo = X; const bar = Y;',
      output: ['foo', 'bar'] },
    { input: 'const foo=X; const bar=Y',
      output: ['foo', 'bar'] }
];
const testsIsUnsafeExpression = [
    { input: 'foo.bar',
      output: false },
    { input: 'foo[\'bar\']',
      output: false  },
    { input: 'foo["a=b=c".match(/=/)',
      output: false },
    { input: 'foo[1==2]',
      output: false },
    { input: '(x=4)',
      output: true },
    { input: '(x = 4)',
      output: true },
    { input: '(x;y)',
      output: true }
];
const testsModifyScope = [
    "foo['a",
    "foo()['b'",
    "obj.foo()('a', 1, 2, 'b')().",
    "foo.[.",
    "foo]]]()))].",
    "123'ab\"",
    "Main.foo.bar = 3; bar.",
    "(Main.foo = 3).",
    "Main[Main.foo+=-1]."
];



// Utility function for comparing arrays
function assertArrayEquals(errorMessage, array1, array2) {
    JsUnit.assertEquals(errorMessage + ' length',
                        array1.length, array2.length);
    for (let j = 0; j < array1.length; j++) {
        JsUnit.assertEquals(errorMessage + ' item ' + j,
                            array1[j], array2[j]);
    }
}

//
// Test javascript parsing
//

for (let i = 0; i < testsFindMatchingQuote.length; i++) {
    let text = testsFindMatchingQuote[i].input;
    let match = JsParse.findMatchingQuote(text, text.length - 1);

    JsUnit.assertEquals('Test testsFindMatchingQuote ' + i,
			match, testsFindMatchingQuote[i].output);
}

for (let i = 0; i < testsFindMatchingSlash.length; i++) {
    let text = testsFindMatchingSlash[i].input;
    let match = JsParse.findMatchingSlash(text, text.length - 1);

    JsUnit.assertEquals('Test testsFindMatchingSlash ' + i,
			match, testsFindMatchingSlash[i].output);
}

for (let i = 0; i < testsFindMatchingBrace.length; i++) {
    let text = testsFindMatchingBrace[i].input;
    let match = JsParse.findMatchingBrace(text, text.length - 1);

    JsUnit.assertEquals('Test testsFindMatchingBrace ' + i,
			match, testsFindMatchingBrace[i].output);
}

for (let i = 0; i < testsGetExpressionOffset.length; i++) {
    let text = testsGetExpressionOffset[i].input;
    let match = JsParse.getExpressionOffset(text, text.length - 1);

    JsUnit.assertEquals('Test testsGetExpressionOffset ' + i,
			match, testsGetExpressionOffset[i].output);
}

for (let i = 0; i < testsGetDeclaredConstants.length; i++) {
    let text = testsGetDeclaredConstants[i].input;
    let match = JsParse.getDeclaredConstants(text);

    assertArrayEquals('Test testsGetDeclaredConstants ' + i,
		      match, testsGetDeclaredConstants[i].output);
}

for (let i = 0; i < testsIsUnsafeExpression.length; i++) {
    let text = testsIsUnsafeExpression[i].input;
    let unsafe = JsParse.isUnsafeExpression(text);

    JsUnit.assertEquals('Test testsIsUnsafeExpression ' + i,
			unsafe, testsIsUnsafeExpression[i].output);
}

//
// Test safety of eval to get completions
//

for (let i = 0; i < testsModifyScope.length; i++) {
    let text = testsModifyScope[i];
    // We need to use var here for the with statement
    var obj = {};

    // Just as in JsParse.getCompletions, we will find the offset
    // of the expression, test whether it is unsafe, and then eval it.
    let offset = JsParse.getExpressionOffset(text, text.length - 1);
    if (offset >= 0) {
        text = text.slice(offset);

        let matches = text.match(/(.*)\.(.*)/);
        if (matches) {
            let [expr, base, attrHead] = matches;

            if (!JsParse.isUnsafeExpression(base)) {
                with (obj) {
                    try {
                        eval(HARNESS_COMMAND_HEADER + base);
                    } catch (e) {
                        JsUnit.assertNotEquals("Code '" + base + "' is valid code", e.constructor, SyntaxError);
                    }
                }
            }
        }
    }
    let propertyNames = Object.getOwnPropertyNames(obj);
    JsUnit.assertEquals("The context '" + JSON.stringify(obj) + "' was not modified", propertyNames.length, 0);
}