| 1 |
package sourcemap_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io/ioutil" |
| 6 |
"net/http" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"gopkg.in/sourcemap.v1" |
| 11 |
) |
| 12 |
|
| •13 |
const jqSourceMapURL = "file:///usr/share/javascript/jquery/jquery.min.map" |
| 14 |
|
| 15 |
var jqSourceMapBytes []byte |
| 16 |
|
| 17 |
func init() { |
| •18 |
trans := &http.Transport{} |
| 19 |
trans.RegisterProtocol("file", http.NewFileTransport(http.Dir("/"))) |
| 20 |
|
| 21 |
cli := &http.Client{Transport: trans} |
| 22 |
resp, err := cli.Get(jqSourceMapURL) |
| 23 |
if err != nil { |
| 24 |
panic(err) |
| 25 |
} |
| 26 |
defer resp.Body.Close() |
| 27 |
|
| 28 |
jqSourceMapBytes, err = ioutil.ReadAll(resp.Body) |
| 29 |
if err != nil { |
| 30 |
panic(err) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
type sourceMapTest struct { |
| 35 |
genLine int |
| 36 |
genCol int |
| 37 |
wantedSource string |
| 38 |
wantedName string |
| 39 |
wantedLine int |
| 40 |
wantedCol int |
| 41 |
} |
| 42 |
|
| 43 |
func (test *sourceMapTest) String() string { |
| 44 |
return fmt.Sprintf("line=%d col=%d in file=%s", test.genLine, test.genCol, test.wantedSource) |
| 45 |
} |
| 46 |
|
| 47 |
func (test *sourceMapTest) assert(t *testing.T, smap *sourcemap.Consumer) { |
| 48 |
source, name, line, col, ok := smap.Source(test.genLine, test.genCol) |
| 49 |
if !ok { |
| 50 |
if test.wantedSource == "" && |
| 51 |
test.wantedName == "" && |
| 52 |
test.wantedLine == 0 && |
| 53 |
test.wantedCol == 0 { |
| 54 |
return |
| 55 |
} |
| 56 |
t.Fatalf("Source not found for %s", test) |
| 57 |
} |
| 58 |
if source != test.wantedSource { |
| 59 |
t.Fatalf("file: got %q, wanted %q (%s)", source, test.wantedSource, test) |
| 60 |
} |
| 61 |
if name != test.wantedName { |
| 62 |
t.Fatalf("func: got %q, wanted %q (%s)", name, test.wantedName, test) |
| 63 |
} |
| 64 |
if line != test.wantedLine { |
| 65 |
t.Fatalf("line: got %d, wanted %d (%s)", line, test.wantedLine, test) |
| 66 |
} |
| 67 |
if col != test.wantedCol { |
| 68 |
t.Fatalf("column: got %d, wanted %d (%s)", col, test.wantedCol, test) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestSourceMap(t *testing.T) { |
| 73 |
smap, err := sourcemap.Parse("", []byte(sourceMapJSON)) |
| 74 |
if err != nil { |
| 75 |
t.Fatal(err) |
| 76 |
} |
| 77 |
|
| 78 |
tests := []*sourceMapTest{ |
| 79 |
{1, 1, "/the/root/one.js", "", 1, 1}, |
| 80 |
{1, 5, "/the/root/one.js", "", 1, 5}, |
| 81 |
{1, 9, "/the/root/one.js", "", 1, 11}, |
| 82 |
{1, 18, "/the/root/one.js", "bar", 1, 21}, |
| 83 |
{1, 21, "/the/root/one.js", "", 2, 3}, |
| 84 |
{1, 28, "/the/root/one.js", "baz", 2, 10}, |
| 85 |
{1, 32, "/the/root/one.js", "bar", 2, 14}, |
| 86 |
|
| 87 |
{2, 1, "/the/root/two.js", "", 1, 1}, |
| 88 |
{2, 5, "/the/root/two.js", "", 1, 5}, |
| 89 |
{2, 9, "/the/root/two.js", "", 1, 11}, |
| 90 |
{2, 18, "/the/root/two.js", "n", 1, 21}, |
| 91 |
{2, 21, "/the/root/two.js", "", 2, 3}, |
| 92 |
{2, 28, "/the/root/two.js", "n", 2, 10}, |
| 93 |
|
| 94 |
|
| 95 |
{1, 20, "/the/root/one.js", "bar", 1, 21}, |
| 96 |
{1, 30, "/the/root/one.js", "baz", 2, 10}, |
| 97 |
{2, 12, "/the/root/two.js", "", 1, 11}, |
| 98 |
} |
| 99 |
for _, test := range tests { |
| 100 |
test.assert(t, smap) |
| 101 |
} |
| 102 |
|
| 103 |
_, _, _, _, ok := smap.Source(3, 0) |
| 104 |
if ok { |
| 105 |
t.Fatal("source must not exist") |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func TestSourceRootURL(t *testing.T) { |
| 110 |
jsonStr := sourceMapJSON |
| 111 |
jsonStr = strings.Replace(jsonStr, "/the/root", "http://the/root", 1) |
| 112 |
jsonStr = strings.Replace(jsonStr, "one.js", "../one.js", 1) |
| 113 |
|
| 114 |
smap, err := sourcemap.Parse("", []byte(jsonStr)) |
| 115 |
if err != nil { |
| 116 |
t.Fatal(err) |
| 117 |
} |
| 118 |
|
| 119 |
tests := []*sourceMapTest{ |
| 120 |
{1, 1, "http://the/one.js", "", 1, 1}, |
| 121 |
{2, 1, "http://the/root/two.js", "", 1, 1}, |
| 122 |
} |
| 123 |
for _, test := range tests { |
| 124 |
test.assert(t, smap) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func TestEmptySourceRootURL(t *testing.T) { |
| 129 |
jsonStr := sourceMapJSON |
| 130 |
jsonStr = strings.Replace(jsonStr, "/the/root", "", 1) |
| 131 |
jsonStr = strings.Replace(jsonStr, "one.js", "../one.js", 1) |
| 132 |
|
| 133 |
smap, err := sourcemap.Parse("http://the/root/app.min.map", []byte(jsonStr)) |
| 134 |
if err != nil { |
| 135 |
t.Fatal(err) |
| 136 |
} |
| 137 |
|
| 138 |
tests := []*sourceMapTest{ |
| 139 |
{1, 1, "http://the/one.js", "", 1, 1}, |
| 140 |
{2, 1, "http://the/root/two.js", "", 1, 1}, |
| 141 |
} |
| 142 |
for _, test := range tests { |
| 143 |
test.assert(t, smap) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func TestAbsSourceURL(t *testing.T) { |
| 148 |
jsonStr := sourceMapJSON |
| 149 |
jsonStr = strings.Replace(jsonStr, "/the/root", "", 1) |
| 150 |
jsonStr = strings.Replace(jsonStr, "one.js", "http://the/root/one.js", 1) |
| 151 |
jsonStr = strings.Replace(jsonStr, "two.js", "/another/root/two.js", 1) |
| 152 |
|
| 153 |
testAbsSourceURL(t, "", jsonStr) |
| 154 |
testAbsSourceURL(t, "http://path/to/map", jsonStr) |
| 155 |
} |
| 156 |
|
| 157 |
func testAbsSourceURL(t *testing.T, mapURL, jsonStr string) { |
| 158 |
smap, err := sourcemap.Parse(mapURL, []byte(jsonStr)) |
| 159 |
if err != nil { |
| 160 |
t.Fatal(err) |
| 161 |
} |
| 162 |
|
| 163 |
tests := []*sourceMapTest{ |
| 164 |
{1, 1, "http://the/root/one.js", "", 1, 1}, |
| 165 |
{2, 1, "/another/root/two.js", "", 1, 1}, |
| 166 |
} |
| 167 |
for _, test := range tests { |
| 168 |
test.assert(t, smap) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| •172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| •193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
var genCode = `exports.testGeneratedCode = "ONE.foo=function(a){return baz(a);}; |
| 216 |
TWO.inc=function(a){return a+1;};` |
| 217 |
|
| 218 |
var sourceMapJSON = `{ |
| 219 |
"version": 3, |
| 220 |
"file": "min.js", |
| 221 |
"names": ["bar", "baz", "n"], |
| 222 |
"sources": ["one.js", "two.js"], |
| 223 |
"sourceRoot": "/the/root", |
| 224 |
"mappings": "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA" |
| 225 |
}` |