Skip to content

Commit a874732

Browse files
committed
enable new tests
1 parent 78ca985 commit a874732

File tree

3 files changed

+150
-48
lines changed

3 files changed

+150
-48
lines changed

test/astTests.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,6 @@ describe('Test AST structure', function() {
99
ast.children.length.should.be.exactly(0);
1010
});
1111

12-
it('test withSource options', function() {
13-
var ast = parser.parseEval('$a = true;', {
14-
ast: {
15-
withSource: true
16-
}
17-
});
18-
// @todo
19-
});
20-
it('test withPositions options', function() {
21-
var ast = parser.parseEval('$a = true;', {
22-
ast: {
23-
withPositions: true
24-
}
25-
});
26-
// @todo
27-
});
28-
2912
it('test inline', function() {
3013
var ast = parser.parseCode('Hello <?php echo "World"; ?> !');
3114
ast.children[0].kind.should.be.exactly('inline');

test/gracefulTests.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var should = require("should");
22
var parser = require('../src/index');
33

4-
/*describe('Test graceful mode', function() {
4+
describe('Test graceful mode', function() {
55

66
describe('to suppress errors', function() {
77

@@ -21,50 +21,55 @@ var parser = require('../src/index');
2121
'}', // 4.
2222
'}' // 5. <-- extra '}' token here
2323
].join('\n'));
24-
ast[2].length.should.be.exactly(2);
25-
ast[1][0][2][5][0][2][0].should.be.exactly('error');
24+
ast.errors.length.should.be.exactly(2);
25+
// @todo ast[1][0][2][5][0][2][0].should.be.exactly('error');
2626
});
2727

2828
it('test expr', function () {
2929
var ast = test.parseEval('$a = $b -; $foo = $a;');
3030

31-
ast[2].length.should.be.exactly(2);
32-
ast[1].length.should.be.exactly(2);
31+
ast.errors.length.should.be.exactly(2);
32+
ast.children.length.should.be.exactly(2);
3333

34+
/** @todo
3435
ast[1][0][2][0].should.be.exactly('bin');
3536
ast[1][0][2][1].should.be.exactly('-');
3637
ast[1][0][2][3][0].should.be.exactly('error');
3738
3839
ast[1][1][0].should.be.exactly('set');
3940
ast[1][1][1][0].should.be.exactly('var');
4041
ast[1][1][1][1].should.be.exactly('$foo');
42+
*/
4143
});
4244

4345
it('test class', function () {
4446
var ast = test.parseEval('class foo { foo const A = 1 ');
4547

46-
ast[2].length.should.be.exactly(3);
47-
ast[1].length.should.be.exactly(1);
48+
ast.errors.length.should.be.exactly(3);
49+
ast.children.length.should.be.exactly(1);
4850

51+
/** @todo
4952
ast[1][0][0].should.be.exactly('class');
5053
ast[1][0][1].should.be.exactly('foo');
5154
ast[1][0][5].length.should.be.exactly(2); // including the foo error
5255
ast[1][0][5][0][0].should.be.exactly('error');
5356
ast[1][0][5][1][0].should.be.exactly('const');
5457
ast[1][0][5][1][1].should.be.exactly('A');
5558
ast[1][0][5][1][2][1].should.be.exactly('1');
59+
*/
5660

5761
});
5862

5963
it('test flags', function () {
60-
var ast = test.parseEval('final final interface foo { abstract function func() ');
61-
ast[2].length.should.be.exactly(4);
62-
ast[1].length.should.be.exactly(2);
63-
ast[1][0][0].should.be.exactly('error');
64-
ast[1][1][0].should.be.exactly('interface');
64+
var ast = test.parseEval([
65+
'final final interface foo {',
66+
' abstract function func() '
67+
].join('\n'));
68+
ast.errors.length.should.be.exactly(4);
69+
ast.children.length.should.be.exactly(1);
70+
ast.children[0].kind.should.be.exactly('interface');
6571
});
6672

6773
});
6874

6975
});
70-
*/

test/locationTests.js

Lines changed: 132 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,141 @@
11
var should = require("should");
22
var parser = require('../src/index');
33

