test: add more tests for sorters

Test RoleSorter and add a test to check tie resolutions in sorters
This commit is contained in:
Pierre-Yves Siret 2017-09-11 21:40:59 +02:00
parent e260eac830
commit 27af0544ec
3 changed files with 91 additions and 6 deletions

View File

@ -18,4 +18,5 @@ OTHER_FILES += \
tst_sourceroles.qml \
tst_sorters.qml \
tst_helpers.qml \
tst_builtins.qml
tst_builtins.qml \
tst_rolesorter.qml

71
tests/tst_rolesorter.qml Normal file
View File

@ -0,0 +1,71 @@
import QtQuick 2.0
import SortFilterProxyModel 0.2
import QtQml.Models 2.2
import QtTest 1.1
Item {
property list<RoleSorter> sorters: [
RoleSorter {
property string tag: "intRole"
property var expectedValues: [1, 2, 3, 4, 5]
roleName: "intRole"
},
RoleSorter {
property string tag: "intRoleDescending"
property var expectedValues: [5, 4, 3, 2, 1]
roleName: "intRole"
sortOrder: Qt.DescendingOrder
},
RoleSorter {
property string tag: "stringRole"
property var expectedValues: ["a", "b", "c", "d", "e"]
roleName: "stringRole"
},
RoleSorter {
property string tag: "stringRoleDescending"
property var expectedValues: ["e", "d", "c", "b", "a"]
roleName: "stringRole"
sortOrder: Qt.DescendingOrder
},
RoleSorter {
property string tag: "mixedCaseStringRole"
property var expectedValues: ["A", "b", "C", "D", "e"]
roleName: "mixedCaseStringRole"
}
]
ListModel {
id: dataModel
ListElement { intRole: 5; stringRole: "c"; mixedCaseStringRole: "C" }
ListElement { intRole: 3; stringRole: "e"; mixedCaseStringRole: "e" }
ListElement { intRole: 1; stringRole: "d"; mixedCaseStringRole: "D" }
ListElement { intRole: 2; stringRole: "a"; mixedCaseStringRole: "A" }
ListElement { intRole: 4; stringRole: "b"; mixedCaseStringRole: "b" }
}
SortFilterProxyModel {
id: testModel
sourceModel: dataModel
}
TestCase {
name: "RoleSorterTests"
function test_roleSorters_data() {
return sorters;
}
function test_roleSorters(sorter) {
testModel.sorters = sorter;
verify(testModel.count === sorter.expectedValues.length,
"Expected count " + sorter.expectedValues.length + ", actual count: " + testModel.count);
for (var i = 0; i < testModel.count; i++)
{
var modelValue = testModel.get(i, sorter.roleName);
verify(modelValue === sorter.expectedValues[i],
"Expected testModel value " + modelValue + ", actual: " + sorter.expectedValues[i]);
}
}
}
}

View File

@ -7,10 +7,10 @@ import SortFilterProxyModel.Test 0.2
Item {
ListModel {
id: listModel
ListElement { test: "first" }
ListElement { test: "second" }
ListElement { test: "third" }
ListElement { test: "fourth" }
ListElement { test: "first"; test2: "c" }
ListElement { test: "second"; test2: "a" }
ListElement { test: "third"; test2: "b" }
ListElement { test: "fourth"; test2: "b" }
}
property list<QtObject> sorters: [
@ -60,9 +60,16 @@ Item {
property var expectedValues: ["first", "second", "third", "fourth"]
}
]
ReverseIndexSorter {
id: reverseIndexSorter
}
property list<RoleSorter> tieSorters: [
RoleSorter { roleName: "test2" },
RoleSorter { roleName: "test" }
]
SortFilterProxyModel {
id: testModel
sourceModel: listModel
@ -100,12 +107,18 @@ Item {
verifyModelValues(testModel, expectedValuesAfterDisabling);
}
function test_tieSorters() {
testModel.sorters = tieSorters;
var expectedValues = ["second", "fourth", "third", "first"];
verifyModelValues(testModel, expectedValues);
}
function verifyModelValues(model, expectedValues) {
verify(model.count === expectedValues.length,
"Expected count " + expectedValues.length + ", actual count: " + model.count);
for (var i = 0; i < model.count; i++)
{
var modelValue = model.data(model.index(i, 0));
var modelValue = model.get(i, "test");
verify(modelValue === expectedValues[i],
"Expected testModel value " + expectedValues[i] + ", actual: " + modelValue);
}