Browse Source

Tests refactoring

Primary motivation: Avoid statefull tests with not ignorable git file tree changes.
Multiple tests reads and overwrites signle file for won needs.
Multiple tests reads and overwrites file under version control.

Secondary motivation: Minimal tests logic aligment, separate error expectation
and not error expectation tests. Introduce sub-test over test data sets and so far.

This commit is not ideal but necessary (IMHO)
Veniamin Albaev 7 years ago
parent
commit
35426ed5dc
12 changed files with 592 additions and 535 deletions
  1. 1 0
      .gitignore
  2. 9 11
      cell_test.go
  3. 22 18
      chart_test.go
  4. 34 8
      datavalidation_test.go
  5. 11 8
      date_test.go
  6. 404 370
      excelize_test.go
  7. 1 1
      go.mod
  8. 2 2
      go.sum
  9. 29 28
      lib_test.go
  10. 69 64
      sheetpr_test.go
  11. 10 25
      sheetview_test.go
  12. 0 0
      test/BadWorkbook.xlsx

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+test/Test*.xlsx

+ 9 - 11
cell_test.go

@@ -1,6 +1,10 @@
 package excelize
 
-import "testing"
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
 
 func TestCheckCellInArea(t *testing.T) {
 	expectedTrueCellInAreaList := [][2]string{
@@ -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{
@@ -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)
 	}
 }

+ 22 - 18
chart_test.go

@@ -4,6 +4,8 @@ import (
 	"bytes"
 	"encoding/xml"
 	"testing"
+
+	"github.com/stretchr/testify/assert"
 )
 
 func TestChartSize(t *testing.T) {
@@ -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 (
@@ -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()
+	}
 }

+ 34 - 8
datavalidation_test.go

@@ -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)
@@ -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()
 	}
 }

+ 11 - 8
date_test.go

@@ -1,8 +1,11 @@
 package excelize
 
 import (
+	"fmt"
 	"testing"
 	"time"
+
+	"github.com/stretchr/testify/assert"
 )
 
 type dateTest struct {
@@ -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))
+		})
 	}
 }
 
@@ -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))
+		})
 	}
 }

File diff suppressed because it is too large
+ 404 - 370
excelize_test.go


+ 1 - 1
go.mod

@@ -4,5 +4,5 @@ require (
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
 	github.com/pmezard/go-difflib v1.0.0 // indirect
-	github.com/stretchr/testify v1.2.2
+	github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb
 )

+ 2 - 2
go.sum

@@ -4,5 +4,5 @@ github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9
 github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
-github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
-github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
+github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb h1:cRItZejS4Ok67vfCdrbGIaqk86wmtQNOjVD7jSyS2aw=
+github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

+ 29 - 28
lib_test.go

@@ -1,8 +1,13 @@
 package excelize
 
