Skip to content

Commit f5f023b

Browse files
evilebottnawialexander-akait
authored andcommitted
test: simple_variable (glayzzle#395)
1 parent 0714ad6 commit f5f023b

File tree

3 files changed

+225
-59
lines changed

3 files changed

+225
-59
lines changed

src/parser/variable.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ module.exports = {
333333
break;
334334
}
335335
case "$": // $$$var
336-
// @fixme check coverage here
337336
result = result(this.read_simple_variable(), false);
338337
break;
339338
case this.tok.T_VARIABLE: {

test/snapshot/__snapshots__/variable.test.js.snap

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,137 @@ Program {
13261326
}
13271327
`;
13281328

1329+
exports[`Test variables simple variable #2 1`] = `
1330+
Program {
1331+
"children": Array [
1332+
ExpressionStatement {
1333+
"expression": Assign {
1334+
"kind": "assign",
1335+
"left": Variable {
1336+
"curly": false,
1337+
"kind": "variable",
1338+
"name": "var",
1339+
},
1340+
"operator": "=",
1341+
"right": Variable {
1342+
"curly": true,
1343+
"kind": "variable",
1344+
"name": Variable {
1345+
"curly": false,
1346+
"kind": "variable",
1347+
"name": "var",
1348+
},
1349+
},
1350+
},
1351+
"kind": "expressionstatement",
1352+
},
1353+
],
1354+
"errors": Array [],
1355+
"kind": "program",
1356+
}
1357+
`;
1358+
1359+
exports[`Test variables simple variable #3 1`] = `
1360+
Program {
1361+
"children": Array [
1362+
ExpressionStatement {
1363+
"expression": Assign {
1364+
"kind": "assign",
1365+
"left": Variable {
1366+
"curly": false,
1367+
"kind": "variable",
1368+
"name": "var",
1369+
},
1370+
"operator": "=",
1371+
"right": Variable {
1372+
"curly": true,
1373+
"kind": "variable",
1374+
"name": Bin {
1375+
"kind": "bin",
1376+
"left": Variable {
1377+
"curly": false,
1378+
"kind": "variable",
1379+
"name": "var",
1380+
},
1381+
"right": String {
1382+
"isDoubleQuote": false,
1383+
"kind": "string",
1384+
"raw": "'foo'",
1385+
"unicode": false,
1386+
"value": "foo",
1387+
},
1388+
"type": "+",
1389+
},
1390+
},
1391+
},
1392+
"kind": "expressionstatement",
1393+
},
1394+
],
1395+
"errors": Array [],
1396+
"kind": "program",
1397+
}
1398+
`;
1399+
1400+
exports[`Test variables simple variable #4 1`] = `
1401+
Program {
1402+
"children": Array [
1403+
ExpressionStatement {
1404+
"expression": Assign {
1405+
"kind": "assign",
1406+
"left": Variable {
1407+
"curly": false,
1408+
"kind": "variable",
1409+
"name": "var",
1410+
},
1411+
"operator": "=",
1412+
"right": Variable {
1413+
"curly": false,
1414+
"kind": "variable",
1415+
"name": Variable {
1416+
"curly": false,
1417+
"kind": "variable",
1418+
"name": Variable {
1419+
"curly": false,
1420+
"kind": "variable",
1421+
"name": "var",
1422+
},
1423+
},
1424+
},
1425+
},
1426+
"kind": "expressionstatement",
1427+
},
1428+
],
1429+
"errors": Array [],
1430+
"kind": "program",
1431+
}
1432+
`;
1433+
1434+
exports[`Test variables simple variable 1`] = `
1435+
Program {
1436+
"children": Array [
1437+
ExpressionStatement {
1438+
"expression": Assign {
1439+
"kind": "assign",
1440+
"left": Variable {
1441+
"curly": false,
1442+
"kind": "variable",
1443+
"name": "var",
1444+
},
1445+
"operator": "=",
1446+
"right": Variable {
1447+
"curly": false,
1448+
"kind": "variable",
1449+
"name": "var",
1450+
},
1451+
},
1452+
"kind": "expressionstatement",
1453+
},
1454+
],
1455+
"errors": Array [],
1456+
"kind": "program",
1457+
}
1458+
`;
1459+
13291460
exports[`Test variables valid offset lookup 1`] = `
13301461
Program {
13311462
"children": Array [

test/snapshot/variable.test.js

Lines changed: 94 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,182 @@
1-
const parser = require('../main');
1+
const parser = require("../main");
22

33
describe("Test variables", function() {
44
it("fix 253 - can't be parsed `global` with multiple `$`", function() {
5-
expect(parser.parseEval(`
5+
expect(
6+
parser.parseEval(`
67
global $$foo;
7-
`
8-
)).toMatchSnapshot();
8+
`)
9+
).toMatchSnapshot();
910
});
1011

1112
it("fix 248 - broken ast for `$$$$$`", function() {
12-
expect(parser.parseEval(`
13+
expect(
14+
parser.parseEval(`
1315
$foo::$$property;
1416
$foo::\${$property};
1517
$bar->$$property;
1618
$bar->\${$property};
17-
`
18-
)).toMatchSnapshot();
19+
`)
20+
).toMatchSnapshot();
1921
});
20-
22+
2123
it("fix 248 - test curly", function() {
22-
expect(parser.parseEval(`
24+
expect(
25+
parser.parseEval(`
2326
$bar->{$property->foo};
2427
$bar->\${$property};
2528
$bar->foo_{$property};
26-
`
27-
)).toMatchSnapshot();
29+
`)
30+
).toMatchSnapshot();
2831
});
2932

30-
3133
it("array destructuring", function() {
3234
expect(parser.parseEval("[$id1, $name1] = $data[0];")).toMatchSnapshot();
3335
});
3436

3537
it("default variables", function() {
36-
expect(parser.parseEval(`
38+
expect(
39+
parser.parseEval(`
3740
$a = "foo";
3841
$b = &$c;
39-
$a->b = true;`
40-
)).toMatchSnapshot();
42+
$a->b = true;`)
43+
).toMatchSnapshot();
4144
});
4245

4346
it("Variable chains", function() {
4447
expect(parser.parseEval("foo::$a[1][2];")).toMatchSnapshot();
4548
});
4649

4750
it("fix #167", function() {
48-
expect(parser.parseEval("$var = Foo::{$bar['baz']}();Foo::$bar['baz']();")).toMatchSnapshot();
51+
expect(
52+
parser.parseEval("$var = Foo::{$bar['baz']}();Foo::$bar['baz']();")
53+
).toMatchSnapshot();
4954
});
5055

5156
it("valid offset lookup", function() {
52-
expect(parser.parseEval("get_class($var)::$$$$$property;")).toMatchSnapshot();
57+
expect(
58+
parser.parseEval("get_class($var)::$$$$$property;")
59+
).toMatchSnapshot();
5360
});
5461

55-
it('fix #185', function() {
56-
expect(parser.parseEval(`
62+
it("fix #185", function() {
63+
expect(
64+
parser.parseEval(`
5765
$var = ($var[0])::foo;
5866
$var = ($var[0][1])::foo;
5967
$var = ($var[0])[1]::foo;
6068
$var = (($var[0])[1])::foo;
6169
$var = (new Foo())::bar;
6270
get_class($this->resource)::$wrap;
63-
`)).toMatchSnapshot();
64-
71+
`)
72+
).toMatchSnapshot();
6573
});
6674

67-
6875
it("Class constants", function() {
69-
expect(parser.parseEval(`
76+
expect(
77+
parser.parseEval(`
7078
static::foo();
7179
self::foo();
7280
parent::foo();
7381
foo::class;
7482
$this->foo();
7583
foo::$bar;
76-
$this->foo::bar["baz"]::qux();`
77-
)).toMatchSnapshot();
84+
$this->foo::bar["baz"]::qux();`)
85+
).toMatchSnapshot();
7886
});
7987

8088
it("Encaps var offset", function() {
81-
expect(parser.parseEval(`
89+
expect(
90+
parser.parseEval(`
8291
$a = "{$a[1]}";
8392
$a = "{$a["a"]}";
84-
$a = "{$a[$b]}";`
85-
)).toMatchSnapshot();
93+
$a = "{$a[$b]}";`)
94+
).toMatchSnapshot();
8695
});
8796

8897
it("Chained encapsed vars", function() {
89-
expect(parser.parseEval(
90-
`
98+
expect(
99+
parser.parseEval(
100+
`
91101
$a = "{$a->foo()[$bar[$foo]]}";
92102
`
93-
)).toMatchSnapshot();
103+
)
104+
).toMatchSnapshot();
94105
});
95106

96107
it("Dynamic variables", function() {
97-
expect(parser.parseEval(`
108+
expect(
109+
parser.parseEval(`
98110
$$a = "bar";
99111
$$$a = "bar";
100112
\${$a."bar"} = "bar";
101113
$foo{$a."bar"} = "bar";
102-
`)).toMatchSnapshot();
114+
`)
115+
).toMatchSnapshot();
103116
});
104117

105118
describe("Check errors", function() {
106119
it("should be ?", function() {
107-
expect(parser.parseEval("$? = true;", {
108-
parser: {
109-
suppressErrors: true
110-
}
111-
})).toMatchSnapshot();
120+
expect(
121+
parser.parseEval("$? = true;", {
122+
parser: {
123+
suppressErrors: true
124+
}
125+
})
126+
).toMatchSnapshot();
112127
});
113128

114129
it("should fail on double static lookup", function() {
115-
expect(parser.parseEval(["this->foo::bar::baz;"].join("\n"), {
116-
parser: {
117-
suppressErrors: true
118-
}
119-
})).toMatchSnapshot();
130+
expect(
131+
parser.parseEval(["this->foo::bar::baz;"].join("\n"), {
132+
parser: {
133+
suppressErrors: true
134+
}
135+
})
136+
).toMatchSnapshot();
120137
});
121138

122139
it("should fail on property lookup on static lookup", function() {
123-
expect(parser.parseEval(["$this->foo::bar->baz;"].join("\n"), {
124-
parser: {
125-
suppressErrors: true
126-
}
127-
})).toMatchSnapshot();
140+
expect(
141+
parser.parseEval(["$this->foo::bar->baz;"].join("\n"), {
142+
parser: {
143+
suppressErrors: true
144+
}
145+
})
146+
).toMatchSnapshot();
128147
});
129148

130149
it("should fail $foo->bar::!", function() {
131-
expect(parser.parseEval("$foo->bar::!", {
132-
parser: {
133-
suppressErrors: true
134-
}
135-
})).toMatchSnapshot();
150+
expect(
151+
parser.parseEval("$foo->bar::!", {
152+
parser: {
153+
suppressErrors: true
154+
}
155+
})
156+
).toMatchSnapshot();
136157
});
137158

138159
it("should fail foo::bar::baz", function() {
139-
expect(parser.parseEval("foo::bar::baz", {
140-
parser: {
141-
suppressErrors: true
142-
}
143-
})).toMatchSnapshot();
160+
expect(
161+
parser.parseEval("foo::bar::baz", {
162+
parser: {
163+
suppressErrors: true
164+
}
165+
})
166+
).toMatchSnapshot();
144167
});
145168
});
169+
170+
it("simple variable", function() {
171+
expect(parser.parseEval("$var = $var;")).toMatchSnapshot();
172+
});
173+
it("simple variable #2", function() {
174+
expect(parser.parseEval("$var = ${$var};")).toMatchSnapshot();
175+
});
176+
it("simple variable #3", function() {
177+
expect(parser.parseEval("$var = ${$var + 'foo'};")).toMatchSnapshot();
178+
});
179+
it("simple variable #4", function() {
180+
expect(parser.parseEval("$var = $$$var;")).toMatchSnapshot();
181+
});
146182
});

0 commit comments

Comments
 (0)