From 35426ed5dc5196374332498c82b4d480b05e5ac5 Mon Sep 17 00:00:00 2001 From: Veniamin Albaev Date: Thu, 27 Dec 2018 13:51:44 +0300 Subject: 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) --- lib_test.go | 57 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'lib_test.go') diff --git a/lib_test.go b/lib_test.go index c668fc8..ef0d8f5 100644 --- a/lib_test.go +++ b/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") + }) } } -- cgit v1.2.1