-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
html_default_page_test.go
62 lines (50 loc) · 1.54 KB
/
html_default_page_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
package htmljson_test
import (
_ "embed"
"encoding/json"
"os"
"strings"
"testing"
"github.com/nikolaydubina/htmljson"
)
//go:embed testdata/example-page.html
var examplePageHTML string
//go:embed testdata/example-page-color.html
var examplePageColorHTML string
func TestMarshalHTML(t *testing.T) {
var v any
json.Unmarshal(exampleJSON, &v)
h := htmljson.DefaultPageMarshaler.Marshal(v)
os.WriteFile("testdata/example-page.out.html", h, 0666)
if strings.TrimSpace(examplePageHTML) != strings.TrimSpace(string(h)) {
t.Errorf("wrong output: %s", string(h))
}
}
func TestMarshalHTML_Color(t *testing.T) {
var v any
json.Unmarshal(exampleJSON, &v)
s := htmljson.Marshaler{
Null: htmljson.NullHTML,
Bool: htmljson.BoolHTML,
String: htmljson.StringHTML,
Number: func(k string, v float64, s string) string {
if k == "$.cakes.strawberry-cake.size" {
return `<div class="json-value json-number" style="color:red;">` + s + `</div>`
}
if v > 10 {
return `<div class="json-value json-number" style="color:blue;">` + s + `</div>`
}
return `<div class="json-value json-number">` + s + `</div>`
},
Array: htmljson.DefaultArrayHTML,
Map: htmljson.DefaultMapHTML,
Row: htmljson.DefaultRowHTML{Padding: 4}.Marshal,
}
m := htmljson.DefaultPageMarshaler
m.Marshaler = &s
htmlPage := m.Marshal(v)
os.WriteFile("testdata/example-page-color.out.html", []byte(htmlPage), 0666)
if strings.TrimSpace(examplePageColorHTML) != strings.TrimSpace(string(htmlPage)) {
t.Errorf("wrong output: %s", string(htmlPage))
}
}