react / wstein / node_modules / browserify / node_modules / browser-pack / node_modules / combine-source-map / node_modules / convert-source-map / test / convert-source-map.js
80556 views'use strict';1/*jshint asi: true */23var test = require('tap').test4, generator = require('inline-source-map')5, convert = require('..')67var gen = generator()8.addMappings('foo.js', [{ original: { line: 2, column: 3 } , generated: { line: 5, column: 10 } }], { line: 5 })9.addGeneratedMappings('bar.js', 'var a = 2;\nconsole.log(a)', { line: 23, column: 22 })1011, base64 = gen.base64Encode()12, comment = gen.inlineMappingUrl()13, json = gen.toString()14, obj = JSON.parse(json)1516test('different formats', function (t) {1718t.equal(convert.fromComment(comment).toComment(), comment, 'comment -> comment')19t.equal(convert.fromComment(comment).toBase64(), base64, 'comment -> base64')20t.equal(convert.fromComment(comment).toJSON(), json, 'comment -> json')21t.deepEqual(convert.fromComment(comment).toObject(), obj, 'comment -> object')2223t.equal(convert.fromBase64(base64).toBase64(), base64, 'base64 -> base64')24t.equal(convert.fromBase64(base64).toComment(), comment, 'base64 -> comment')25t.equal(convert.fromBase64(base64).toJSON(), json, 'base64 -> json')26t.deepEqual(convert.fromBase64(base64).toObject(), obj, 'base64 -> object')2728t.equal(convert.fromJSON(json).toJSON(), json, 'json -> json')29t.equal(convert.fromJSON(json).toBase64(), base64, 'json -> base64')30t.equal(convert.fromJSON(json).toComment(), comment, 'json -> comment')31t.deepEqual(convert.fromJSON(json).toObject(), obj, 'json -> object')32t.end()33})3435test('to object returns a copy', function (t) {36var c = convert.fromJSON(json)37var o = c.toObject()38o.version = '99';39t.equal(c.toObject().version, 3, 'setting property on returned object does not affect original')40t.end()41})4243test('to multi-line map', function (t) {44var c = convert.fromObject(obj);45var s = c.toComment({ multiline: true });46t.similar(s, /^\/\*# sourceMappingURL=.+ \*\/$/);47t.end();48})4950test('from source', function (t) {51var foo = [52'function foo() {'53, ' console.log("hello I am foo");'54, ' console.log("who are you");'55, '}'56, ''57, 'foo();'58, ''59].join('\n')60, map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'61, otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'6263function getComment(src) {64var map = convert.fromSource(src);65return map ? map.toComment() : null;66}6768t.equal(getComment(foo), null, 'no comment returns null')69t.equal(getComment(foo + map), map, 'beginning of last line')70t.equal(getComment(foo + ' ' + map), map, 'indented of last line')71t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')72t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')73t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')74t.end()75})7677test('from source with a large source', function (t) {78var foo = [79'function foo() {'80, ' console.log("hello I am foo");'81, ' console.log("who are you");'82, '}'83, ''84, 'foo();'85, ''86].join('\n')87, map = '//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'88, otherMap = '//# sourceMappingURL=data:application/json;base64,otherZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'8990function getComment(src) {91var map = convert.fromSource(src, true);92return map ? map.toComment() : null;93}9495t.equal(getComment(foo), null, 'no comment returns null')96t.equal(getComment(foo + map), map, 'beginning of last line')97t.equal(getComment(foo + ' ' + map), map, 'indented of last line')98t.equal(getComment(foo + ' ' + map + '\n\n'), map, 'indented on last non empty line')99t.equal(getComment(foo + map + '\nconsole.log("more code");\nfoo()\n'), map, 'in the middle of code')100t.equal(getComment(foo + otherMap + '\n' + map), map, 'finds last map in source')101t.end()102})103104test('remove comments', function (t) {105var foo = [106'function foo() {'107, ' console.log("hello I am foo");'108, ' console.log("who are you");'109, '}'110, ''111, 'foo();'112, ''113].join('\n')114// this one is old spec on purpose115, map = '//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'116, otherMap = '//# sourceMappingURL=data:application/json;base64,ZXJzaW9uIjozLCJmaWxlIjoiIiwic291cmNlcyI6WyJmdW5jdGlvbiBmb28oKSB7XG4gY29uc29sZS5sb2coXCJoZWxsbyBJIGFtIGZvb1wiKTtcbiBjb25zb2xlLmxvZyhcIndobyBhcmUgeW91XCIpO1xufVxuXG5mb28oKTtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9'117, extraCode = '\nconsole.log("more code");\nfoo()\n'118119t.equal(convert.removeComments(foo + map), foo, 'from last line')120t.equal(convert.removeComments(foo + map + extraCode), foo + extraCode, 'from the middle of code')121t.equal(convert.removeComments(foo + otherMap + extraCode + map), foo + extraCode, 'multiple comments from the middle of code')122t.end()123})124125test('remove map file comments', function (t) {126var foo = [127'function foo() {'128, ' console.log("hello I am foo");'129, ' console.log("who are you");'130, '}'131, ''132, 'foo();'133, ''134].join('\n')135, fileMap1 = '//# sourceMappingURL=foo.js.map'136, fileMap2 = '/*# sourceMappingURL=foo.js.map */';137138t.equal(convert.removeMapFileComments(foo + fileMap1), foo, '// style filemap comment')139t.equal(convert.removeMapFileComments(foo + fileMap2), foo, '/* */ style filemap comment')140t.end()141})142143test('pretty json', function (t) {144var mod = convert.fromJSON(json).toJSON(2)145, expected = JSON.stringify(obj, null, 2);146147t.equal(148mod149, expected150, 'pretty prints json when space is given')151t.end()152})153154test('adding properties', function (t) {155var mod = convert156.fromJSON(json)157.addProperty('foo', 'bar')158.toJSON()159, expected = JSON.parse(json);160expected.foo = 'bar';161t.equal(162mod163, JSON.stringify(expected)164, 'includes added property'165)166t.end()167})168169test('setting properties', function (t) {170var mod = convert171.fromJSON(json)172.setProperty('version', '2')173.setProperty('mappings', ';;;UACG')174.setProperty('should add', 'this')175.toJSON()176, expected = JSON.parse(json);177expected.version = '2';178expected.mappings = ';;;UACG';179expected['should add'] = 'this';180t.equal(181mod182, JSON.stringify(expected)183, 'includes new property and changes existing properties'184)185t.end()186})187188test('getting properties', function (t) {189var sm = convert.fromJSON(json)190191t.equal(sm.getProperty('version'), 3, 'gets version')192t.deepEqual(sm.getProperty('sources'), ['foo.js', 'bar.js'], 'gets sources')193t.end()194})195196197