forked from SebastiaanKlippert/go-wkhtmltopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
139 lines (111 loc) · 2.87 KB
/
json_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
package wkhtmltopdf
import (
"bytes"
"encoding/json"
"io"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPDFGenerator_ToJSON(t *testing.T) {
pdfg := newTestPDFGenerator(t)
// add a reader page as well
htmlfile, err := os.Open("testdata/htmlsimple.html")
if err != nil {
t.Fatal(err)
}
defer htmlfile.Close()
pdfg.AddPage(NewPageReader(htmlfile))
jb, err := pdfg.ToJSON()
if err != nil {
t.Fatal(err)
}
expected, err := os.ReadFile("testdata/expected.json")
if err != nil {
t.Fatal(err)
}
assert.JSONEq(t, string(expected), string(jb))
}
func TestNewPDFGeneratorFromJSON(t *testing.T) {
pdfg := newTestPDFGenerator(t)
jb, err := pdfg.ToJSON()
if err != nil {
t.Fatal(err)
}
pdfgFromJSON, err := NewPDFGeneratorFromJSON(bytes.NewReader(jb))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expectedArgString(), pdfgFromJSON.ArgString())
pdfgFromJSON.OutputFile = "testdata/TestNewPDFGeneratorFromJSON.PDF"
err = pdfgFromJSON.Create()
if err != nil {
t.Fatal(err)
}
}
func TestNewPDFGeneratorFromJSONWithReader(t *testing.T) {
pdfg := NewPDFPreparer()
htmlfile, err := os.ReadFile("testdata/htmlsimple.html")
if err != nil {
t.Fatal(err)
}
pdfg.AddPage(NewPageReader(bytes.NewReader(htmlfile)))
jb, err := pdfg.ToJSON()
if err != nil {
t.Fatal(err)
}
pdfgFromJSON, err := NewPDFGeneratorFromJSON(bytes.NewReader(jb))
if err != nil {
t.Fatal(err)
}
// assert argstring
if pdfg.ArgString() != pdfgFromJSON.ArgString() {
t.Errorf("Want argstring:\n%s\nHave:\n%s", pdfg.ArgString(), pdfgFromJSON.ArgString())
}
// assert content
buf, err := io.ReadAll(pdfgFromJSON.pages[0].Reader())
if err != nil {
t.Fatal(err)
}
if string(buf) != string(htmlfile) {
t.Errorf("Want HTML:\n%s\nHave:\n%s", string(htmlfile), string(buf))
}
}
func TestBoolOption_JSON(t *testing.T) {
bo := &boolOption{"option", true}
assertJSON(t, bo, new(boolOption))
}
func TestFloatOptionJSON(t *testing.T) {
fo := &floatOption{"option", 1.11, true}
assertJSON(t, fo, new(floatOption))
}
func TestMapOption_JSON(t *testing.T) {
mo := &mapOption{"option", map[string]string{"foo1": "bar1", "foo2": "bar2"}}
assertJSON(t, mo, new(mapOption))
}
func TestUintOption_JSON(t *testing.T) {
uo := &uintOption{"option", 111, true}
assertJSON(t, uo, new(uintOption))
}
func TestStringOption_JSON(t *testing.T) {
so := &stringOption{"option", "abc"}
assertJSON(t, so, new(stringOption))
}
func TestSliceOption_JSON(t *testing.T) {
so := &sliceOption{"option", []string{"foo", "bar"}}
assertJSON(t, so, new(sliceOption))
}
func assertJSON(t *testing.T, option, newOption interface{}) {
j, err := json.Marshal(option)
if err != nil {
t.Error(err)
}
err = json.Unmarshal(j, newOption)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(option, newOption) {
t.Errorf("Diff after marshal and unmarshal:\n%+v\n%+v", option, newOption)
}
}