diff --git a/tests/SortFilterProxyModel.pro b/tests/SortFilterProxyModel.pro new file mode 100644 index 0000000..c3d743b --- /dev/null +++ b/tests/SortFilterProxyModel.pro @@ -0,0 +1,12 @@ +TEMPLATE = app +TARGET = tst_sortfilterproxymodel +QT += qml quick +CONFIG += c++11 warn_on qmltestcase qml_debug + +include(../SortFilterProxyModel.pri) + +SOURCES += tst_sortfilterproxymodel.cpp + +OTHER_FILES += \ + tst_rangefilter.qml \ + tst_indexfilter.qml diff --git a/tests/tst_indexfilter.qml b/tests/tst_indexfilter.qml new file mode 100644 index 0000000..09132d5 --- /dev/null +++ b/tests/tst_indexfilter.qml @@ -0,0 +1,74 @@ +import QtQuick 2.0 +import SortFilterProxyModel 0.2 +import QtQml.Models 2.2 +import QtTest 1.1 + +Item { + property list filters: [ + IndexFilter { + property string tag: "basicUsage" + property int expectedModelCount: 3 + property var expectedValues: [3, 1, 2]; + minimumIndex: 1; maximumIndex: 3 + }, + IndexFilter { + property string tag: "outOfBounds" + property int expectedModelCount: 0 + minimumIndex: 3; maximumIndex: 1 + }, + IndexFilter { + property string tag: "0to0Inverted" + property int expectedModelCount: 4 + property var expectedValues: [3,1,2,4] + minimumIndex: 0; maximumIndex: 0; inverted: true + }, + IndexFilter { + property string tag: "0to0" // bug / issue #15 + property int expectedModelCount: 1 + property var expectedValues: [5] + minimumIndex: 0; maximumIndex: 0 + }, + IndexFilter { + property string tag: "basicUsageInverted" + property int expectedModelCount: 2 + property var expectedValues: [5,4] + minimumIndex: 1; maximumIndex: 3; inverted: true + } + ] + + ListModel { + id: dataModel + ListElement { value: 5 } + ListElement { value: 3 } + ListElement { value: 1 } + ListElement { value: 2 } + ListElement { value: 4 } + } + + SortFilterProxyModel { + id: testModel + // FIXME: Crashes/fails with error if I define ListModel directly within sourceModel + sourceModel: dataModel + } + + TestCase { + name: "IndexFilterTests" + + function test_minMax_data() { + return filters; + } + + function test_minMax(filter) { + testModel.filters = filter; + + verify(testModel.count === filter.expectedModelCount, + "Expected count " + filter.expectedModelCount + ", actual count: " + testModel.count); + for (var i = 0; i < testModel.count; i++) + { + var modelValue = testModel.data(testModel.index(i, 0)); + verify(modelValue === filter.expectedValues[i], + "Expected testModel value " + modelValue + ", actual: " + filter.expectedValues[i]); + } + } + } +} diff --git a/tests/tst_rangefilter.qml b/tests/tst_rangefilter.qml new file mode 100644 index 0000000..7261ccf --- /dev/null +++ b/tests/tst_rangefilter.qml @@ -0,0 +1,92 @@ +import QtQuick 2.0 +import SortFilterProxyModel 0.2 +import QtQml.Models 2.2 +import QtTest 1.1 + +Item { + property list filters: [ + RangeFilter { + property string tag: "inclusive" + property int expectedModelCount: 3 + property var expectedValues: [3, 2, 4] + property QtObject dataModel: dataModel0 + roleName: "value"; minimumValue: 2; maximumValue: 4 + }, + RangeFilter { + property string tag: "explicitInclusive" + property int expectedModelCount: 3 + property var expectedValues: [3, 2, 4] + property QtObject dataModel: dataModel0 + roleName: "value"; minimumValue: 2; maximumValue: 4; minimumInclusive: true; maximumInclusive: true + }, + RangeFilter { + property string tag: "inclusiveMinExclusiveMax" + property int expectedModelCount: 2 + property var expectedValues: [2, 3] + property QtObject dataModel: dataModel1 + roleName: "value"; minimumValue: 2; maximumValue: 4; minimumInclusive: true; maximumInclusive: false + }, + RangeFilter { + property string tag: "exclusiveMinInclusiveMax" + property int expectedModelCount: 2 + property var expectedValues: [3, 4] + property QtObject dataModel: dataModel1 + roleName: "value"; minimumValue: 2; maximumValue: 4; minimumInclusive: false; maximumInclusive: true + }, + RangeFilter { + property string tag: "exclusive" + property int expectedModelCount: 1 + property var expectedValues: [3] + property QtObject dataModel: dataModel1 + roleName: "value"; minimumValue: 2; maximumValue: 4; minimumInclusive: false; maximumInclusive: false + }, + RangeFilter { + property string tag: "outOfBoundsRange" + property int expectedModelCount: 0 + property QtObject dataModel: dataModel1 + roleName: "value"; minimumValue: 4; maximumValue: 2 + } + ] + + ListModel { + id: dataModel0 + ListElement { value: 5 } + ListElement { value: 3 } + ListElement { value: 1 } + ListElement { value: 2 } + ListElement { value: 4 } + } + + ListModel { + id: dataModel1 + ListElement { value: 5 } + ListElement { value: 2 } + ListElement { value: 3 } + ListElement { value: 1 } + ListElement { value: 4 } + } + + SortFilterProxyModel { id: testModel } + + TestCase { + name:"RangeFilterTests" + + function test_minMax_data() { + return filters; + } + + function test_minMax(filter) { + testModel.sourceModel = filter.dataModel; + testModel.filters = filter; + + verify(testModel.count === filter.expectedModelCount, + "Expected count " + filter.expectedModelCount + ", actual count: " + testModel.count); + for (var i = 0; i < testModel.count; i++) + { + var modelValue = testModel.data(testModel.index(i, 0), "value"); + verify(modelValue === filter.expectedValues[i], + "Expected testModel value " + modelValue + ", actual: " + filter.expectedValues[i]); + } + } + } +} diff --git a/tests/tst_sortfilterproxymodel.cpp b/tests/tst_sortfilterproxymodel.cpp new file mode 100644 index 0000000..90f5a6b --- /dev/null +++ b/tests/tst_sortfilterproxymodel.cpp @@ -0,0 +1,2 @@ +#include +QUICK_TEST_MAIN(SortFilterProxyModel)