-import "testing"
+import (
+	"fmt"
+	"testing"
 
-func TestAxisLowerOrEqualThan(t *testing.T) {
+	"github.com/stretchr/testify/assert"
+)
+
+func TestAxisLowerOrEqualThanIsTrue(t *testing.T) {
 	trueExpectedInputList := [][2]string{
 		{"A", "B"},
 		{"A", "AA"},
@@ -12,13 +17,14 @@ func TestAxisLowerOrEqualThan(t *testing.T) {
 		{"2", "11"},
 	}
 
-	for _, trueExpectedInput := range trueExpectedInputList {
-		isLowerOrEqual := axisLowerOrEqualThan(trueExpectedInput[0], trueExpectedInput[1])
-		if !isLowerOrEqual {
-			t.Fatalf("Expected %v <= %v = true, got false\n", trueExpectedInput[0], trueExpectedInput[1])
-		}
+	for i, trueExpectedInput := range trueExpectedInputList {
+		t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
+			assert.True(t, axisLowerOrEqualThan(trueExpectedInput[0], trueExpectedInput[1]))
+		})
 	}
+}
 
+func TestAxisLowerOrEqualThanIsFalse(t *testing.T) {
 	falseExpectedInputList := [][2]string{
 		{"B", "A"},
 		{"AA", "A"},
@@ -28,32 +34,27 @@ func TestAxisLowerOrEqualThan(t *testing.T) {
 		{"11", "2"},
 	}
 
-	for _, falseExpectedInput := range falseExpectedInputList {
-		isLowerOrEqual := axisLowerOrEqualThan(falseExpectedInput[0], falseExpectedInput[1])
-		if isLowerOrEqual {
-			t.Fatalf("Expected %v <= %v = false, got true\n", falseExpectedInput[0], falseExpectedInput[1])
-		}
+	for i, falseExpectedInput := range falseExpectedInputList {
+		t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
+			assert.False(t, axisLowerOrEqualThan(falseExpectedInput[0], falseExpectedInput[1]))
+		})
 	}
 }
 
 func TestGetCellColRow(t *testing.T) {
-	cellExpectedColRowList := map[string][2]string{
-		"C220":    {"C", "220"},
-		"aaef42":  {"aaef", "42"},
-		"bonjour": {"bonjour", ""},
-		"59":      {"", "59"},
-		"":        {"", ""},
+	cellExpectedColRowList := [][3]string{
+		{"C220", "C", "220"},
+		{"aaef42", "aaef", "42"},
+		{"bonjour", "bonjour", ""},
+		{"59", "", "59"},
+		{"", "", ""},
 	}
 
-	for cell, expectedColRow := range cellExpectedColRowList {
-		col, row := getCellColRow(cell)
-
-		if col != expectedColRow[0] {
-			t.Fatalf("Expected cell %v to return col %v, got col %v\n", cell, expectedColRow[0], col)
-		}
-
-		if row != expectedColRow[1] {
-			t.Fatalf("Expected cell %v to return row %v, got row %v\n", cell, expectedColRow[1], row)
-		}
+	for i, test := range cellExpectedColRowList {
+		t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
+			col, row := getCellColRow(test[0])
+			assert.Equal(t, test[1], col, "Unexpected col")
+			assert.Equal(t, test[2], row, "Unexpected row")
+		})
 	}
 }

+ 69 - 64
sheetpr_test.go

@@ -2,11 +2,12 @@ package excelize_test
 
 import (
 	"fmt"
-	"reflect"
 	"testing"
 
-	"github.com/360EntSecGroup-Skylar/excelize"
 	"github.com/mohae/deepcopy"
+	"github.com/stretchr/testify/assert"
+
+	"github.com/360EntSecGroup-Skylar/excelize"
 )
 
 var _ = []excelize.SheetPrOption{
@@ -86,7 +87,8 @@ func ExampleFile_GetSheetPrOptions() {
 
 func TestSheetPrOptions(t *testing.T) {
 	const sheet = "Sheet1"
-	for _, test := range []struct {
+
+	testData := []struct {
 		container  excelize.SheetPrOptionPtr
 		nonDefault excelize.SheetPrOption
 	}{
@@ -96,66 +98,69 @@ func TestSheetPrOptions(t *testing.T) {
 		{new(excelize.FitToPage), excelize.FitToPage(true)},
 		{new(excelize.AutoPageBreaks), excelize.AutoPageBreaks(true)},
 		{new(excelize.OutlineSummaryBelow), excelize.OutlineSummaryBelow(false)},
-	} {
-		opt := test.nonDefault
-		t.Logf("option %T", opt)
-
-		def := deepcopy.Copy(test.container).(excelize.SheetPrOptionPtr)
-		val1 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
-		val2 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
-
-		xl := excelize.NewFile()
-		// Get the default value
-		if err := xl.GetSheetPrOptions(sheet, def); err != nil {
-			t.Fatalf("%T: %s", opt, err)
-		}
-		// Get again and check
-		if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
-			t.Fatalf("%T: %s", opt, err)
-		}
-		if !reflect.DeepEqual(val1, def) {
-			t.Fatalf("%T: value should not have changed", opt)
-		}
-		// Set the same value
-		if err := xl.SetSheetPrOptions(sheet, val1); err != nil {
-			t.Fatalf("%T: %s", opt, err)
-		}
-		// Get again and check
-		if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
-			t.Fatalf("%T: %s", opt, err)
-		}
-		if !reflect.DeepEqual(val1, def) {
-			t.Fatalf("%T: value should not have changed", opt)
-		}
-
-		// Set a different value
-		if err := xl.SetSheetPrOptions(sheet, test.nonDefault); err != nil {
-			t.Fatalf("%T: %s", opt, err)
-		}
-		if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
-			t.Fatalf("%T: %s", opt, err)
-		}
-		// Get again and compare
-		if err := xl.GetSheetPrOptions(sheet, val2); err != nil {
-			t.Fatalf("%T: %s", opt, err)
-		}
-		if !reflect.DeepEqual(val2, val1) {
-			t.Fatalf("%T: value should not have changed", opt)
-		}
-		// Value should not be the same as the default
-		if reflect.DeepEqual(val1, def) {
-			t.Fatalf("%T: value should have changed from default", opt)
-		}
-
-		// Restore the default value
-		if err := xl.SetSheetPrOptions(sheet, def); err != nil {
-			t.Fatalf("%T: %s", opt, err)
-		}
-		if err := xl.GetSheetPrOptions(sheet, val1); err != nil {
-			t.Fatalf("%T: %s", opt, err)
-		}
-		if !reflect.DeepEqual(val1, def) {
-			t.Fatalf("%T: value should now be the same as default", opt)
-		}
+	}
+
+	for i, test := range testData {
+		t.Run(fmt.Sprintf("TestData%d", i), func(t *testing.T) {
+
+			opt := test.nonDefault
+			t.Logf("option %T", opt)
+
+			def := deepcopy.Copy(test.container).(excelize.SheetPrOptionPtr)
+			val1 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
+			val2 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr)
+
+			xl := excelize.NewFile()
+			// Get the default value
+			if !assert.NoError(t, xl.GetSheetPrOptions(sheet, def), opt) {
+				t.FailNow()
+			}
+			// Get again and check
+			if !assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt) {
+				t.FailNow()
+			}
+			if !assert.Equal(t, val1, def, opt) {
+				t.FailNow()
+			}
+			// Set the same value
+			if !assert.NoError(t, xl.SetSheetPrOptions(sheet, val1), opt) {
+				t.FailNow()
+			}
+			// Get again and check
+			if !assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt) {
+				t.FailNow()
+			}
+			if !assert.Equal(t, val1, def, "%T: value should not have changed", opt) {
+				t.FailNow()
+			}
+			// Set a different value
+			if !assert.NoError(t, xl.SetSheetPrOptions(sheet, test.nonDefault), opt) {
+				t.FailNow()
+			}
+			if !assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt) {
+				t.FailNow()
+			}
+			// Get again and compare
+			if !assert.NoError(t, xl.GetSheetPrOptions(sheet, val2), opt) {
+				t.FailNow()
+			}
+			if !assert.Equal(t, val1, val2, "%T: value should not have changed", opt) {
+				t.FailNow()
+			}
+			// Value should not be the same as the default
+			if !assert.NotEqual(t, def, val1, "%T: value should have changed from default", opt) {
+				t.FailNow()
+			}
+			// Restore the default value
+			if !assert.NoError(t, xl.SetSheetPrOptions(sheet, def), opt) {
+				t.FailNow()
+			}
+			if !assert.NoError(t, xl.GetSheetPrOptions(sheet, val1), opt) {
+				t.FailNow()
+			}
+			if !assert.Equal(t, def, val1) {
+				t.FailNow()
+			}
+		})
 	}
 }