4-
/*describe('Test offsets', function() {
4+
describe('Test offsets', function() {
55

6-
describe('to check offsets', function() {
6+
// init a new parser instance
7+
var test = parser.create({
8+
ast: {
9+
withPositions: true,
10+
withSource: true
11+
},
12+
parser: {
13+
extractDoc: true,
14+
debug: true
15+
},
16+
lexer: {
17+
debug: true
18+
}
19+
});
20+
21+
var lines = [
22+
'// a comment',
23+
'echo "string";',
24+
'$a = true;'
25+
];
26+
var text = lines.join('\r\n');
27+
var ast = test.parseEval(text);
28+
29+
describe('to program node', function() {
30+
it('test line', function() {
31+
ast.loc.start.line.should.be.exactly(1);
32+
ast.loc.end.line.should.be.exactly(lines.length);
33+
});
34+
it('test column', function() {
35+
ast.loc.start.column.should.be.exactly(0);
36+
ast.loc.end.column.should.be.exactly(
37+
lines[lines.length - 1].length
38+
);
39+
});
40+
it('test offsets', function() {
41+
ast.loc.start.offset.should.be.exactly(0);
42+
ast.loc.end.offset.should.be.exactly(text.length);
43+
});
44+
});
745

8-
// init a new parser instance
9-
var text = [
10-
' // a comment ',
11-
'echo "string";'
12-
].join('\r\n');
13-
var ast = parser.parseEval(
14-
text,
15-
{
16-
parser: {
17-
locations: true
18-
}
19-
}
20-
);
21-
/** @fixme should test & fix offsets **-/
22-
// console.log(ast[1]);
46+
describe('to comment node', function() {
47+
it('test line', function() {
48+
ast.children[0].loc.start.line.should.be.exactly(1);
49+
ast.children[0].loc.end.line.should.be.exactly(2);
50+
});
51+
it('test column', function() {
52+
ast.children[0].loc.start.column.should.be.exactly(0);
53+
ast.children[0].loc.end.column.should.be.exactly(0);
54+
});
55+
it('test offsets', function() {
56+
ast.children[0].loc.start.offset.should.be.exactly(0);
57+
ast.children[0].loc.end.offset.should.be.exactly(
58+
lines[0].length + 2 // first line + \r\n
59+
);
60+
ast.children[0].loc.source.should.be.exactly(lines[0] + '\r\n');
61+
});
62+
});
63+
64+
describe('on echo node', function() {
65+
it('test line', function() {
66+
ast.children[1].loc.start.line.should.be.exactly(2);
67+
ast.children[1].loc.end.line.should.be.exactly(2);
68+
});
69+
it('test column', function() {
70+
ast.children[1].loc.start.column.should.be.exactly(0);
71+
ast.children[1].loc.end.column.should.be.exactly(14);
72+
});
73+
it('test offsets', function() {
74+
ast.children[1].loc.start.offset.should.be.exactly(14);
75+
ast.children[1].loc.end.offset.should.be.exactly(28);
76+
ast.children[1].loc.source.should.be.exactly(lines[1]);
77+
});
78+
});
79+
80+
describe('on text node', function() {
81+
var arg = ast.children[1].arguments[0];
82+
it('test line', function() {
83+
arg.loc.start.line.should.be.exactly(2);
84+
arg.loc.end.line.should.be.exactly(2);
85+
});
86+
it('test column', function() {
87+
arg.loc.start.column.should.be.exactly(5);
88+
arg.loc.end.column.should.be.exactly(13);
89+
});
90+
it('test offsets', function() {
91+
arg.loc.source.should.be.exactly('"string"');
92+
});
93+
});
94+
95+
describe('on assign node', function() {
96+
it('test line', function() {
97+
ast.children[2].loc.start.line.should.be.exactly(3);
98+
ast.children[2].loc.end.line.should.be.exactly(3);
99+
});
100+
it('test column', function() {
101+
ast.children[2].loc.start.column.should.be.exactly(0);
102+
// ignored ';' because it was eaten by expr and assign
103+
// was retrieved by expr_item without it
104+
ast.children[2].loc.end.column.should.be.exactly(9);
105+
});
106+
it('test offsets', function() {
107+
ast.children[2].loc.source.should.be.exactly(lines[2].substring(0, 9));
108+
});
109+
});
110+
111+
describe('on variable node', function() {
112+
var node = ast.children[2].left;
113+
it('test line', function() {
114+
node.loc.start.line.should.be.exactly(3);
115+
node.loc.end.line.should.be.exactly(3);
116+
});
117+
it('test column', function() {
118+
node.loc.start.column.should.be.exactly(0);
119+
node.loc.end.column.should.be.exactly(2);
120+
});
121+
it('test offsets', function() {
122+
node.loc.source.should.be.exactly('$a');
123+
});
124+
});
23125

126+
describe('on boolean node', function() {
127+
var node = ast.children[2].right;
128+
it('test line', function() {
129+
node.loc.start.line.should.be.exactly(3);
130+
node.loc.end.line.should.be.exactly(3);
131+
});
132+
it('test column', function() {
133+
node.loc.start.column.should.be.exactly(5);
134+
node.loc.end.column.should.be.exactly(9);
135+
});
136+
it('test offsets', function() {
137+
node.loc.source.should.be.exactly('true');
138+
});
24139
});
25140

26141
});
27-
*/

0 commit comments

Comments
 (0)