forked from plandem/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlsx_test.go
110 lines (92 loc) · 2.5 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
// Copyright (c) 2017 Andrey Gayvoronsky <plandem@gmail.com>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package xlsx_test
import (
"bytes"
"github.com/plandem/xlsx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
)
func TestNew(t *testing.T) {
xl := xlsx.New()
assert.NotNil(t, xl)
assert.IsType(t, &xlsx.Spreadsheet{}, xl)
}
func TestOpening(t *testing.T) {
//can't open
xl, err := xlsx.Open("./test_files/unknown_file.xlsx")
assert.Nil(t, xl)
assert.NotNil(t, err)
//non zip
xl, err = xlsx.Open("./xlsx.go")
assert.Nil(t, xl)
assert.NotNil(t, err)
//ok
xl, err = xlsx.Open("./test_files/example_simple.xlsx")
assert.NotNil(t, xl)
assert.Nil(t, err)
assert.IsType(t, &xlsx.Spreadsheet{}, xl)
//non zip
nonZipFile, err := os.Open("./xlsx.go")
assert.Nil(t, err)
xl, err = xlsx.Open(nonZipFile)
assert.NotNil(t, err)
assert.Nil(t, xl)
//zip file
zipFile, err := os.Open("./test_files/example_simple.xlsx")
assert.NotNil(t, zipFile)
assert.Nil(t, err)
//ok
xl, err = xlsx.Open(zipFile)
assert.NotNil(t, xl)
assert.Nil(t, err)
assert.IsType(t, &xlsx.Spreadsheet{}, xl)
}
func TestSaving(t *testing.T) {
xl := xlsx.New()
assert.NotNil(t, xl)
assert.IsType(t, &xlsx.Spreadsheet{}, xl)
//no filename
err := xl.Save()
assert.NotNil(t, err)
//no sheets
err = xl.SaveAs("./test_files/tmp.xlsx")
assert.NotNil(t, err)
//ok
xl.AddSheet("new sheet")
err = xl.SaveAs("./test_files/tmp.xlsx")
assert.Nil(t, err)
//save to io.Writer
xl, err = xlsx.Open("./test_files/example_simple.xlsx")
require.NotNil(t, xl)
require.Nil(t, err)
require.IsType(t, &xlsx.Spreadsheet{}, xl)
err = xl.SaveAs(bytes.NewBuffer(nil))
require.Nil(t, err)
//save under differ name
xl, err = xlsx.Open("./test_files/example_simple.xlsx")
require.NotNil(t, xl)
require.Nil(t, err)
require.IsType(t, &xlsx.Spreadsheet{}, xl)
err = xl.SaveAs("./test_files/tmp.xlsx")
require.Nil(t, err)
//open saved
xl, err = xlsx.Open("./test_files/tmp.xlsx")
require.NotNil(t, xl)
require.Nil(t, err)
require.IsType(t, &xlsx.Spreadsheet{}, xl)
assert.Equal(t, []string{"Sheet1"}, xl.SheetNames())
//save with same name
xl.AddSheet("new sheet")
err = xl.Save()
assert.Nil(t, err)
//open saved and check for a new sheet
xl, err = xlsx.Open("./test_files/tmp.xlsx")
require.NotNil(t, xl)
require.Nil(t, err)
require.IsType(t, &xlsx.Spreadsheet{}, xl)
require.Equal(t, []string{"Sheet1", "new sheet"}, xl.SheetNames())
}