forked from plandem/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rich_text_test.go
126 lines (111 loc) · 2.48 KB
/
rich_text_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
// Copyright (c) 2017 Andrey Gayvoronsky <[email protected]>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package xlsx
import (
"github.com/plandem/xlsx/format/styles"
"github.com/plandem/xlsx/internal/ml"
"github.com/plandem/xlsx/types"
"github.com/stretchr/testify/require"
"testing"
)
func TestToRichFont(t *testing.T) {
style := styles.New(
styles.Font.Size(8),
styles.Font.Color("#FF1122"),
)
font := toRichFont(style)
require.IsType(t, &ml.RichFont{}, font)
require.Equal(t, &ml.RichFont{
Size: 8,
Color: &ml.Color{RGB: "FFFF1122"},
}, font)
}
func TestToRichText(t *testing.T) {
s := styles.New(
styles.Alignment.HAlign(styles.HAlignCenter),
)
text, cellStyles, err := toRichText(
//normal strings
"1", "2", "3",
//fmt.Stringer
types.BoundsFromIndexes(0, 0, 1, 1),
//custom type with underlying type as string
types.CellRefFromIndexes(2, 2),
//cell styles
s,
)
require.Nil(t, err)
require.Equal(t, &ml.StringItem{
RichText: []*ml.RichText{
{
Text: "1",
},
{
Text: "2",
},
{
Text: "3",
},
{
Text: "A1:B2",
},
{
Text: "C3",
},
},
}, text)
require.Equal(t, s, cellStyles)
text, cellStyles, err = toRichText(styles.New(
styles.Font.Color("#FF1122"),
), "1", styles.New(
styles.Font.Size(8),
styles.Font.Color("#FF3344"),
), "2")
require.Nil(t, err)
require.Equal(t, &ml.StringItem{
RichText: []*ml.RichText{
{
Text: "1",
Font: &ml.RichFont{
Color: &ml.Color{RGB: "FFFF1122"},
},
},
{
Text: "2",
Font: &ml.RichFont{
Size: 8,
Color: &ml.Color{RGB: "FFFF3344"},
},
},
},
}, text)
require.Nil(t, cellStyles)
text, cellStyles, err = toRichText("1", "2", "3", styles.New(
styles.Font.Color("#FF3344"),
), styles.New(
styles.Font.Color("#FF3344"),
), "4")
require.NotNil(t, err)
require.Nil(t, text)
require.Nil(t, cellStyles)
}
func TestFromRichText(t *testing.T) {
s := styles.New(
styles.Alignment.HAlign(styles.HAlignCenter),
)
text, cellStyles, err := toRichText("1", "2", "3", s)
require.Nil(t, err)
require.Equal(t, "123", fromRichText(text))
require.Equal(t, s, cellStyles)
text, cellStyles, err = toRichText(styles.New(
styles.Font.Color("#FF1122"),
), "1", styles.New(
styles.Font.Size(8),
styles.Font.Color("#FF3344"),
), "2")
require.Nil(t, err)
require.Equal(t, "12", fromRichText(text))
require.Equal(t, "", fromRichText(nil))
require.Nil(t, cellStyles)
}