-
Notifications
You must be signed in to change notification settings - Fork 15
/
raw_op_test.go
103 lines (92 loc) · 1.9 KB
/
raw_op_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
package quill
import (
"reflect"
"testing"
)
func TestRawOp_makeOp(t *testing.T) {
rawOps := []rawOp{
{
Insert: "stuff to insert.\n",
Attrs: map[string]interface{}{
"bold": true,
"link": "https://widerwebs.com",
"italic": false,
"underline": nil, // nil value is set if JSON value is null
},
},
{
Insert: "\n",
Attrs: map[string]interface{}{
"align": "center",
},
},
{
Insert: "\n",
Attrs: map[string]interface{}{
"align": "center",
"blockquote": true,
},
},
{
Insert: map[string]interface{}{
"image": "url-or-base64",
},
},
}
want := []Op{
{
Data: "stuff to insert.\n",
Type: "text",
Attrs: map[string]string{
"bold": "y",
"italic": "",
"link": "https://widerwebs.com",
"underline": "",
},
},
{
Data: "\n",
Type: "text",
Attrs: map[string]string{
"align": "center",
},
},
{
Data: "\n",
Type: "text",
Attrs: map[string]string{
"align": "center",
"blockquote": "y",
},
},
{
Data: "url-or-base64",
Type: "image",
Attrs: make(map[string]string), // like in code (already initialized)
},
}
o := new(Op) // reuse in loop
o.Attrs = make(map[string]string, 3) // initialize once here only (as in real code)
for i := range rawOps {
if err := rawOps[i].makeOp(o); err != nil {
t.Fatalf("error making Op: %s", err)
}
if !reflect.DeepEqual(*o, want[i]) {
t.Errorf("failed Op comparison; got %+v for index %d", o, i)
}
}
}
func TestExtractString(t *testing.T) {
if extractString("random string") != "random string" {
t.Errorf("failed stringc extract")
}
if extractString(true) != "y" {
t.Errorf("failed bool true extract")
}
if extractString(false) != "" {
t.Errorf("failed bool false extract")
}
if extractString(float64(3)) != "3" {
t.Errorf("failed float64 extract")
}
}