Debian Codegraph @

consumer_test.go

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