forked from glayzzle/php-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocationTests.js
More file actions
141 lines (130 loc) · 4.16 KB
/
locationTests.js
File metadata and controls
141 lines (130 loc) · 4.16 KB
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
var should = require("should");
var parser = require('../src/index');
describe('Test offsets', function() {
// init a new parser instance
var test = parser.create({
ast: {
withPositions: true,
withSource: true
},
parser: {
extractDoc: true,
// debug: true
},
lexer: {
// debug: true
}
});
var lines = [
'// a comment',
'echo "string";',
'$a = true;'
];
var text = lines.join('\r\n');
var ast = test.parseEval(text);
describe('to program node', function() {
it('test line', function() {
ast.loc.start.line.should.be.exactly(1);
ast.loc.end.line.should.be.exactly(lines.length);
});
it('test column', function() {
ast.loc.start.column.should.be.exactly(0);
ast.loc.end.column.should.be.exactly(
lines[lines.length - 1].length
);
});
it('test offsets', function() {
ast.loc.start.offset.should.be.exactly(0);
ast.loc.end.offset.should.be.exactly(text.length);
});
});
describe('to comment node', function() {
it('test line', function() {
ast.children[0].loc.start.line.should.be.exactly(1);
ast.children[0].loc.end.line.should.be.exactly(2);
});
it('test column', function() {
ast.children[0].loc.start.column.should.be.exactly(0);
ast.children[0].loc.end.column.should.be.exactly(0);
});
it('test offsets', function() {
ast.children[0].loc.start.offset.should.be.exactly(0);
ast.children[0].loc.end.offset.should.be.exactly(
lines[0].length + 2 // first line + \r\n
);
ast.children[0].loc.source.should.be.exactly(lines[0] + '\r\n');
});
});
describe('on echo node', function() {
it('test line', function() {
ast.children[1].loc.start.line.should.be.exactly(2);
ast.children[1].loc.end.line.should.be.exactly(2);
});
it('test column', function() {
ast.children[1].loc.start.column.should.be.exactly(0);
ast.children[1].loc.end.column.should.be.exactly(14);
});
it('test offsets', function() {
ast.children[1].loc.start.offset.should.be.exactly(14);
ast.children[1].loc.end.offset.should.be.exactly(28);
ast.children[1].loc.source.should.be.exactly(lines[1]);
});
});
describe('on text node', function() {
var arg = ast.children[1].arguments[0];
it('test line', function() {
arg.loc.start.line.should.be.exactly(2);
arg.loc.end.line.should.be.exactly(2);
});
it('test column', function() {
arg.loc.start.column.should.be.exactly(5);
arg.loc.end.column.should.be.exactly(13);
});
it('test offsets', function() {
arg.loc.source.should.be.exactly('"string"');
});
});
describe('on assign node', function() {
it('test line', function() {
ast.children[2].loc.start.line.should.be.exactly(3);
ast.children[2].loc.end.line.should.be.exactly(3);
});
it('test column', function() {
ast.children[2].loc.start.column.should.be.exactly(0);
// ignored ';' because it was eaten by expr and assign
// was retrieved by expr_item without it
ast.children[2].loc.end.column.should.be.exactly(9);
});
it('test offsets', function() {
ast.children[2].loc.source.should.be.exactly(lines[2].substring(0, 9));
});
});
describe('on variable node', function() {
var node = ast.children[2].left;
it('test line', function() {
node.loc.start.line.should.be.exactly(3);
node.loc.end.line.should.be.exactly(3);
});
it('test column', function() {
node.loc.start.column.should.be.exactly(0);
node.loc.end.column.should.be.exactly(2);
});
it('test offsets', function() {
node.loc.source.should.be.exactly('$a');
});
});
describe('on boolean node', function() {
var node = ast.children[2].right;
it('test line', function() {
node.loc.start.line.should.be.exactly(3);
node.loc.end.line.should.be.exactly(3);
});
it('test column', function() {
node.loc.start.column.should.be.exactly(5);
node.loc.end.column.should.be.exactly(9);
});
it('test offsets', function() {
node.loc.source.should.be.exactly('true');
});
});
});