This repository was archived by the owner on Nov 27, 2019. It is now read-only.
forked from psmithuk/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxlsx_test.go
125 lines (103 loc) · 2.91 KB
/
xlsx_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
package xlsx
import (
"bytes"
"fmt"
"io"
"testing"
"time"
)
type CellIndexTestCase struct {
x uint64
y uint64
expected string
}
func TestCellIndex(t *testing.T) {
tests := []CellIndexTestCase{
CellIndexTestCase{0, 0, "A1"},
CellIndexTestCase{2, 2, "C3"},
CellIndexTestCase{26, 45, "AA46"},
CellIndexTestCase{2600, 100000, "CVA100001"},
}
for _, c := range tests {
cellX, cellY := CellIndex(c.x, c.y)
s := fmt.Sprintf("%s%d", cellX, cellY)
if s != c.expected {
t.Errorf("expected %s, got %s", c.expected, s)
}
}
}
type OADateTestCase struct {
datetime time.Time
expected string
}
func TestOADate(t *testing.T) {
tests := []OADateTestCase{
OADateTestCase{time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC), "25569"},
OADateTestCase{time.Date(1970, 1, 1, 12, 20, 0, 0, time.UTC), "25569.513889"},
OADateTestCase{time.Date(2014, 12, 20, 0, 0, 0, 0, time.UTC), "41993"},
}
for _, d := range tests {
s := OADate(d.datetime)
if s != d.expected {
t.Errorf("expected %s, got %s", d.expected, s)
}
}
}
func TestTemplates(t *testing.T) {
var b bytes.Buffer
var err error
var s Sheet
var sheetNames = []string{"SheetOne", "SheetTwo"}
err = TemplateContentTypes.Execute(&b, nil)
if err != nil {
t.Errorf("template TemplateContentTypes failed to Execute returning error %s", err.Error())
}
err = TemplateRelationships.Execute(&b, nil)
if err != nil {
t.Errorf("template TemplateRelationships failed to Execute returning error %s", err.Error())
}
err = TemplateApp.Execute(&b, sheetNames)
if err != nil {
t.Errorf("template TemplateApp failed to Execute returning error %s", err.Error())
}
err = TemplateCore.Execute(&b, s.DocumentInfo)
if err != nil {
t.Errorf("template TemplateCore failed to Execute returning error %s", err.Error())
}
err = TemplateWorkbook.Execute(&b, nil)
if err != nil {
t.Errorf("template TemplateWorkbook failed to Execute returning error %s", err.Error())
}
err = TemplateWorkbookRelationships.Execute(&b, sheetNames)
if err != nil {
t.Errorf("template TemplateWorkbookRelationships failed to Execute returning error %s", err.Error())
}
err = TemplateStyles.Execute(&b, nil)
if err != nil {
t.Errorf("template TemplateStyles failed to Execute returning error %s", err.Error())
}
err = TemplateStringLookups.Execute(&b, []string{})
if err != nil {
t.Errorf("template TemplateStringLookups failed to Execute returning error %s", err.Error())
}
sheet := struct {
Cols []Column
Rows []string
Start string
End string
}{
Cols: []Column{},
Rows: []string{},
Start: "A1",
End: "C3",
}
err = TemplateSheetStart.Execute(&b, sheet)
if err != nil {
t.Errorf("template TemplateSheetStart failed to Execute returning error %s", err.Error())
}
for i, _ := range sheet.Rows {
rb := &bytes.Buffer{}
rowString := fmt.Sprintf(`<row r="%d">%s</row>`, uint64(i), rb.String())
_, err = io.WriteString(&b, rowString)
}
}