Skip to content

Commit ae2865d

Browse files
committed
Improve code coverage unit tests
1 parent 7358dca commit ae2865d

16 files changed

+438
-142
lines changed

calcchain_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package excelize
2+
3+
import "testing"
4+
5+
func TestCalcChainReader(t *testing.T) {
6+
f := NewFile()
7+
f.CalcChain = nil
8+
f.XLSX["xl/calcChain.xml"] = MacintoshCyrillicCharset
9+
f.calcChainReader()
10+
}
11+
12+
func TestDeleteCalcChain(t *testing.T) {
13+
f := NewFile()
14+
f.CalcChain = &xlsxCalcChain{C: []xlsxCalcChainC{}}
15+
f.ContentTypes.Overrides = append(f.ContentTypes.Overrides, xlsxOverride{
16+
PartName: "/xl/calcChain.xml",
17+
})
18+
f.deleteCalcChain(1, "A1")
19+
}

cell_test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,15 @@ func TestSetCellBool(t *testing.T) {
9595
}
9696

9797
func TestGetCellFormula(t *testing.T) {
98+
// Test get cell formula on not exist worksheet.
9899
f := NewFile()
99-
f.GetCellFormula("Sheet", "A1")
100+
_, err := f.GetCellFormula("SheetN", "A1")
101+
assert.EqualError(t, err, "sheet SheetN is not exist")
102+
103+
// Test get cell formula on no formula cell.
104+
f.SetCellValue("Sheet1", "A1", true)
105+
_, err = f.GetCellFormula("Sheet1", "A1")
106+
assert.NoError(t, err)
100107
}
101108

102109
func ExampleFile_SetCellFloat() {

comment_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
2+
// this source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file.
4+
//
5+
// Package excelize providing a set of functions that allow you to write to
6+
// and read from XLSX files. Support reads and writes XLSX file generated by
7+
// Microsoft Excel™ 2007 and later. Support save file without losing original
8+
// charts of XLSX. This library needs Go version 1.10 or later.
9+
10+
package excelize
11+
12+
import (
13+
"path/filepath"
14+
"strings"
15+
"testing"
16+
17+
"github.com/stretchr/testify/assert"
18+
)
19+
20+
func TestAddComments(t *testing.T) {
21+
f, err := prepareTestBook1()
22+
if !assert.NoError(t, err) {
23+
t.FailNow()
24+
}
25+
26+
s := strings.Repeat("c", 32768)
27+
assert.NoError(t, f.AddComment("Sheet1", "A30", `{"author":"`+s+`","text":"`+s+`"}`))
28+
assert.NoError(t, f.AddComment("Sheet2", "B7", `{"author":"Excelize: ","text":"This is a comment."}`))
29+
30+
// Test add comment on not exists worksheet.
31+
assert.EqualError(t, f.AddComment("SheetN", "B7", `{"author":"Excelize: ","text":"This is a comment."}`), "sheet SheetN is not exist")
32+
33+
if assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddComments.xlsx"))) {
34+
assert.Len(t, f.GetComments(), 2)
35+
}
36+
}
37+
38+
func TestDecodeVMLDrawingReader(t *testing.T) {
39+
f := NewFile()
40+
path := "xl/drawings/vmlDrawing1.xml"
41+
f.XLSX[path] = MacintoshCyrillicCharset
42+
f.decodeVMLDrawingReader(path)
43+
}
44+
45+
func TestCommentsReader(t *testing.T) {
46+
f := NewFile()
47+
path := "xl/comments1.xml"
48+
f.XLSX[path] = MacintoshCyrillicCharset
49+
f.commentsReader(path)
50+
}
51+
52+
func TestCountComments(t *testing.T) {
53+
f := NewFile()
54+
f.Comments["xl/comments1.xml"] = nil
55+
assert.Equal(t, f.countComments(), 1)
56+
}

datavalidation_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package excelize
1111

1212
import (
1313
"path/filepath"
14+
"strings"
1415
"testing"
1516

1617
"github.com/stretchr/testify/assert"
@@ -85,4 +86,12 @@ func TestDataValidationError(t *testing.T) {
8586
if !assert.NoError(t, f.SaveAs(resultFile)) {
8687
t.FailNow()
8788
}
89+
90+
// Test width invalid data validation formula.
91+
dvRange.Formula1 = strings.Repeat("s", dataValidationFormulaStrLen+22)
92+
assert.EqualError(t, dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan), "data validation must be 0-255 characters")
93+
94+
// Test add data validation on no exists worksheet.
95+
f = NewFile()
96+
assert.EqualError(t, f.AddDataValidation("SheetN", nil), "sheet SheetN is not exist")
8897
}

