1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Copyright 2008 the V8 project authors. All rights reserved.
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met:
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions of source code must retain the above copyright
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       notice, this list of conditions and the following disclaimer.
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions in binary form must reproduce the above
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       copyright notice, this list of conditions and the following
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       disclaimer in the documentation and/or other materials provided
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       with the distribution.
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Neither the name of Google Inc. nor the names of its
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       contributors may be used to endorse or promote products derived
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       from this software without specific prior written permission.
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Flags: --expose-debug-as debug
29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// For this test to work this file MUST have CR LF line endings.
30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction a() { b(); };
31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction    b() {
32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  c(true);
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block};
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  function c(x) {
35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    if (x) {
36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      return 1;
37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    } else {
38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      return 1;
39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    }
40a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  };
41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction d(x) {
42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 1 ;
43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 2 ;
44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 3 ;
45a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 4 ;
46a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 5 ;
47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 6 ;
48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 7 ;
49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 8 ;
50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 9 ;
51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 10;
52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 11;
53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 12;
54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 13;
55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 14;
56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  x = 15;
57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Get the Debug object exposed from the debug context global object.
60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockDebug = debug.Debug
61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// This is the number of comment lines above the first test function.
63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar comment_lines = 29;
64a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// This is the last position in the entire file (note: this equals
66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// file size of <debug-sourceinfo.js> - 1, since starting at 0).
67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar last_position = 14312;
68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// This is the last line of entire file (note: starting at 0).
69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar last_line = 351;
70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// This is the last column of last line (note: starting at 0 and +2, due
71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// to trailing <CR><LF>).
72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar last_column = 2;
73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// This magic number is the length or the first line comment (actually number
75a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// of characters before 'function a(...'.
76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar comment_line_length = 1726;
77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar start_a = 10 + comment_line_length;
78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar start_b = 37 + comment_line_length;
79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar start_c = 71 + comment_line_length;
80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar start_d = 163 + comment_line_length;
81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// The position of the first line of d(), i.e. "x = 1 ;".
83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar start_code_d = start_d + 7;
84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// The line # of the first line of d() (note: starting at 0).
85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar start_line_d = 41;
86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar line_length_d = 11;
87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar num_lines_d = 15;
88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_a, Debug.sourcePosition(a));
90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_b, Debug.sourcePosition(b));
91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_c, Debug.sourcePosition(c));
92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_d, Debug.sourcePosition(d));
93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
94a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar script = Debug.findScript(a);
95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertTrue(script.data === Debug.findScript(b).data);
96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertTrue(script.data === Debug.findScript(c).data);
97a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertTrue(script.data === Debug.findScript(d).data);
98a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertTrue(script.source === Debug.findScript(b).source);
99a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertTrue(script.source === Debug.findScript(c).source);
100a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertTrue(script.source === Debug.findScript(d).source);
101a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
102a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test that when running through source positions the position, line and
103a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// column progresses as expected.
104a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar position;
105a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar line;
106a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar column;
107a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var p = 0; p < 100; p++) {
108a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var location = script.locationFromPosition(p);
109a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  if (p > 0) {
110a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(position + 1, location.position);
111a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    if (line == location.line) {
112a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      assertEquals(column + 1, location.column);
113a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    } else {
114a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      assertEquals(line + 1, location.line);
115a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      assertEquals(0, location.column);
116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    }
117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  } else {
118a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(0, location.position);
119a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(0, location.line);
120a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(0, location.column);
121a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
122a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
123a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Remember the location.
124a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  position = location.position;
125a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  line = location.line;
126a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  column = location.column;
127a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
128a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
129a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Every line of d() is the same length.  Verify we can loop through all
130a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// positions and find the right line # for each.
131a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar p = start_code_d;
132a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (line = 0; line < num_lines_d; line++) {
133a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  for (column = 0; column < line_length_d; column++) {
134a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    var location = script.locationFromPosition(p);
135a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(p, location.position);
136a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(start_line_d + line, location.line);
137a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    assertEquals(column, location.column);
138a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    p++;
139a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
140a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
141a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
142a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test first position.
143a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromPosition(0).position);
144a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromPosition(0).line);
145a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromPosition(0).column);
146a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
147a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test second position.
148a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, script.locationFromPosition(1).position);
149a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromPosition(1).line);
150a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, script.locationFromPosition(1).column);
151a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
152a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test first position in function a().
153a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_a, script.locationFromPosition(start_a).position);
154a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromPosition(start_a).line - comment_lines);
155a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, script.locationFromPosition(start_a).column);
156a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
157a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test first position in function b().
158a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_b, script.locationFromPosition(start_b).position);
159a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, script.locationFromPosition(start_b).line - comment_lines);
160a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(13, script.locationFromPosition(start_b).column);
161a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
162a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test first position in function c().
163a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_c, script.locationFromPosition(start_c).position);
164a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(4, script.locationFromPosition(start_c).line - comment_lines);
165a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(12, script.locationFromPosition(start_c).column);
166a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
167a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test first position in function d().
168a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_d, script.locationFromPosition(start_d).position);
169a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(11, script.locationFromPosition(start_d).line - comment_lines);
170a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, script.locationFromPosition(start_d).column);
171a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
172a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test first line.
173a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine().position);
174a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine().line);
175a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine().column);
176a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine(0).position);
177a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine(0).line);
178a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine(0).column);
179a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
180a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test first line column 1.
181a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, script.locationFromLine(0, 1).position);
182a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine(0, 1).line);
183a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, script.locationFromLine(0, 1).column);
184a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
185a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test first line offset 1.
186a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, script.locationFromLine(0, 0, 1).position);
187a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine(0, 0, 1).line);
188a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, script.locationFromLine(0, 0, 1).column);
189a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
190a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test offset function a().
191a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_a, script.locationFromLine(void 0, void 0, start_a).position);
192a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine(void 0, void 0, start_a).line - comment_lines);
193a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, script.locationFromLine(void 0, void 0, start_a).column);
194a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_a, script.locationFromLine(0, void 0, start_a).position);
195a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine(0, void 0, start_a).line - comment_lines);
196a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, script.locationFromLine(0, void 0, start_a).column);
197a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_a, script.locationFromLine(0, 0, start_a).position);
198a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine(0, 0, start_a).line - comment_lines);
199a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(10, script.locationFromLine(0, 0, start_a).column);
200a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
201a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test second line offset function a().
202a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_a + 14, script.locationFromLine(1, 0, start_a).position);
203a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, script.locationFromLine(1, 0, start_a).line - comment_lines);
204a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine(1, 0, start_a).column);
205a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
206a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test second line column 2 offset function a().
207a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_a + 14 + 2, script.locationFromLine(1, 2, start_a).position);
208a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, script.locationFromLine(1, 2, start_a).line - comment_lines);
209a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(2, script.locationFromLine(1, 2, start_a).column);
210a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
211a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test offset function b().
212a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_b, script.locationFromLine(0, 0, start_b).position);
213a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(1, script.locationFromLine(0, 0, start_b).line - comment_lines);
214a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(13, script.locationFromLine(0, 0, start_b).column);
215a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
216a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test second line offset function b().
217a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_b + 6, script.locationFromLine(1, 0, start_b).position);
218a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(2, script.locationFromLine(1, 0, start_b).line - comment_lines);
219a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine(1, 0, start_b).column);
220a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
221a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test second line column 11 offset function b().
222a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_b + 6 + 11, script.locationFromLine(1, 11, start_b).position);
223a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(2, script.locationFromLine(1, 11, start_b).line - comment_lines);
224a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(11, script.locationFromLine(1, 11, start_b).column);
225a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
226a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test second line column 12 offset function b. Second line in b is 11 long
227a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// using column 12 wraps to next line.
228a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(start_b + 6 + 12, script.locationFromLine(1, 12, start_b).position);
229a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(3, script.locationFromLine(1, 12, start_b).line - comment_lines);
230a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromLine(1, 12, start_b).column);
231a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
232a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test the Debug.findSourcePosition which wraps SourceManager.
233a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0 + start_a, Debug.findFunctionSourceLocation(a, 0, 0).position);
234a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0 + start_b, Debug.findFunctionSourceLocation(b, 0, 0).position);
235a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(6 + start_b, Debug.findFunctionSourceLocation(b, 1, 0).position);
236a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(8 + start_b, Debug.findFunctionSourceLocation(b, 1, 2).position);
237a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(18 + start_b, Debug.findFunctionSourceLocation(b, 2, 0).position);
238a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0 + start_c, Debug.findFunctionSourceLocation(c, 0, 0).position);
239a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(7 + start_c, Debug.findFunctionSourceLocation(c, 1, 0).position);
240a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(21 + start_c, Debug.findFunctionSourceLocation(c, 2, 0).position);
241a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(38 + start_c, Debug.findFunctionSourceLocation(c, 3, 0).position);
242a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(52 + start_c, Debug.findFunctionSourceLocation(c, 4, 0).position);
243a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(69 + start_c, Debug.findFunctionSourceLocation(c, 5, 0).position);
244a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(76 + start_c, Debug.findFunctionSourceLocation(c, 6, 0).position);
245a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0 + start_d, Debug.findFunctionSourceLocation(d, 0, 0).position);
246a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(7 + start_d, Debug.findFunctionSourceLocation(d, 1, 0).position);
247a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (i = 1; i <= num_lines_d; i++) {
248a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(7 + (i * line_length_d) + start_d, Debug.findFunctionSourceLocation(d, (i + 1), 0).position);
249a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
250a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(175 + start_d, Debug.findFunctionSourceLocation(d, 17, 0).position);
251a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
252a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Make sure invalid inputs work properly.
253a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(0, script.locationFromPosition(-1).line);
254a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(null, script.locationFromPosition(last_position + 1));
255a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
256a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test last position.
257a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(last_position, script.locationFromPosition(last_position).position);
258a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(last_line, script.locationFromPosition(last_position).line);
259a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(last_column, script.locationFromPosition(last_position).column);
260a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
261a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test source line and restriction. All the following tests start from line 1
262a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// column 2 in function b, which is the call to c.
263a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//   c(true);
264a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//   ^
265a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
266a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar location;
267a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
268a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 0, start_b);
269a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('  c(true);', location.sourceText());
270a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
271a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockresult = ['c', ' c', ' c(', '  c(', '  c(t']
272a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var i = 1; i <= 5; i++) {
273a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  location = script.locationFromLine(1, 2, start_b);
274a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  location.restrict(i);
275a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(result[i - 1], location.sourceText());
276a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
277a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
278a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 2, start_b);
279a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(1, 0);
280a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('c', location.sourceText());
281a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
282a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 2, start_b);
283a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(2, 0);
284a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('c(', location.sourceText());
285a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
286a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 2, start_b);
287a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(2, 1);
288a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(' c', location.sourceText());
289a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
290a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 2, start_b);
291a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(2, 2);
292a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(' c', location.sourceText());
293a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
294a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 2, start_b);
295a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(2, 3);
296a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(' c', location.sourceText());
297a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
298a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 2, start_b);
299a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(3, 1);
300a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(' c(', location.sourceText());
301a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
302a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 2, start_b);
303a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(5, 0);
304a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('c(tru', location.sourceText());
305a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
306a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 2, start_b);
307a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(5, 2);
308a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('  c(t', location.sourceText());
309a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
310a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 2, start_b);
311a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(5, 4);
312a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('  c(t', location.sourceText());
313a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
314a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// All the following tests start from line 1 column 10 in function b, which is
315a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// the final character.
316a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//   c(true);
317a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//          ^
318a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
319a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 10, start_b);
320a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(5, 0);
321a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('rue);', location.sourceText());
322a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
323a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 10, start_b);
324a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(7, 0);
325a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('(true);', location.sourceText());
326a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
327a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// All the following tests start from line 1 column 0 in function b, which is
328a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// the first character.
329a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//   c(true);
330a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//^
331a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
332a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 0, start_b);
333a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(5, 0);
334a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('  c(t', location.sourceText());
335a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
336a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 0, start_b);
337a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(5, 4);
338a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('  c(t', location.sourceText());
339a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
340a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 0, start_b);
341a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(7, 0);
342a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('  c(tru', location.sourceText());
343a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
344a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation = script.locationFromLine(1, 0, start_b);
345a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocklocation.restrict(7, 6);
346a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals('  c(tru', location.sourceText());
347a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
348a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test that script.sourceLine(line) works.
349a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (line = 0; line < num_lines_d; line++) {
350a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var line_content_regexp = new RegExp("  x = " + (line + 1));
351a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertTrue(line_content_regexp.test(script.sourceLine(start_line_d + line)));
352a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
353