-
Notifications
You must be signed in to change notification settings - Fork 29
/
compiler_test.go
194 lines (162 loc) · 3.85 KB
/
compiler_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
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
package libsass
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"testing"
)
func ExampleCompiler_stdin() {
src := bytes.NewBufferString(`div { p { color: red; } }`)
comp, err := New(os.Stdout, src)
if err != nil {
log.Fatal(err)
}
err = comp.Run()
if err != nil {
log.Fatal(err)
}
// Output:
// div p {
// color: red; }
//
}
func ExampleComipler_sass() {
src := bytes.NewBufferString(`
html
font-family: 'MonoSocial'
`)
comp, err := New(os.Stdout, src, WithSyntax(SassSyntax))
if err != nil {
log.Fatal(err)
}
err = comp.Run()
if err != nil {
log.Fatal(err)
}
// Output:
// html {
// font-family: 'MonoSocial'; }
}
func TestCompiler_path(t *testing.T) {
var dst bytes.Buffer
comp, err := New(&dst, nil, Path("test/scss/basic.scss"))
if err != nil {
t.Fatal(err)
}
err = comp.Run()
if err != nil {
t.Fatal(err)
}
e := `div p {
color: red; }
`
if e != dst.String() {
t.Errorf("got: %s wanted: %s", dst.String(), e)
}
if e := 1; len(comp.Imports()) != e {
t.Errorf("got: %d wanted: %d", len(comp.Imports()), e)
}
}
func TestCompiler_path_withpathsresolver(t *testing.T) {
var dst bytes.Buffer
absArgs := [][]string{}
comp, err := New(
&dst,
nil,
Path("test/scss/file.scss"),
ImportsOption(NewImportsWithResolver(func(importedUrl string, importerAbsPath string) (string, string, bool) {
absArgs = append(absArgs, []string{importedUrl, importerAbsPath})
if importedUrl == "a" {
return "test/scss/a.scss", ".a { color: #aaaaaa }\n@import 'b';", true
} else if importedUrl == "b" {
return "test/scss/b.scss", ".b { color: #bbbbbb }", true
}
return "", "", false
})),
)
if err != nil {
t.Fatal(err)
}
err = comp.Run()
if err != nil {
t.Fatal(err)
}
e := `.a {
color: #aaaaaa; }
.b {
color: #bbbbbb; }
`
if e != dst.String() {
t.Errorf("got: %s wanted: %s", dst.String(), e)
}
if e := 3; len(comp.Imports()) != e {
t.Errorf("got: %d wanted: %d", len(comp.Imports()), e)
}
expectedAbsArgs := `[[a test/scss/file.scss] [b a]]`
if absArgsStr := fmt.Sprintf("%+v", absArgs); expectedAbsArgs != absArgsStr {
t.Errorf("abs args got: %s wanted: %s", absArgsStr, expectedAbsArgs)
}
}
func TestCompiler_path_withabsresolver(t *testing.T) {
var dst bytes.Buffer
absArgs := [][]string{}
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
comp, err := New(
&dst,
nil,
Path("test/scss/file.scss"),
ImportsOption(NewImportsWithAbsResolver(func(importedUrl string, importerAbsPath string) (string, string, bool) {
// convert the path to a stable representation
// (convert abs path to a relative path, with the prefix $CWD/)
var pathToSave string
if filepath.IsAbs(importerAbsPath) {
pathToSave, err = filepath.Rel(wd, importerAbsPath)
if err != nil {
t.Fatal(err)
}
pathToSave = "$CWD/" + pathToSave
} else {
pathToSave = importerAbsPath
}
// save the args
absArgs = append(absArgs, []string{
importedUrl,
filepath.ToSlash(pathToSave),
})
// handle magic modules "a" and "b"
if importedUrl == "a" {
return wd + filepath.FromSlash("/test/scss/a.scss"), ".a { color: #aaaaaa }\n@import 'b';", true
} else if importedUrl == "b" {
return wd + filepath.FromSlash("/test/scss/b.scss"), ".b { color: #bbbbbb }", true
}
return "", "", false
})),
)
if err != nil {
t.Fatal(err)
}
err = comp.Run()
if err != nil {
t.Fatal(err)
}
e := `.a {
color: #aaaaaa; }
.b {
color: #bbbbbb; }
`
if e != dst.String() {
t.Errorf("got: %s wanted: %s", dst.String(), e)
}
if e := 3; len(comp.Imports()) != e {
t.Errorf("got: %d wanted: %d", len(comp.Imports()), e)
}
expectedAbsArgs := `[[a $CWD/test/scss/file.scss] [b $CWD/test/scss/a.scss]]`
if absArgsStr := fmt.Sprintf("%+v", absArgs); expectedAbsArgs != absArgsStr {
t.Errorf("abs args got: %s wanted: %s", absArgsStr, expectedAbsArgs)
}
}