Change tests to write less

This commit is contained in:
kolaente 2019-12-04 22:51:37 +01:00
parent 2fca034aad
commit 6dc275fa00
Signed by: konrad
GPG Key ID: F40E70337AB24C9B

View File

@ -182,15 +182,16 @@ var (
) )
type taskSortTestCase struct { type taskSortTestCase struct {
name string name string
want []*Task wantAsc []*Task
sortParams []*sortParam wantDesc []*Task
sortProperty sortProperty
} }
var taskSortTestCases = []taskSortTestCase{ var taskSortTestCases = []taskSortTestCase{
{ {
name: "Order by ID Ascending", name: "Order by ID",
want: []*Task{ wantAsc: []*Task{
task1, task1,
task2, task2,
task3, task3,
@ -202,16 +203,7 @@ var taskSortTestCases = []taskSortTestCase{
task9, task9,
task10, task10,
}, },
sortParams: []*sortParam{ wantDesc: []*Task{
{
sortBy: taskPropertyID,
orderBy: orderAscending,
},
},
},
{
name: "Order by ID Descending",
want: []*Task{
task10, task10,
task9, task9,
task8, task8,
@ -223,12 +215,7 @@ var taskSortTestCases = []taskSortTestCase{
task2, task2,
task1, task1,
}, },
sortParams: []*sortParam{ sortProperty: taskPropertyID,
{
sortBy: taskPropertyID,
orderBy: orderDescending,
},
},
}, },
{ {
name: "Order by Text Ascending", name: "Order by Text Ascending",
@ -333,27 +320,95 @@ var taskSortTestCases = []taskSortTestCase{
func TestTaskSort(t *testing.T) { func TestTaskSort(t *testing.T) {
for _, testCase := range taskSortTestCases { for _, testCase := range taskSortTestCases {
t.Run(testCase.name, func(t *testing.T) { t.Run(testCase.name, func(t *testing.T) {
got := deepcopy.Copy(testCase.want).([]*Task) t.Run("asc default", func(t *testing.T) {
by := []*sortParam{
{
sortBy: testCase.sortProperty,
},
}
// Destroy wanted order to obtain some slice we can sort got := deepcopy.Copy(testCase.wantAsc).([]*Task)
rand.Shuffle(len(got), func(i, j int) {
got[i], got[j] = got[j], got[i] // Destroy wanted order to obtain some slice we can sort
rand.Shuffle(len(got), func(i, j int) {
got[i], got[j] = got[j], got[i]
})
sortTasks(got, by)
if !reflect.DeepEqual(got, testCase.wantAsc) {
t.Error("Slices do not match in order")
t.Error("Got:")
for _, task := range got {
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
}
t.Error("Want:")
for _, task := range testCase.wantAsc {
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
}
}
}) })
t.Run("asc", func(t *testing.T) {
sortTasks(got, testCase.sortParams) by := []*sortParam{
{
if !reflect.DeepEqual(got, testCase.want) { sortBy: testCase.sortProperty,
t.Error("Slices do not match in order") orderBy: orderAscending,
t.Error("Got:") },
for _, task := range got {
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
} }
t.Error("Want:") got := deepcopy.Copy(testCase.wantAsc).([]*Task)
for _, task := range testCase.want {
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text) // Destroy wanted order to obtain some slice we can sort
rand.Shuffle(len(got), func(i, j int) {
got[i], got[j] = got[j], got[i]
})
sortTasks(got, by)
if !reflect.DeepEqual(got, testCase.wantAsc) {
t.Error("Slices do not match in order")
t.Error("Got:")
for _, task := range got {
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
}
t.Error("Want:")
for _, task := range testCase.wantAsc {
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
}
} }
} })
t.Run("desc", func(t *testing.T) {
by := []*sortParam{
{
sortBy: testCase.sortProperty,
orderBy: orderDescending,
},
}
got := deepcopy.Copy(testCase.wantAsc).([]*Task)
// Destroy wanted order to obtain some slice we can sort
rand.Shuffle(len(got), func(i, j int) {
got[i], got[j] = got[j], got[i]
})
sortTasks(got, by)
if !reflect.DeepEqual(got, testCase.wantDesc) {
t.Error("Slices do not match in order")
t.Error("Got:")
for _, task := range got {
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
}
t.Error("Want:")
for _, task := range testCase.wantDesc {
t.Errorf(" - Task ID %d (%s)", task.ID, task.Text)
}
}
})
}) })
} }
} }