-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
71 lines (60 loc) · 1.53 KB
/
writer_test.go
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
package sourcemap
import (
"bufio"
"bytes"
"encoding/json"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
func Test(t *testing.T) {
assert := assert.New(t)
f1 := "alert('f1-1');\nalert('f1-2');\nalert('f1-3');\n;\nalert('f1-4');\n\n"
f2 := "alert('f2-1');\nalert('f2-2');\n"
var out bytes.Buffer
w, err := NewWriter(&out, "out.js", []string{"f1.js", "f2.js"})
assert.Nil(err)
for i, f := range []string{f1, f2} {
w.NextSource()
ln := 0
sf := bufio.NewScanner(strings.NewReader(f))
for sf.Scan() {
w.WriteSourceLine(sf.Text())
w.WriteGeneratedLine(Segment{ln, 0, i, ln, 0, 0})
ln++
}
w.WriteSourceLine("")
w.WriteGeneratedLine(Segment{ln, 0, i, ln, 0, 0})
}
w.Close()
type Result struct {
Mappings string `json:"mappings"`
}
var result Result
err = json.NewDecoder(&out).Decode(&result)
assert.Nil(err)
assert.Equal("AAAA;AACA;AACA;AACA;AACA;AACA;AACA;ACNA;AACA;AACA", result.Mappings)
}
func TestWrite(t *testing.T) {
assert := assert.New(t)
tf := filepath.Join(os.TempDir(), "test-write.map")
defer os.Remove(tf)
sm := &SourceMap{
File: "test.js",
Mappings: [][]Segment{
{{0, 0, 0, 0, 0, 0}},
{{1, 0, 0, 1, 0, 0}},
{{2, 0, 1, 0, 0, 0}},
{{2, 0, 1, 1, 0, 0}},
},
Sources: []string{"f1.js", "j2.js"},
SourcesContent: []string{"alert('f1-1');\nalert('f1-2');\nalert('f1-3');\n;\nalert('f1-4');\n\n", "alert('f2-1');\nalert('f2-2');\n"},
}
err := Write(tf, sm)
assert.Nil(err)
_, err = ioutil.ReadFile(tf)
assert.Nil(err)
}