-
Notifications
You must be signed in to change notification settings - Fork 0
/
v8_test.go
120 lines (106 loc) · 3.19 KB
/
v8_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
package svelte_test
import (
"strings"
"testing"
"github.com/dvcorreia/go-svelte"
v8 "github.com/livebud/bud/package/js/v8"
"github.com/matryer/is"
)
func BenchmarkV8SSR(b *testing.B) {
benchmarkV8SSR(b)
}
func benchmarkV8SSR(b *testing.B) {
vm, err := v8.Load()
if err != nil {
b.Fatal(err)
}
compiler, err := svelte.LoadV8(vm, svelte.GenerateSSR)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := compiler.Compile("test.svelte", benchCode)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkV8DOM(b *testing.B) {
benchmarkV8DOM(b)
}
func benchmarkV8DOM(b *testing.B) {
vm, err := v8.Load()
if err != nil {
b.Fatal(err)
}
compiler, err := svelte.LoadV8(vm, svelte.GenerateDOM)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := compiler.Compile("test.svelte", benchCode)
if err != nil {
b.Fatal(err)
}
}
}
func TestV8SSR(t *testing.T) {
is := is.New(t)
vm, err := v8.Load()
is.NoErr(err)
compiler, err := svelte.LoadV8(vm, svelte.GenerateSSR)
is.NoErr(err)
r, err := compiler.Compile("test.svelte", []byte(`<h1>hi world!</h1>`))
is.NoErr(err)
is.True(strings.Contains(r.JS, `import { create_ssr_component } from "svelte/internal";`))
is.True(strings.Contains(r.JS, `<h1>hi world!</h1>`))
}
func TestV8SSRRecovery(t *testing.T) {
is := is.New(t)
vm, err := v8.Load()
is.NoErr(err)
compiler, err := svelte.LoadV8(vm, svelte.GenerateSSR)
is.NoErr(err)
ssr, err := compiler.Compile("test.svelte", []byte(`<h1>hi world!</h1></h1>`))
is.True(err != nil)
is.True(strings.Contains(err.Error(), `</h1> attempted to close an element that was not open`))
is.True(strings.Contains(err.Error(), `<h1>hi world!</h1></h1`))
is.Equal(ssr, nil)
ssr, err = compiler.Compile("test.svelte", []byte(`<h1>hi world!</h1>`))
is.NoErr(err)
is.True(strings.Contains(ssr.JS, `import { create_ssr_component } from "svelte/internal";`))
is.True(strings.Contains(ssr.JS, `<h1>hi world!</h1>`))
}
func TestV8DOM(t *testing.T) {
is := is.New(t)
vm, err := v8.Load()
is.NoErr(err)
compiler, err := svelte.LoadV8(vm, svelte.GenerateDOM)
is.NoErr(err)
dom, err := compiler.Compile("test.svelte", []byte(`<h1>hi world!</h1>`))
is.NoErr(err)
is.True(strings.Contains(dom.JS, `from "svelte/internal"`))
is.True(strings.Contains(dom.JS, `function create_fragment`))
is.True(strings.Contains(dom.JS, `element("h1")`))
is.True(strings.Contains(dom.JS, `text("hi world!")`))
}
func TestV8DOMRecovery(t *testing.T) {
is := is.New(t)
vm, err := v8.Load()
is.NoErr(err)
compiler, err := svelte.LoadV8(vm, svelte.GenerateDOM)
is.NoErr(err)
dom, err := compiler.Compile("test.svelte", []byte(`<h1>hi world!</h1></h1>`))
is.True(err != nil)
is.True(strings.Contains(err.Error(), `</h1> attempted to close an element that was not open`))
is.True(strings.Contains(err.Error(), `<h1>hi world!</h1></h1`))
is.Equal(dom, nil)
dom, err = compiler.Compile("test.svelte", []byte(`<h1>hi world!</h1>`))
is.NoErr(err)
is.True(strings.Contains(dom.JS, `from "svelte/internal"`))
is.True(strings.Contains(dom.JS, `function create_fragment`))
is.True(strings.Contains(dom.JS, `element("h1")`))
is.True(strings.Contains(dom.JS, `text("hi world!")`))
}