ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved.
|
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without
|
| 3 | // modification, are permitted provided that the following conditions are
|
| 4 | // met:
|
| 5 | //
|
| 6 | // * Redistributions of source code must retain the above copyright
|
| 7 | // notice, this list of conditions and the following disclaimer.
|
| 8 | // * Redistributions in binary form must reproduce the above
|
| 9 | // copyright notice, this list of conditions and the following
|
| 10 | // disclaimer in the documentation and/or other materials provided
|
| 11 | // with the distribution.
|
| 12 | // * Neither the name of Google Inc. nor the names of its
|
| 13 | // contributors may be used to endorse or promote products derived
|
| 14 | // from this software without specific prior written permission.
|
| 15 | //
|
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 27 |
|
| 28 | // Flags: --expose-debug-as debug
|
| 29 | // For this test to work this file MUST have CR LF line endings.
|
| 30 | function a() { b(); };
|
| 31 | function b() {
|
| 32 | c(true);
|
| 33 | };
|
| 34 | function c(x) {
|
| 35 | if (x) {
|
| 36 | return 1;
|
| 37 | } else {
|
| 38 | return 1;
|
| 39 | }
|
| 40 | };
|
| 41 |
|
| 42 | // Get the Debug object exposed from the debug context global object.
|
| 43 | Debug = debug.Debug
|
| 44 |
|
| 45 | // This is the number of comment lines above the first test function.
|
| 46 | var comment_lines = 29;
|
| 47 |
|
| 48 | // This magic number is the length or the first line comment (actually number
|
| 49 | // of characters before 'function a(...'.
|
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 50 | var comment_line_length = 1726;
|
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 51 | var start_a = 10 + comment_line_length;
|
| 52 | var start_b = 37 + comment_line_length;
|
| 53 | var start_c = 71 + comment_line_length;
|
| 54 |
|
| 55 | assertEquals(start_a, Debug.sourcePosition(a));
|
| 56 | assertEquals(start_b, Debug.sourcePosition(b));
|
| 57 | assertEquals(start_c, Debug.sourcePosition(c));
|
| 58 |
|
| 59 | var script = Debug.findScript(a);
|
| 60 | assertTrue(script.data === Debug.findScript(b).data);
|
| 61 | assertTrue(script.data === Debug.findScript(c).data);
|
| 62 | assertTrue(script.source === Debug.findScript(b).source);
|
| 63 | assertTrue(script.source === Debug.findScript(c).source);
|
| 64 |
|
| 65 | // Test that when running through source positions the position, line and
|
| 66 | // column progresses as expected.
|
| 67 | var position;
|
| 68 | var line;
|
| 69 | var column;
|
| 70 | for (var p = 0; p < 100; p++) {
|
| 71 | var location = script.locationFromPosition(p);
|
| 72 | if (p > 0) {
|
| 73 | assertEquals(position + 1, location.position);
|
| 74 | if (line == location.line) {
|
| 75 | assertEquals(column + 1, location.column);
|
| 76 | } else {
|
| 77 | assertEquals(line + 1, location.line);
|
| 78 | assertEquals(0, location.column);
|
| 79 | }
|
| 80 | } else {
|
| 81 | assertEquals(0, location.position);
|
| 82 | assertEquals(0, location.line);
|
| 83 | assertEquals(0, location.column);
|
| 84 | }
|
| 85 |
|
| 86 | // Remember the location.
|
| 87 | position = location.position;
|
| 88 | line = location.line;
|
| 89 | column = location.column;
|
| 90 | }
|
| 91 |
|
| 92 | // Test first position.
|
| 93 | assertEquals(0, script.locationFromPosition(0).position);
|
| 94 | assertEquals(0, script.locationFromPosition(0).line);
|
| 95 | assertEquals(0, script.locationFromPosition(0).column);
|
| 96 |
|
| 97 | // Test second position.
|
| 98 | assertEquals(1, script.locationFromPosition(1).position);
|
| 99 | assertEquals(0, script.locationFromPosition(1).line);
|
| 100 | assertEquals(1, script.locationFromPosition(1).column);
|
| 101 |
|
| 102 | // Test first position in finction a.
|
| 103 | assertEquals(start_a, script.locationFromPosition(start_a).position);
|
| 104 | assertEquals(0, script.locationFromPosition(start_a).line - comment_lines);
|
| 105 | assertEquals(10, script.locationFromPosition(start_a).column);
|
| 106 |
|
| 107 | // Test first position in finction b.
|
| 108 | assertEquals(start_b, script.locationFromPosition(start_b).position);
|
| 109 | assertEquals(1, script.locationFromPosition(start_b).line - comment_lines);
|
| 110 | assertEquals(13, script.locationFromPosition(start_b).column);
|
| 111 |
|
| 112 | // Test first position in finction b.
|
| 113 | assertEquals(start_c, script.locationFromPosition(start_c).position);
|
| 114 | assertEquals(4, script.locationFromPosition(start_c).line - comment_lines);
|
| 115 | assertEquals(12, script.locationFromPosition(start_c).column);
|
| 116 |
|
| 117 | // Test first line.
|
| 118 | assertEquals(0, script.locationFromLine().position);
|
| 119 | assertEquals(0, script.locationFromLine().line);
|
| 120 | assertEquals(0, script.locationFromLine().column);
|
| 121 | assertEquals(0, script.locationFromLine(0).position);
|
| 122 | assertEquals(0, script.locationFromLine(0).line);
|
| 123 | assertEquals(0, script.locationFromLine(0).column);
|
| 124 |
|
| 125 | // Test first line column 1
|
| 126 | assertEquals(1, script.locationFromLine(0, 1).position);
|
| 127 | assertEquals(0, script.locationFromLine(0, 1).line);
|
| 128 | assertEquals(1, script.locationFromLine(0, 1).column);
|
| 129 |
|
| 130 | // Test first line offset 1
|
| 131 | assertEquals(1, script.locationFromLine(0, 0, 1).position);
|
| 132 | assertEquals(0, script.locationFromLine(0, 0, 1).line);
|
| 133 | assertEquals(1, script.locationFromLine(0, 0, 1).column);
|
| 134 |
|
| 135 | // Test offset function a
|
| 136 | assertEquals(start_a, script.locationFromLine(void 0, void 0, start_a).position);
|
| 137 | assertEquals(0, script.locationFromLine(void 0, void 0, start_a).line - comment_lines);
|
| 138 | assertEquals(10, script.locationFromLine(void 0, void 0, start_a).column);
|
| 139 | assertEquals(start_a, script.locationFromLine(0, void 0, start_a).position);
|
| 140 | assertEquals(0, script.locationFromLine(0, void 0, start_a).line - comment_lines);
|
| 141 | assertEquals(10, script.locationFromLine(0, void 0, start_a).column);
|
| 142 | assertEquals(start_a, script.locationFromLine(0, 0, start_a).position);
|
| 143 | assertEquals(0, script.locationFromLine(0, 0, start_a).line - comment_lines);
|
| 144 | assertEquals(10, script.locationFromLine(0, 0, start_a).column);
|
| 145 |
|
| 146 | // Test second line offset function a
|
| 147 | assertEquals(start_a + 14, script.locationFromLine(1, 0, start_a).position);
|
| 148 | assertEquals(1, script.locationFromLine(1, 0, start_a).line - comment_lines);
|
| 149 | assertEquals(0, script.locationFromLine(1, 0, start_a).column);
|
| 150 |
|
| 151 | // Test second line column 2 offset function a
|
| 152 | assertEquals(start_a + 14 + 2, script.locationFromLine(1, 2, start_a).position);
|
| 153 | assertEquals(1, script.locationFromLine(1, 2, start_a).line - comment_lines);
|
| 154 | assertEquals(2, script.locationFromLine(1, 2, start_a).column);
|
| 155 |
|
| 156 | // Test offset function b
|
| 157 | assertEquals(start_b, script.locationFromLine(0, 0, start_b).position);
|
| 158 | assertEquals(1, script.locationFromLine(0, 0, start_b).line - comment_lines);
|
| 159 | assertEquals(13, script.locationFromLine(0, 0, start_b).column);
|
| 160 |
|
| 161 | // Test second line offset function b
|
| 162 | assertEquals(start_b + 6, script.locationFromLine(1, 0, start_b).position);
|
| 163 | assertEquals(2, script.locationFromLine(1, 0, start_b).line - comment_lines);
|
| 164 | assertEquals(0, script.locationFromLine(1, 0, start_b).column);
|
| 165 |
|
| 166 | // Test second line column 11 offset function b
|
| 167 | assertEquals(start_b + 6 + 11, script.locationFromLine(1, 11, start_b).position);
|
| 168 | assertEquals(2, script.locationFromLine(1, 11, start_b).line - comment_lines);
|
| 169 | assertEquals(11, script.locationFromLine(1, 11, start_b).column);
|
| 170 |
|
| 171 | // Test second line column 12 offset function b. Second line in b is 11 long
|
| 172 | // using column 12 wraps to next line.
|
| 173 | assertEquals(start_b + 6 + 12, script.locationFromLine(1, 12, start_b).position);
|
| 174 | assertEquals(3, script.locationFromLine(1, 12, start_b).line - comment_lines);
|
| 175 | assertEquals(0, script.locationFromLine(1, 12, start_b).column);
|
| 176 |
|
| 177 | // Test the Debug.findSourcePosition which wraps SourceManager.
|
| 178 | assertEquals(0 + start_a, Debug.findFunctionSourcePosition(a, 0, 0));
|
| 179 | assertEquals(0 + start_b, Debug.findFunctionSourcePosition(b, 0, 0));
|
| 180 | assertEquals(6 + start_b, Debug.findFunctionSourcePosition(b, 1, 0));
|
| 181 | assertEquals(8 + start_b, Debug.findFunctionSourcePosition(b, 1, 2));
|
| 182 | assertEquals(18 + start_b, Debug.findFunctionSourcePosition(b, 2, 0));
|
| 183 | assertEquals(0 + start_c, Debug.findFunctionSourcePosition(c, 0, 0));
|
| 184 | assertEquals(7 + start_c, Debug.findFunctionSourcePosition(c, 1, 0));
|
| 185 | assertEquals(21 + start_c, Debug.findFunctionSourcePosition(c, 2, 0));
|
| 186 | assertEquals(38 + start_c, Debug.findFunctionSourcePosition(c, 3, 0));
|
| 187 | assertEquals(52 + start_c, Debug.findFunctionSourcePosition(c, 4, 0));
|
| 188 | assertEquals(69 + start_c, Debug.findFunctionSourcePosition(c, 5, 0));
|
| 189 | assertEquals(76 + start_c, Debug.findFunctionSourcePosition(c, 6, 0));
|
| 190 |
|
| 191 | // Test source line and restriction. All the following tests start from line 1
|
| 192 | // column 2 in function b, which is the call to c.
|
| 193 | // c(true);
|
| 194 | // ^
|
| 195 |
|
| 196 | var location;
|
| 197 |
|
| 198 | location = script.locationFromLine(1, 0, start_b);
|
| 199 | assertEquals(' c(true);', location.sourceText());
|
| 200 |
|
| 201 | result = ['c', ' c', ' c(', ' c(', ' c(t']
|
| 202 | for (var i = 1; i <= 5; i++) {
|
| 203 | location = script.locationFromLine(1, 2, start_b);
|
| 204 | location.restrict(i);
|
| 205 | assertEquals(result[i - 1], location.sourceText());
|
| 206 | }
|
| 207 |
|
| 208 | location = script.locationFromLine(1, 2, start_b);
|
| 209 | location.restrict(1, 0);
|
| 210 | assertEquals('c', location.sourceText());
|
| 211 |
|
| 212 | location = script.locationFromLine(1, 2, start_b);
|
| 213 | location.restrict(2, 0);
|
| 214 | assertEquals('c(', location.sourceText());
|
| 215 |
|
| 216 | location = script.locationFromLine(1, 2, start_b);
|
| 217 | location.restrict(2, 1);
|
| 218 | assertEquals(' c', location.sourceText());
|
| 219 |
|
| 220 | location = script.locationFromLine(1, 2, start_b);
|
| 221 | location.restrict(2, 2);
|
| 222 | assertEquals(' c', location.sourceText());
|
| 223 |
|
| 224 | location = script.locationFromLine(1, 2, start_b);
|
| 225 | location.restrict(2, 3);
|
| 226 | assertEquals(' c', location.sourceText());
|
| 227 |
|
| 228 | location = script.locationFromLine(1, 2, start_b);
|
| 229 | location.restrict(3, 1);
|
| 230 | assertEquals(' c(', location.sourceText());
|
| 231 |
|
| 232 | location = script.locationFromLine(1, 2, start_b);
|
| 233 | location.restrict(5, 0);
|
| 234 | assertEquals('c(tru', location.sourceText());
|
| 235 |
|
| 236 | location = script.locationFromLine(1, 2, start_b);
|
| 237 | location.restrict(5, 2);
|
| 238 | assertEquals(' c(t', location.sourceText());
|
| 239 |
|
| 240 | location = script.locationFromLine(1, 2, start_b);
|
| 241 | location.restrict(5, 4);
|
| 242 | assertEquals(' c(t', location.sourceText());
|
| 243 |
|
| 244 | // All the following tests start from line 1 column 10 in function b, which is
|
| 245 | // the final character.
|
| 246 | // c(true);
|
| 247 | // ^
|
| 248 |
|
| 249 | location = script.locationFromLine(1, 10, start_b);
|
| 250 | location.restrict(5, 0);
|
| 251 | assertEquals('rue);', location.sourceText());
|
| 252 |
|
| 253 | location = script.locationFromLine(1, 10, start_b);
|
| 254 | location.restrict(7, 0);
|
| 255 | assertEquals('(true);', location.sourceText());
|
| 256 |
|
| 257 | // All the following tests start from line 1 column 0 in function b, which is
|
| 258 | // the first character.
|
| 259 | // c(true);
|
| 260 | //^
|
| 261 |
|
| 262 | location = script.locationFromLine(1, 0, start_b);
|
| 263 | location.restrict(5, 0);
|
| 264 | assertEquals(' c(t', location.sourceText());
|
| 265 |
|
| 266 | location = script.locationFromLine(1, 0, start_b);
|
| 267 | location.restrict(5, 4);
|
| 268 | assertEquals(' c(t', location.sourceText());
|
| 269 |
|
| 270 | location = script.locationFromLine(1, 0, start_b);
|
| 271 | location.restrict(7, 0);
|
| 272 | assertEquals(' c(tru', location.sourceText());
|
| 273 |
|
| 274 | location = script.locationFromLine(1, 0, start_b);
|
| 275 | location.restrict(7, 6);
|
| 276 | assertEquals(' c(tru', location.sourceText());
|