-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtailwind_test.go
83 lines (77 loc) · 2.02 KB
/
tailwind_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
package tailwind_test
import (
"context"
"fmt"
"os"
"strings"
"testing"
"github.com/livebud/js"
v8 "github.com/livebud/js/v8"
"github.com/livebud/tailwind"
"github.com/matryer/is"
)
type Test struct {
Path string
Input string
Contains []string
Error string
}
func runTest(test Test) error {
ctx := context.Background()
vm, err := v8.Load(&js.Console{
Log: os.Stdout,
Error: os.Stderr,
})
if err != nil {
if err.Error() == test.Error {
return nil
}
return fmt.Errorf("v8: load error: %w", err)
}
defer vm.Close()
processor := tailwind.New(vm)
css, err := processor.Process(ctx, test.Path, test.Input)
if err != nil {
if err.Error() == test.Error {
return nil
}
return fmt.Errorf("tailwind: process error: %w", err)
}
for _, contain := range test.Contains {
if !strings.Contains(css, contain) {
return fmt.Errorf("tailwind: expected result to contain %q.\n\n%q", contain, css)
}
}
return nil
}
func TestHTML(t *testing.T) {
is := is.New(t)
is.NoErr(runTest(Test{
Path: "index.html",
Input: `
<h1 class="bg-[url(/img/grid.svg)] text-sky-500 hover:text-sky-600">Hello {name}!</h1>
`,
Contains: []string{
// ".bg-\\[url\\(\\/img\\/grid\\.svg\\)\\]{background-image:url(/img/grid.svg)}",
// ".text-sky-500{--tw-text-opacity:1;color:rgb(14 165 233/var(--tw-text-opacity))}",
// ".hover\\:text-sky-600:hover{--tw-text-opacity:1;color:rgb(2 132 199/var(--tw-text-opacity))}",
},
}))
}
func TestSvelte(t *testing.T) {
is := is.New(t)
is.NoErr(runTest(Test{
Path: "index.svelte",
Input: `
<script lang="typescript">
export let name: string = "Mark";
</script>
<h1 class="bg-[url(/img/grid.svg)] text-sky-500 hover:text-sky-600">Hello {name}!</h1>
`,
Contains: []string{
// ".bg-\\[url\\(\\/img\\/grid\\.svg\\)\\]{background-image:url(/img/grid.svg)}",
// ".text-sky-500{--tw-text-opacity:1;color:rgb(14 165 233/var(--tw-text-opacity))}",
// ".hover\\:text-sky-600:hover{--tw-text-opacity:1;color:rgb(2 132 199/var(--tw-text-opacity))}",
},
}))
}