docProps_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/stretchr/testify/assert"
1717
)
1818

19+
var MacintoshCyrillicCharset = []byte{0x8F, 0xF0, 0xE8, 0xE2, 0xE5, 0xF2, 0x20, 0xEC, 0xE8, 0xF0}
20+
1921
func TestSetDocProps(t *testing.T) {
2022
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
2123
if !assert.NoError(t, err) {
@@ -40,6 +42,11 @@ func TestSetDocProps(t *testing.T) {
4042
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetDocProps.xlsx")))
4143
f.XLSX["docProps/core.xml"] = nil
4244
assert.NoError(t, f.SetDocProps(&DocProperties{}))
45+
46+
// Test unsupport charset
47+
f = NewFile()
48+
f.XLSX["docProps/core.xml"] = MacintoshCyrillicCharset
49+
assert.EqualError(t, f.SetDocProps(&DocProperties{}), "xml decode error: XML syntax error on line 1: invalid UTF-8")
4350
}
4451

4552
func TestGetDocProps(t *testing.T) {
@@ -53,4 +60,10 @@ func TestGetDocProps(t *testing.T) {
5360
f.XLSX["docProps/core.xml"] = nil
5461
_, err = f.GetDocProps()
5562
assert.NoError(t, err)
63+
64+
// Test unsupport charset
65+
f = NewFile()
66+
f.XLSX["docProps/core.xml"] = MacintoshCyrillicCharset
67+
_, err = f.GetDocProps()
68+
assert.EqualError(t, err, "xml decode error: XML syntax error on line 1: invalid UTF-8")
5669
}

excelize_test.go

+104-29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package excelize
22

33
import (
44
"bytes"
5+
"compress/gzip"
6+
"encoding/xml"
57
"fmt"
68
"image/color"
79
_ "image/gif"
@@ -184,6 +186,11 @@ func TestSaveAsWrongPath(t *testing.T) {
184186
}
185187
}
186188

189+
func TestCharsetTranscoder(t *testing.T) {
190+
f := NewFile()
191+
f.CharsetTranscoder(*new(charsetTranscoderFn))
192+
}
193+
187194
func TestOpenReader(t *testing.T) {
188195
_, err := OpenReader(strings.NewReader(""))
189196
assert.EqualError(t, err, "zip: not a valid zip file")
@@ -195,6 +202,18 @@ func TestOpenReader(t *testing.T) {
195202
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
196203
}))
197204
assert.EqualError(t, err, "not support encrypted file currently")
205+
206+
// Test unexpected EOF.
207+
var b bytes.Buffer
208+
w := gzip.NewWriter(&b)
209+
defer w.Close()
210+
w.Flush()
211+
212+
r, _ := gzip.NewReader(&b)
213+
defer r.Close()
214+
215+
_, err = OpenReader(r)
216+
assert.EqualError(t, err, "unexpected EOF")
198217
}
199218

200219
func TestBrokenFile(t *testing.T) {
@@ -924,24 +943,6 @@ func TestAddShape(t *testing.T) {
924943
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape2.xlsx")))
925944
}
926945

927-
func TestAddComments(t *testing.T) {
928-
f, err := prepareTestBook1()
929-
if !assert.NoError(t, err) {
930-
t.FailNow()
931-
}
932-
933-
s := strings.Repeat("c", 32768)
934-
assert.NoError(t, f.AddComment("Sheet1", "A30", `{"author":"`+s+`","text":"`+s+`"}`))
935-
assert.NoError(t, f.AddComment("Sheet2", "B7", `{"author":"Excelize: ","text":"This is a comment."}`))
936-
937-
// Test add comment on not exists worksheet.
938-
assert.EqualError(t, f.AddComment("SheetN", "B7", `{"author":"Excelize: ","text":"This is a comment."}`), "sheet SheetN is not exist")
939-
940-
if assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddComments.xlsx"))) {
941-
assert.Len(t, f.GetComments(), 2)
942-
}
943-
}
944-
945946
func TestGetSheetComments(t *testing.T) {
946947
f := NewFile()
947948
assert.Equal(t, "", f.getSheetComments(0))
@@ -1005,18 +1006,37 @@ func TestAutoFilterError(t *testing.T) {
10051006
}
10061007
}
10071008

1008-
func TestSetPane(t *testing.T) {
1009+
func TestSetActiveSheet(t *testing.T) {
1010+
f := NewFile()
1011+
f.WorkBook.BookViews = nil
1012+
f.SetActiveSheet(1)
1013+
f.WorkBook.BookViews = &xlsxBookViews{WorkBookView: []xlsxWorkBookView{}}
1014+
f.Sheet["xl/worksheets/sheet1.xml"].SheetViews = &xlsxSheetViews{SheetView: []xlsxSheetView{}}
1015+
f.SetActiveSheet(1)
1016+
}
1017+
1018+
func TestSetSheetVisible(t *testing.T) {
1019+
f := NewFile()
1020+
f.WorkBook.Sheets.Sheet[0].Name = "SheetN"
1021+
assert.EqualError(t, f.SetSheetVisible("Sheet1", false), "sheet SheetN is not exist")
1022+
}
1023+
1024+
func TestGetActiveSheetIndex(t *testing.T) {
1025+
f := NewFile()
1026+
f.WorkBook.BookViews = nil
1027+
assert.Equal(t, 1, f.GetActiveSheetIndex())
1028+
}
1029+
1030+
func TestRelsWriter(t *testing.T) {
1031+
f := NewFile()
1032+
f.Relationships["xl/worksheets/sheet/rels/sheet1.xml.rel"] = &xlsxRelationships{}
1033+
f.relsWriter()
1034+
}
1035+
1036+
func TestGetSheetView(t *testing.T) {
10091037
f := NewFile()
1010-
f.SetPanes("Sheet1", `{"freeze":false,"split":false}`)
1011-
f.NewSheet("Panes 2")
1012-
f.SetPanes("Panes 2", `{"freeze":true,"split":false,"x_split":1,"y_split":0,"top_left_cell":"B1","active_pane":"topRight","panes":[{"sqref":"K16","active_cell":"K16","pane":"topRight"}]}`)
1013-
f.NewSheet("Panes 3")
1014-
f.SetPanes("Panes 3", `{"freeze":false,"split":true,"x_split":3270,"y_split":1800,"top_left_cell":"N57","active_pane":"bottomLeft","panes":[{"sqref":"I36","active_cell":"I36"},{"sqref":"G33","active_cell":"G33","pane":"topRight"},{"sqref":"J60","active_cell":"J60","pane":"bottomLeft"},{"sqref":"O60","active_cell":"O60","pane":"bottomRight"}]}`)
1015-
f.NewSheet("Panes 4")
1016-
f.SetPanes("Panes 4", `{"freeze":true,"split":false,"x_split":0,"y_split":9,"top_left_cell":"A34","active_pane":"bottomLeft","panes":[{"sqref":"A11:XFD11","active_cell":"A11","pane":"bottomLeft"}]}`)
1017-
f.SetPanes("Panes 4", "")
1018-
1019-
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetPane.xlsx")))
1038+
_, err := f.getSheetView("SheetN", 0)
1039+
assert.EqualError(t, err, "sheet SheetN is not exist")
10201040
}
10211041

10221042
func TestConditionalFormat(t *testing.T) {
@@ -1207,6 +1227,61 @@ func TestAddVBAProject(t *testing.T) {
12071227
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddVBAProject.xlsm")))
12081228
}
12091229

1230+
func TestContentTypesReader(t *testing.T) {
1231+
// Test unsupport charset.
1232+
f := NewFile()
1233+
f.ContentTypes = nil
1234+
f.XLSX["[Content_Types].xml"] = MacintoshCyrillicCharset
1235+
f.contentTypesReader()
1236+
}
1237+
1238+
func TestWorkbookReader(t *testing.T) {
1239+
// Test unsupport charset.
1240+
f := NewFile()
1241+
f.WorkBook = nil
1242+
f.XLSX["xl/workbook.xml"] = MacintoshCyrillicCharset
1243+
f.workbookReader()
1244+
}
1245+
1246+
func TestWorkSheetReader(t *testing.T) {
1247+
// Test unsupport charset.
1248+
f := NewFile()
1249+
delete(f.Sheet, "xl/worksheets/sheet1.xml")
1250+
f.XLSX["xl/worksheets/sheet1.xml"] = MacintoshCyrillicCharset
1251+
_, err := f.workSheetReader("Sheet1")
1252+
assert.EqualError(t, err, "xml decode error: XML syntax error on line 1: invalid UTF-8")
1253+
1254+
// Test on no checked worksheet.
1255+
f = NewFile()
1256+
delete(f.Sheet, "xl/worksheets/sheet1.xml")
1257+
f.XLSX["xl/worksheets/sheet1.xml"] = []byte(`<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData/></worksheet>`)
1258+
f.checked = nil
1259+
_, err = f.workSheetReader("Sheet1")
1260+
assert.NoError(t, err)
1261+
}
1262+
1263+
func TestRelsReader(t *testing.T) {
1264+
// Test unsupport charset.
1265+
f := NewFile()
1266+
rels := "xl/_rels/workbook.xml.rels"
1267+
f.Relationships[rels] = nil
1268+
f.XLSX[rels] = MacintoshCyrillicCharset
1269+
f.relsReader(rels)
1270+
}
1271+
1272+
func TestDeleteSheetFromWorkbookRels(t *testing.T) {
1273+
f := NewFile()
1274+
rels := "xl/_rels/workbook.xml.rels"
1275+
f.Relationships[rels] = nil
1276+
assert.Equal(t, f.deleteSheetFromWorkbookRels("rID"), "")
1277+
}
1278+
1279+
func TestAttrValToInt(t *testing.T) {
1280+
_, err := attrValToInt("r", []xml.Attr{
1281+
{Name: xml.Name{Local: "r"}, Value: "s"}})
1282+
assert.EqualError(t, err, `strconv.Atoi: parsing "s": invalid syntax`)
1283+
}
1284+
12101285
func prepareTestBook1() (*File, error) {
12111286
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
12121287
if err != nil {

picture.go

+26-13
Original file line numberDiff line numberDiff line change
@@ -477,24 +477,14 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string)
477477
var (
478478
wsDr *xlsxWsDr
479479
ok bool
480-
anchor *xdrCellAnchor
481480
deWsDr *decodeWsDr
482481
drawRel *xlsxRelationship
483482
deTwoCellAnchor *decodeTwoCellAnchor
484483
)
485484

486485
wsDr, _ = f.drawingParser(drawingXML)
487-
for _, anchor = range wsDr.TwoCellAnchor {
488-
if anchor.From != nil && anchor.Pic != nil {
489-
if anchor.From.Col == col && anchor.From.Row == row {
490-
drawRel = f.getDrawingRelationships(drawingRelationships,
491-
anchor.Pic.BlipFill.Blip.Embed)
492-
if _, ok = supportImageTypes[filepath.Ext(drawRel.Target)]; ok {
493-
ret, buf = filepath.Base(drawRel.Target), []byte(f.XLSX[strings.Replace(drawRel.Target, "..", "xl", -1)])
494-
return
495-
}
496-
}
497-
}
486+
if ret, buf = f.getPictureFromWsDr(row, col, drawingRelationships, wsDr); len(buf) > 0 {
487+
return
498488
}
499489
deWsDr = new(decodeWsDr)
500490
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(drawingXML)))).
@@ -514,13 +504,36 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string)
514504
if deTwoCellAnchor.From.Col == col && deTwoCellAnchor.From.Row == row {
515505
drawRel = f.getDrawingRelationships(drawingRelationships, deTwoCellAnchor.Pic.BlipFill.Blip.Embed)
516506
if _, ok = supportImageTypes[filepath.Ext(drawRel.Target)]; ok {
517-
ret, buf = filepath.Base(drawRel.Target), []byte(f.XLSX[strings.Replace(drawRel.Target, "..", "xl", -1)])
507+
ret, buf = filepath.Base(drawRel.Target), f.XLSX[strings.Replace(drawRel.Target, "..", "xl", -1)]
518508
return
519509
}
520510
}
521511
}
522512
}
513+
return
514+
}
523515

516+
// getPictureFromWsDr provides a function to get picture base name and raw
517+
// content in worksheet drawing by given coordinates and drawing
518+
// relationships.
519+
func (f *File) getPictureFromWsDr(row, col int, drawingRelationships string, wsDr *xlsxWsDr) (ret string, buf []byte) {
520+
var (
521+
ok bool
522+
anchor *xdrCellAnchor
523+
drawRel *xlsxRelationship
524+
)
525+
for _, anchor = range wsDr.TwoCellAnchor {
526+
if anchor.From != nil && anchor.Pic != nil {
527+
if anchor.From.Col == col && anchor.From.Row == row {
528+
drawRel = f.getDrawingRelationships(drawingRelationships,
529+
anchor.Pic.BlipFill.Blip.Embed)
530+
if _, ok = supportImageTypes[filepath.Ext(drawRel.Target)]; ok {
531+
ret, buf = filepath.Base(drawRel.Target), f.XLSX[strings.Replace(drawRel.Target, "..", "xl", -1)]
532+
return
533+
}
534+
}
535+
}
536+
}
524537
return
525538
}
526539

0 commit comments

Comments
 (0)