Skip to content

Commit

Permalink
Merge pull request qax-os#323 from albenik/tests_fix
Browse files Browse the repository at this point in the history
Tests refactoring
  • Loading branch information
xuri authored Dec 29, 2018
2 parents 9a6f66a + 35426ed commit 45ab95a
Show file tree
Hide file tree
Showing 12 changed files with 810 additions and 642 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/Test*.xlsx
20 changes: 9 additions & 11 deletions cell_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package excelize

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCheckCellInArea(t *testing.T) {
expectedTrueCellInAreaList := [][2]string{
Expand All @@ -14,11 +18,8 @@ func TestCheckCellInArea(t *testing.T) {
cell := expectedTrueCellInArea[0]
area := expectedTrueCellInArea[1]

cellInArea := checkCellInArea(cell, area)

if !cellInArea {
t.Fatalf("Expected cell %v to be in area %v, got false\n", cell, area)
}
assert.True(t, checkCellInArea(cell, area),
"Expected cell %v to be in area %v, got false\n", cell, area)
}

expectedFalseCellInAreaList := [][2]string{
Expand All @@ -31,10 +32,7 @@ func TestCheckCellInArea(t *testing.T) {
cell := expectedFalseCellInArea[0]
area := expectedFalseCellInArea[1]

cellInArea := checkCellInArea(cell, area)

if cellInArea {
t.Fatalf("Expected cell %v not to be inside of area %v, but got true\n", cell, area)
}
assert.False(t, checkCellInArea(cell, area),
"Expected cell %v not to be inside of area %v, but got true\n", cell, area)
}
}
40 changes: 22 additions & 18 deletions chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"encoding/xml"
"testing"

"github.com/stretchr/testify/assert"
)

func TestChartSize(t *testing.T) {
Expand All @@ -22,18 +24,18 @@ func TestChartSize(t *testing.T) {
xlsx.AddChart("Sheet1", "E4", `{"type":"col3DClustered","dimension":{"width":640, "height":480},"series":[{"name":"Sheet1!$A$2","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$2:$D$2"},{"name":"Sheet1!$A$3","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$3:$D$3"},{"name":"Sheet1!$A$4","categories":"Sheet1!$B$1:$D$1","values":"Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Clustered Column Chart"}}`)
// Save xlsx file by the given path.
err := xlsx.Write(&buffer)
if err != nil {
t.Fatal(err)
if !assert.NoError(t, err) {
t.FailNow()
}

newFile, err := OpenReader(&buffer)
if err != nil {
t.Fatal(err)
if !assert.NoError(t, err) {
t.FailNow()
}

chartsNum := newFile.countCharts()
if chartsNum != 1 {
t.Fatalf("Expected 1 chart, actual %d", chartsNum)
if !assert.Equal(t, 1, chartsNum, "Expected 1 chart, actual %d", chartsNum) {
t.FailNow()
}

var (
Expand All @@ -42,25 +44,27 @@ func TestChartSize(t *testing.T) {
)

content, ok := newFile.XLSX["xl/drawings/drawing1.xml"]
if !ok {
t.Fatal("Can't open the chart")
}
assert.True(t, ok, "Can't open the chart")

err = xml.Unmarshal([]byte(content), &workdir)
if err != nil {
t.Fatal(err)
if !assert.NoError(t, err) {
t.FailNow()
}

err = xml.Unmarshal([]byte("<decodeTwoCellAnchor>"+workdir.TwoCellAnchor[0].Content+"</decodeTwoCellAnchor>"), &anchor)
if err != nil {
t.Fatal(err)
if !assert.NoError(t, err) {
t.FailNow()
}

if anchor.From.Col != 4 || anchor.From.Row != 3 {
t.Fatalf("From: Expected column 4, row 3, actual column %d, row %d", anchor.From.Col, anchor.From.Row)
}
if anchor.To.Col != 14 || anchor.To.Row != 27 {
t.Fatalf("To: Expected column 14, row 27, actual column %d, row %d", anchor.To.Col, anchor.To.Row)
if !assert.Equal(t, 4, anchor.From.Col, "Expected 'from' column 4") ||
!assert.Equal(t, 3, anchor.From.Row, "Expected 'from' row 3") {

t.FailNow()
}

if !assert.Equal(t, 14, anchor.To.Col, "Expected 'to' column 14") ||
!assert.Equal(t, 27, anchor.To.Row, "Expected 'to' row 27") {

t.FailNow()
}
}
42 changes: 34 additions & 8 deletions datavalidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@

package excelize

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDataValidation(t *testing.T) {
const resultFile = "./test/TestDataValidation.xlsx"

xlsx := NewFile()

dvRange := NewDataValidation(true)
Expand All @@ -21,37 +27,57 @@ func TestDataValidation(t *testing.T) {
dvRange.SetError(DataValidationErrorStyleWarning, "error title", "error body")
dvRange.SetError(DataValidationErrorStyleInformation, "error title", "error body")
xlsx.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
t.FailNow()
}

dvRange = NewDataValidation(true)
dvRange.Sqref = "A3:B4"
dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
dvRange.SetInput("input title", "input body")
xlsx.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
t.FailNow()
}

dvRange = NewDataValidation(true)
dvRange.Sqref = "A5:B6"
dvRange.SetDropList([]string{"1", "2", "3"})
xlsx.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
t.FailNow()
}
}

func TestDataValidationError(t *testing.T) {
const resultFile = "./test/TestDataValidationError.xlsx"

xlsx := NewFile()
xlsx.SetCellStr("Sheet1", "E1", "E1")
xlsx.SetCellStr("Sheet1", "E2", "E2")
xlsx.SetCellStr("Sheet1", "E3", "E3")
dvRange = NewDataValidation(true)

dvRange := NewDataValidation(true)
dvRange.SetSqref("A7:B8")
dvRange.SetSqref("A7:B8")
dvRange.SetSqrefDropList("$E$1:$E$3", true)

err := dvRange.SetSqrefDropList("$E$1:$E$3", false)
t.Log(err)
assert.EqualError(t, err, "cross-sheet sqref cell are not supported")

xlsx.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
t.FailNow()
}

dvRange = NewDataValidation(true)
dvRange.SetDropList(make([]string, 258))

err = dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
t.Log(err)
assert.EqualError(t, err, "data validation must be 0-255 characters")

// Test write file to given path.
err = xlsx.SaveAs("./test/Book_data_validation.xlsx")
if err != nil {
t.Error(err)
xlsx.AddDataValidation("Sheet1", dvRange)
if !assert.NoError(t, xlsx.SaveAs(resultFile)) {
t.FailNow()
}
}
19 changes: 11 additions & 8 deletions date_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package excelize

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

type dateTest struct {
Expand All @@ -18,10 +21,10 @@ func TestTimeToExcelTime(t *testing.T) {
{401769.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)},
}

for _, test := range trueExpectedInputList {
if test.ExcelValue != timeToExcelTime(test.GoValue) {
t.Fatalf("Expected %v from %v = true, got %v\n", test.ExcelValue, test.GoValue, timeToExcelTime(test.GoValue))
}
for i, test := range trueExpectedInputList {
t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
assert.Equal(t, test.ExcelValue, timeToExcelTime(test.GoValue))
})
}
}

Expand All @@ -34,9 +37,9 @@ func TestTimeFromExcelTime(t *testing.T) {
{401769.0, time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC)},
}

for _, test := range trueExpectedInputList {
if test.GoValue != timeFromExcelTime(test.ExcelValue, false) {
t.Fatalf("Expected %v from %v = true, got %v\n", test.GoValue, test.ExcelValue, timeFromExcelTime(test.ExcelValue, false))
}
for i, test := range trueExpectedInputList {
t.Run(fmt.Sprintf("TestData%d", i+1), func(t *testing.T) {
assert.Equal(t, test.GoValue, timeFromExcelTime(test.ExcelValue, false))
})
}
}
Loading

0 comments on commit 45ab95a

Please sign in to comment.