toolkit/devtools/sourcemap/tests/unit/test_source_map_generator.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * WARNING!
michael@0 3 *
michael@0 4 * Do not edit this file directly, it is built from the sources at
michael@0 5 * https://github.com/mozilla/source-map/
michael@0 6 */
michael@0 7
michael@0 8 Components.utils.import('resource://test/Utils.jsm');
michael@0 9 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 10 /*
michael@0 11 * Copyright 2011 Mozilla Foundation and contributors
michael@0 12 * Licensed under the New BSD license. See LICENSE or:
michael@0 13 * http://opensource.org/licenses/BSD-3-Clause
michael@0 14 */
michael@0 15 define("test/source-map/test-source-map-generator", ["require", "exports", "module"], function (require, exports, module) {
michael@0 16
michael@0 17 var SourceMapGenerator = require('source-map/source-map-generator').SourceMapGenerator;
michael@0 18 var SourceMapConsumer = require('source-map/source-map-consumer').SourceMapConsumer;
michael@0 19 var SourceNode = require('source-map/source-node').SourceNode;
michael@0 20 var util = require('source-map/util');
michael@0 21
michael@0 22 exports['test some simple stuff'] = function (assert, util) {
michael@0 23 var map = new SourceMapGenerator({
michael@0 24 file: 'foo.js',
michael@0 25 sourceRoot: '.'
michael@0 26 });
michael@0 27 assert.ok(true);
michael@0 28 };
michael@0 29
michael@0 30 exports['test JSON serialization'] = function (assert, util) {
michael@0 31 var map = new SourceMapGenerator({
michael@0 32 file: 'foo.js',
michael@0 33 sourceRoot: '.'
michael@0 34 });
michael@0 35 assert.equal(map.toString(), JSON.stringify(map));
michael@0 36 };
michael@0 37
michael@0 38 exports['test adding mappings (case 1)'] = function (assert, util) {
michael@0 39 var map = new SourceMapGenerator({
michael@0 40 file: 'generated-foo.js',
michael@0 41 sourceRoot: '.'
michael@0 42 });
michael@0 43
michael@0 44 assert.doesNotThrow(function () {
michael@0 45 map.addMapping({
michael@0 46 generated: { line: 1, column: 1 }
michael@0 47 });
michael@0 48 });
michael@0 49 };
michael@0 50
michael@0 51 exports['test adding mappings (case 2)'] = function (assert, util) {
michael@0 52 var map = new SourceMapGenerator({
michael@0 53 file: 'generated-foo.js',
michael@0 54 sourceRoot: '.'
michael@0 55 });
michael@0 56
michael@0 57 assert.doesNotThrow(function () {
michael@0 58 map.addMapping({
michael@0 59 generated: { line: 1, column: 1 },
michael@0 60 source: 'bar.js',
michael@0 61 original: { line: 1, column: 1 }
michael@0 62 });
michael@0 63 });
michael@0 64 };
michael@0 65
michael@0 66 exports['test adding mappings (case 3)'] = function (assert, util) {
michael@0 67 var map = new SourceMapGenerator({
michael@0 68 file: 'generated-foo.js',
michael@0 69 sourceRoot: '.'
michael@0 70 });
michael@0 71
michael@0 72 assert.doesNotThrow(function () {
michael@0 73 map.addMapping({
michael@0 74 generated: { line: 1, column: 1 },
michael@0 75 source: 'bar.js',
michael@0 76 original: { line: 1, column: 1 },
michael@0 77 name: 'someToken'
michael@0 78 });
michael@0 79 });
michael@0 80 };
michael@0 81
michael@0 82 exports['test adding mappings (invalid)'] = function (assert, util) {
michael@0 83 var map = new SourceMapGenerator({
michael@0 84 file: 'generated-foo.js',
michael@0 85 sourceRoot: '.'
michael@0 86 });
michael@0 87
michael@0 88 // Not enough info.
michael@0 89 assert.throws(function () {
michael@0 90 map.addMapping({});
michael@0 91 });
michael@0 92
michael@0 93 // Original file position, but no source.
michael@0 94 assert.throws(function () {
michael@0 95 map.addMapping({
michael@0 96 generated: { line: 1, column: 1 },
michael@0 97 original: { line: 1, column: 1 }
michael@0 98 });
michael@0 99 });
michael@0 100 };
michael@0 101
michael@0 102 exports['test that the correct mappings are being generated'] = function (assert, util) {
michael@0 103 var map = new SourceMapGenerator({
michael@0 104 file: 'min.js',
michael@0 105 sourceRoot: '/the/root'
michael@0 106 });
michael@0 107
michael@0 108 map.addMapping({
michael@0 109 generated: { line: 1, column: 1 },
michael@0 110 original: { line: 1, column: 1 },
michael@0 111 source: 'one.js'
michael@0 112 });
michael@0 113 map.addMapping({
michael@0 114 generated: { line: 1, column: 5 },
michael@0 115 original: { line: 1, column: 5 },
michael@0 116 source: 'one.js'
michael@0 117 });
michael@0 118 map.addMapping({
michael@0 119 generated: { line: 1, column: 9 },
michael@0 120 original: { line: 1, column: 11 },
michael@0 121 source: 'one.js'
michael@0 122 });
michael@0 123 map.addMapping({
michael@0 124 generated: { line: 1, column: 18 },
michael@0 125 original: { line: 1, column: 21 },
michael@0 126 source: 'one.js',
michael@0 127 name: 'bar'
michael@0 128 });
michael@0 129 map.addMapping({
michael@0 130 generated: { line: 1, column: 21 },
michael@0 131 original: { line: 2, column: 3 },
michael@0 132 source: 'one.js'
michael@0 133 });
michael@0 134 map.addMapping({
michael@0 135 generated: { line: 1, column: 28 },
michael@0 136 original: { line: 2, column: 10 },
michael@0 137 source: 'one.js',
michael@0 138 name: 'baz'
michael@0 139 });
michael@0 140 map.addMapping({
michael@0 141 generated: { line: 1, column: 32 },
michael@0 142 original: { line: 2, column: 14 },
michael@0 143 source: 'one.js',
michael@0 144 name: 'bar'
michael@0 145 });
michael@0 146
michael@0 147 map.addMapping({
michael@0 148 generated: { line: 2, column: 1 },
michael@0 149 original: { line: 1, column: 1 },
michael@0 150 source: 'two.js'
michael@0 151 });
michael@0 152 map.addMapping({
michael@0 153 generated: { line: 2, column: 5 },
michael@0 154 original: { line: 1, column: 5 },
michael@0 155 source: 'two.js'
michael@0 156 });
michael@0 157 map.addMapping({
michael@0 158 generated: { line: 2, column: 9 },
michael@0 159 original: { line: 1, column: 11 },
michael@0 160 source: 'two.js'
michael@0 161 });
michael@0 162 map.addMapping({
michael@0 163 generated: { line: 2, column: 18 },
michael@0 164 original: { line: 1, column: 21 },
michael@0 165 source: 'two.js',
michael@0 166 name: 'n'
michael@0 167 });
michael@0 168 map.addMapping({
michael@0 169 generated: { line: 2, column: 21 },
michael@0 170 original: { line: 2, column: 3 },
michael@0 171 source: 'two.js'
michael@0 172 });
michael@0 173 map.addMapping({
michael@0 174 generated: { line: 2, column: 28 },
michael@0 175 original: { line: 2, column: 10 },
michael@0 176 source: 'two.js',
michael@0 177 name: 'n'
michael@0 178 });
michael@0 179
michael@0 180 map = JSON.parse(map.toString());
michael@0 181
michael@0 182 util.assertEqualMaps(assert, map, util.testMap);
michael@0 183 };
michael@0 184
michael@0 185 exports['test that source content can be set'] = function (assert, util) {
michael@0 186 var map = new SourceMapGenerator({
michael@0 187 file: 'min.js',
michael@0 188 sourceRoot: '/the/root'
michael@0 189 });
michael@0 190 map.addMapping({
michael@0 191 generated: { line: 1, column: 1 },
michael@0 192 original: { line: 1, column: 1 },
michael@0 193 source: 'one.js'
michael@0 194 });
michael@0 195 map.addMapping({
michael@0 196 generated: { line: 2, column: 1 },
michael@0 197 original: { line: 1, column: 1 },
michael@0 198 source: 'two.js'
michael@0 199 });
michael@0 200 map.setSourceContent('one.js', 'one file content');
michael@0 201
michael@0 202 map = JSON.parse(map.toString());
michael@0 203 assert.equal(map.sources[0], 'one.js');
michael@0 204 assert.equal(map.sources[1], 'two.js');
michael@0 205 assert.equal(map.sourcesContent[0], 'one file content');
michael@0 206 assert.equal(map.sourcesContent[1], null);
michael@0 207 };
michael@0 208
michael@0 209 exports['test .fromSourceMap'] = function (assert, util) {
michael@0 210 var map = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(util.testMap));
michael@0 211 util.assertEqualMaps(assert, map.toJSON(), util.testMap);
michael@0 212 };
michael@0 213
michael@0 214 exports['test .fromSourceMap with sourcesContent'] = function (assert, util) {
michael@0 215 var map = SourceMapGenerator.fromSourceMap(
michael@0 216 new SourceMapConsumer(util.testMapWithSourcesContent));
michael@0 217 util.assertEqualMaps(assert, map.toJSON(), util.testMapWithSourcesContent);
michael@0 218 };
michael@0 219
michael@0 220 exports['test applySourceMap'] = function (assert, util) {
michael@0 221 var node = new SourceNode(null, null, null, [
michael@0 222 new SourceNode(2, 0, 'fileX', 'lineX2\n'),
michael@0 223 'genA1\n',
michael@0 224 new SourceNode(2, 0, 'fileY', 'lineY2\n'),
michael@0 225 'genA2\n',
michael@0 226 new SourceNode(1, 0, 'fileX', 'lineX1\n'),
michael@0 227 'genA3\n',
michael@0 228 new SourceNode(1, 0, 'fileY', 'lineY1\n')
michael@0 229 ]);
michael@0 230 var mapStep1 = node.toStringWithSourceMap({
michael@0 231 file: 'fileA'
michael@0 232 }).map;
michael@0 233 mapStep1.setSourceContent('fileX', 'lineX1\nlineX2\n');
michael@0 234 mapStep1 = mapStep1.toJSON();
michael@0 235
michael@0 236 node = new SourceNode(null, null, null, [
michael@0 237 'gen1\n',
michael@0 238 new SourceNode(1, 0, 'fileA', 'lineA1\n'),
michael@0 239 new SourceNode(2, 0, 'fileA', 'lineA2\n'),
michael@0 240 new SourceNode(3, 0, 'fileA', 'lineA3\n'),
michael@0 241 new SourceNode(4, 0, 'fileA', 'lineA4\n'),
michael@0 242 new SourceNode(1, 0, 'fileB', 'lineB1\n'),
michael@0 243 new SourceNode(2, 0, 'fileB', 'lineB2\n'),
michael@0 244 'gen2\n'
michael@0 245 ]);
michael@0 246 var mapStep2 = node.toStringWithSourceMap({
michael@0 247 file: 'fileGen'
michael@0 248 }).map;
michael@0 249 mapStep2.setSourceContent('fileB', 'lineB1\nlineB2\n');
michael@0 250 mapStep2 = mapStep2.toJSON();
michael@0 251
michael@0 252 node = new SourceNode(null, null, null, [
michael@0 253 'gen1\n',
michael@0 254 new SourceNode(2, 0, 'fileX', 'lineA1\n'),
michael@0 255 new SourceNode(2, 0, 'fileA', 'lineA2\n'),
michael@0 256 new SourceNode(2, 0, 'fileY', 'lineA3\n'),
michael@0 257 new SourceNode(4, 0, 'fileA', 'lineA4\n'),
michael@0 258 new SourceNode(1, 0, 'fileB', 'lineB1\n'),
michael@0 259 new SourceNode(2, 0, 'fileB', 'lineB2\n'),
michael@0 260 'gen2\n'
michael@0 261 ]);
michael@0 262 var expectedMap = node.toStringWithSourceMap({
michael@0 263 file: 'fileGen'
michael@0 264 }).map;
michael@0 265 expectedMap.setSourceContent('fileX', 'lineX1\nlineX2\n');
michael@0 266 expectedMap.setSourceContent('fileB', 'lineB1\nlineB2\n');
michael@0 267 expectedMap = expectedMap.toJSON();
michael@0 268
michael@0 269 // apply source map "mapStep1" to "mapStep2"
michael@0 270 var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2));
michael@0 271 generator.applySourceMap(new SourceMapConsumer(mapStep1));
michael@0 272 var actualMap = generator.toJSON();
michael@0 273
michael@0 274 util.assertEqualMaps(assert, actualMap, expectedMap);
michael@0 275 };
michael@0 276
michael@0 277 exports['test sorting with duplicate generated mappings'] = function (assert, util) {
michael@0 278 var map = new SourceMapGenerator({
michael@0 279 file: 'test.js'
michael@0 280 });
michael@0 281 map.addMapping({
michael@0 282 generated: { line: 3, column: 0 },
michael@0 283 original: { line: 2, column: 0 },
michael@0 284 source: 'a.js'
michael@0 285 });
michael@0 286 map.addMapping({
michael@0 287 generated: { line: 2, column: 0 }
michael@0 288 });
michael@0 289 map.addMapping({
michael@0 290 generated: { line: 2, column: 0 }
michael@0 291 });
michael@0 292 map.addMapping({
michael@0 293 generated: { line: 1, column: 0 },
michael@0 294 original: { line: 1, column: 0 },
michael@0 295 source: 'a.js'
michael@0 296 });
michael@0 297
michael@0 298 util.assertEqualMaps(assert, map.toJSON(), {
michael@0 299 version: 3,
michael@0 300 file: 'test.js',
michael@0 301 sources: ['a.js'],
michael@0 302 names: [],
michael@0 303 mappings: 'AAAA;A;AACA'
michael@0 304 });
michael@0 305 };
michael@0 306
michael@0 307 exports['test ignore duplicate mappings.'] = function (assert, util) {
michael@0 308 var init = { file: 'min.js', sourceRoot: '/the/root' };
michael@0 309 var map1, map2;
michael@0 310
michael@0 311 // null original source location
michael@0 312 var nullMapping1 = {
michael@0 313 generated: { line: 1, column: 0 }
michael@0 314 };
michael@0 315 var nullMapping2 = {
michael@0 316 generated: { line: 2, column: 2 }
michael@0 317 };
michael@0 318
michael@0 319 map1 = new SourceMapGenerator(init);
michael@0 320 map2 = new SourceMapGenerator(init);
michael@0 321
michael@0 322 map1.addMapping(nullMapping1);
michael@0 323 map1.addMapping(nullMapping1);
michael@0 324
michael@0 325 map2.addMapping(nullMapping1);
michael@0 326
michael@0 327 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
michael@0 328
michael@0 329 map1.addMapping(nullMapping2);
michael@0 330 map1.addMapping(nullMapping1);
michael@0 331
michael@0 332 map2.addMapping(nullMapping2);
michael@0 333
michael@0 334 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
michael@0 335
michael@0 336 // original source location
michael@0 337 var srcMapping1 = {
michael@0 338 generated: { line: 1, column: 0 },
michael@0 339 original: { line: 11, column: 0 },
michael@0 340 source: 'srcMapping1.js'
michael@0 341 };
michael@0 342 var srcMapping2 = {
michael@0 343 generated: { line: 2, column: 2 },
michael@0 344 original: { line: 11, column: 0 },
michael@0 345 source: 'srcMapping2.js'
michael@0 346 };
michael@0 347
michael@0 348 map1 = new SourceMapGenerator(init);
michael@0 349 map2 = new SourceMapGenerator(init);
michael@0 350
michael@0 351 map1.addMapping(srcMapping1);
michael@0 352 map1.addMapping(srcMapping1);
michael@0 353
michael@0 354 map2.addMapping(srcMapping1);
michael@0 355
michael@0 356 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
michael@0 357
michael@0 358 map1.addMapping(srcMapping2);
michael@0 359 map1.addMapping(srcMapping1);
michael@0 360
michael@0 361 map2.addMapping(srcMapping2);
michael@0 362
michael@0 363 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
michael@0 364
michael@0 365 // full original source and name information
michael@0 366 var fullMapping1 = {
michael@0 367 generated: { line: 1, column: 0 },
michael@0 368 original: { line: 11, column: 0 },
michael@0 369 source: 'fullMapping1.js',
michael@0 370 name: 'fullMapping1'
michael@0 371 };
michael@0 372 var fullMapping2 = {
michael@0 373 generated: { line: 2, column: 2 },
michael@0 374 original: { line: 11, column: 0 },
michael@0 375 source: 'fullMapping2.js',
michael@0 376 name: 'fullMapping2'
michael@0 377 };
michael@0 378
michael@0 379 map1 = new SourceMapGenerator(init);
michael@0 380 map2 = new SourceMapGenerator(init);
michael@0 381
michael@0 382 map1.addMapping(fullMapping1);
michael@0 383 map1.addMapping(fullMapping1);
michael@0 384
michael@0 385 map2.addMapping(fullMapping1);
michael@0 386
michael@0 387 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
michael@0 388
michael@0 389 map1.addMapping(fullMapping2);
michael@0 390 map1.addMapping(fullMapping1);
michael@0 391
michael@0 392 map2.addMapping(fullMapping2);
michael@0 393
michael@0 394 util.assertEqualMaps(assert, map1.toJSON(), map2.toJSON());
michael@0 395 };
michael@0 396
michael@0 397 exports['test github issue #72, check for duplicate names or sources'] = function (assert, util) {
michael@0 398 var map = new SourceMapGenerator({
michael@0 399 file: 'test.js'
michael@0 400 });
michael@0 401 map.addMapping({
michael@0 402 generated: { line: 1, column: 1 },
michael@0 403 original: { line: 2, column: 2 },
michael@0 404 source: 'a.js',
michael@0 405 name: 'foo'
michael@0 406 });
michael@0 407 map.addMapping({
michael@0 408 generated: { line: 3, column: 3 },
michael@0 409 original: { line: 4, column: 4 },
michael@0 410 source: 'a.js',
michael@0 411 name: 'foo'
michael@0 412 });
michael@0 413 util.assertEqualMaps(assert, map.toJSON(), {
michael@0 414 version: 3,
michael@0 415 file: 'test.js',
michael@0 416 sources: ['a.js'],
michael@0 417 names: ['foo'],
michael@0 418 mappings: 'CACEA;;GAEEA'
michael@0 419 });
michael@0 420 };
michael@0 421
michael@0 422 });
michael@0 423 function run_test() {
michael@0 424 runSourceMapTests('test/source-map/test-source-map-generator', do_throw);
michael@0 425 }

mercurial