File: consumer_test.go

package info (click to toggle)
golang-gopkg-sourcemap.v1 1.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 128 kB
  • sloc: makefile: 6
file content (225 lines) | stat: -rw-r--r-- 5,716 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
216
217
218
219
220
221
222
223
224
225
package sourcemap_test

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"testing"

	"gopkg.in/sourcemap.v1"
)

const jqSourceMapURL = "file:///usr/share/javascript/jquery/jquery.min.map"

var jqSourceMapBytes []byte

func init() {
	trans := &http.Transport{}
	trans.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))

	cli := &http.Client{Transport: trans}
	resp, err := cli.Get(jqSourceMapURL)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	jqSourceMapBytes, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
}

type sourceMapTest struct {
	genLine      int
	genCol       int
	wantedSource string
	wantedName   string
	wantedLine   int
	wantedCol    int
}

func (test *sourceMapTest) String() string {
	return fmt.Sprintf("line=%d col=%d in file=%s", test.genLine, test.genCol, test.wantedSource)
}

func (test *sourceMapTest) assert(t *testing.T, smap *sourcemap.Consumer) {
	source, name, line, col, ok := smap.Source(test.genLine, test.genCol)
	if !ok {
		if test.wantedSource == "" &&
			test.wantedName == "" &&
			test.wantedLine == 0 &&
			test.wantedCol == 0 {
			return
		}
		t.Fatalf("Source not found for %s", test)
	}
	if source != test.wantedSource {
		t.Fatalf("file: got %q, wanted %q (%s)", source, test.wantedSource, test)
	}
	if name != test.wantedName {
		t.Fatalf("func: got %q, wanted %q (%s)", name, test.wantedName, test)
	}
	if line != test.wantedLine {
		t.Fatalf("line: got %d, wanted %d (%s)", line, test.wantedLine, test)
	}
	if col != test.wantedCol {
		t.Fatalf("column: got %d, wanted %d (%s)", col, test.wantedCol, test)
	}
}

func TestSourceMap(t *testing.T) {
	smap, err := sourcemap.Parse("", []byte(sourceMapJSON))
	if err != nil {
		t.Fatal(err)
	}

	tests := []*sourceMapTest{
		{1, 1, "/the/root/one.js", "", 1, 1},
		{1, 5, "/the/root/one.js", "", 1, 5},
		{1, 9, "/the/root/one.js", "", 1, 11},
		{1, 18, "/the/root/one.js", "bar", 1, 21},
		{1, 21, "/the/root/one.js", "", 2, 3},
		{1, 28, "/the/root/one.js", "baz", 2, 10},
		{1, 32, "/the/root/one.js", "bar", 2, 14},

		{2, 1, "/the/root/two.js", "", 1, 1},
		{2, 5, "/the/root/two.js", "", 1, 5},
		{2, 9, "/the/root/two.js", "", 1, 11},
		{2, 18, "/the/root/two.js", "n", 1, 21},
		{2, 21, "/the/root/two.js", "", 2, 3},
		{2, 28, "/the/root/two.js", "n", 2, 10},

		// Fuzzy match.
		{1, 20, "/the/root/one.js", "bar", 1, 21},
		{1, 30, "/the/root/one.js", "baz", 2, 10},
		{2, 12, "/the/root/two.js", "", 1, 11},
	}
	for _, test := range tests {
		test.assert(t, smap)
	}

	_, _, _, _, ok := smap.Source(3, 0)
	if ok {
		t.Fatal("source must not exist")
	}
}

func TestSourceRootURL(t *testing.T) {
	jsonStr := sourceMapJSON
	jsonStr = strings.Replace(jsonStr, "/the/root", "http://the/root", 1)
	jsonStr = strings.Replace(jsonStr, "one.js", "../one.js", 1)

	smap, err := sourcemap.Parse("", []byte(jsonStr))
	if err != nil {
		t.Fatal(err)
	}

	tests := []*sourceMapTest{
		{1, 1, "http://the/one.js", "", 1, 1},
		{2, 1, "http://the/root/two.js", "", 1, 1},
	}
	for _, test := range tests {
		test.assert(t, smap)
	}
}

func TestEmptySourceRootURL(t *testing.T) {
	jsonStr := sourceMapJSON
	jsonStr = strings.Replace(jsonStr, "/the/root", "", 1)
	jsonStr = strings.Replace(jsonStr, "one.js", "../one.js", 1)

	smap, err := sourcemap.Parse("http://the/root/app.min.map", []byte(jsonStr))
	if err != nil {
		t.Fatal(err)
	}

	tests := []*sourceMapTest{
		{1, 1, "http://the/one.js", "", 1, 1},
		{2, 1, "http://the/root/two.js", "", 1, 1},
	}
	for _, test := range tests {
		test.assert(t, smap)
	}
}

func TestAbsSourceURL(t *testing.T) {
	jsonStr := sourceMapJSON
	jsonStr = strings.Replace(jsonStr, "/the/root", "", 1)
	jsonStr = strings.Replace(jsonStr, "one.js", "http://the/root/one.js", 1)
	jsonStr = strings.Replace(jsonStr, "two.js", "/another/root/two.js", 1)

	testAbsSourceURL(t, "", jsonStr)
	testAbsSourceURL(t, "http://path/to/map", jsonStr)
}

func testAbsSourceURL(t *testing.T, mapURL, jsonStr string) {
	smap, err := sourcemap.Parse(mapURL, []byte(jsonStr))
	if err != nil {
		t.Fatal(err)
	}

	tests := []*sourceMapTest{
		{1, 1, "http://the/root/one.js", "", 1, 1},
		{2, 1, "/another/root/two.js", "", 1, 1},
	}
	for _, test := range tests {
		test.assert(t, smap)
	}
}

/*
func TestJQuerySourceMap(t *testing.T) {
	smap, err := sourcemap.Parse(jqSourceMapURL, jqSourceMapBytes)
	if err != nil {
		t.Fatal(err)
	}

	tests := []*sourceMapTest{
		{1, 1, "", "", 0, 0},
		{4, 0, "", "", 0, 0},
		{4, 1, "http://code.jquery.com/jquery-2.0.3.js", "", 14, 0},
		{4, 10, "http://code.jquery.com/jquery-2.0.3.js", "window", 14, 11},
		{5, 6789, "http://code.jquery.com/jquery-2.0.3.js", "apply", 4360, 27},
		{5, 10006, "http://code.jquery.com/jquery-2.0.3.js", "apply", 4676, 8},
		{4, 553, "http://code.jquery.com/jquery-2.0.3.js", "ready", 93, 9},
		{999999, 0, "", "", 0, 0},
	}
	for _, test := range tests {
		test.assert(t, smap)
	}
}
*/

// This is a test mapping which maps functions from two different files
// (one.js and two.js) to a minified generated source.
//
// Here is one.js:
//
//     ONE.foo = function (bar) {
//       return baz(bar);
//     };
//
// Here is two.js:
//
//     TWO.inc = function (n) {
//       return n + 1;
//     };
//
// And here is the generated code (min.js):
//
//     ONE.foo=function(a){return baz(a);};
//     TWO.inc=function(a){return a+1;};

var genCode = `exports.testGeneratedCode = "ONE.foo=function(a){return baz(a);};
TWO.inc=function(a){return a+1;};`

var sourceMapJSON = `{
  "version": 3,
  "file": "min.js",
  "names": ["bar", "baz", "n"],
  "sources": ["one.js", "two.js"],
  "sourceRoot": "/the/root",
  "mappings": "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA"
}`