|
1 | 1 | var should = require("should");
|
2 | 2 | var parser = require('../src/index');
|
3 | 3 |
|
4 |
| -/*describe('Test offsets', function() { |
| 4 | +describe('Test offsets', function() { |
5 | 5 |
|
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 | + }); |
7 | 45 |
|
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 | + }); |
23 | 125 |
|
| 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 | + }); |
24 | 139 | });
|
25 | 140 |
|
26 | 141 | });
|
27 |
| -*/ |
|
0 commit comments