+ 10 - 25
sheetview_test.go

@@ -4,6 +4,8 @@ import (
 	"fmt"
 	"testing"
 
+	"github.com/stretchr/testify/assert"
+
 	"github.com/360EntSecGroup-Skylar/excelize"
 )
 
@@ -157,29 +159,12 @@ func TestSheetViewOptionsErrors(t *testing.T) {
 	xl := excelize.NewFile()
 	const sheet = "Sheet1"
 
-	if err := xl.GetSheetViewOptions(sheet, 0); err != nil {
-		t.Errorf("Unexpected error: %s", err)
-	}
-	if err := xl.GetSheetViewOptions(sheet, -1); err != nil {
-		t.Errorf("Unexpected error: %s", err)
-	}
-	if err := xl.GetSheetViewOptions(sheet, 1); err == nil {
-		t.Error("Error expected but got nil")
-	}
-	if err := xl.GetSheetViewOptions(sheet, -2); err == nil {
-		t.Error("Error expected but got nil")
-	}
-
-	if err := xl.SetSheetViewOptions(sheet, 0); err != nil {
-		t.Errorf("Unexpected error: %s", err)
-	}
-	if err := xl.SetSheetViewOptions(sheet, -1); err != nil {
-		t.Errorf("Unexpected error: %s", err)
-	}
-	if err := xl.SetSheetViewOptions(sheet, 1); err == nil {
-		t.Error("Error expected but got nil")
-	}
-	if err := xl.SetSheetViewOptions(sheet, -2); err == nil {
-		t.Error("Error expected but got nil")
-	}
+	assert.NoError(t, xl.GetSheetViewOptions(sheet, 0))
+	assert.NoError(t, xl.GetSheetViewOptions(sheet, -1))
+	assert.Error(t, xl.GetSheetViewOptions(sheet, 1))
+	assert.Error(t, xl.GetSheetViewOptions(sheet, -2))
+	assert.NoError(t, xl.SetSheetViewOptions(sheet, 0))
+	assert.NoError(t, xl.SetSheetViewOptions(sheet, -1))
+	assert.Error(t, xl.SetSheetViewOptions(sheet, 1))
+	assert.Error(t, xl.SetSheetViewOptions(sheet, -2))
 }

+ 0 - 0
test/badWorkbook.xlsx → test/BadWorkbook.xlsx


Some files were not shown because too many files changed in this diff