tst_stringsorter.qml
1 import QtQuick 2.0 2 import SortFilterProxyModel 0.2 3 import QtQml.Models 2.2 4 import QtTest 1.1 5 6 Item { 7 property list<StringSorter> sorters: [ 8 StringSorter { 9 property string tag: "normal" 10 property var expectedValues: ["haha", "hähä", "hehe", "héhé", "hihi", "huhu"] 11 roleName: "accentRole" 12 }, 13 StringSorter { 14 property string tag: "numericMode" 15 property var expectedValues: ["a1", "a20", "a30", "a99", "a100", "a1000"] 16 roleName: "numericRole" 17 numericMode: true 18 }, 19 StringSorter { 20 property string tag: "nonNumericMode" 21 property var expectedValues: ["a1", "a100", "a1000", "a20", "a30", "a99"] 22 roleName: "numericRole" 23 numericMode: false 24 }, 25 StringSorter { 26 property string tag: "caseSensitive" 27 property var expectedValues: ["a", "A", "b", "c", "z", "Z"] 28 roleName: "caseRole" 29 caseSensitivity: Qt.CaseSensitive 30 }, 31 StringSorter { 32 property string tag: "nonCaseSensitive" 33 property var expectedValues: ["A", "a", "b", "c", "Z", "z"] 34 roleName: "caseRole" 35 caseSensitivity: Qt.CaseInsensitive 36 }, 37 StringSorter { 38 property string tag: "ignorePunctuation" 39 property var expectedValues: ["a-a", "aa", "b-b", "b-c", "b.c", "bc"] 40 roleName: "punctuationRole" 41 ignorePunctation: true 42 }, 43 StringSorter { 44 property string tag: "doNotIgnorePunctuation" 45 property var expectedValues: ["aa", "a-a", "b.c", "b-b", "bc", "b-c"] 46 roleName: "punctuationRole" 47 ignorePunctation: false 48 } 49 ] 50 51 ListModel { 52 id: dataModel 53 ListElement { accentRole: "héhé"; numericRole: "a20"; caseRole: "b"; punctuationRole: "a-a"} 54 ListElement { accentRole: "hehe"; numericRole: "a1"; caseRole: "A"; punctuationRole: "aa"} 55 ListElement { accentRole: "haha"; numericRole: "a100"; caseRole: "a"; punctuationRole: "b-c"} 56 ListElement { accentRole: "huhu"; numericRole: "a99"; caseRole: "c"; punctuationRole: "b.c"} 57 ListElement { accentRole: "hihi"; numericRole: "a30"; caseRole: "Z"; punctuationRole: "bc"} 58 ListElement { accentRole: "hähä"; numericRole: "a1000"; caseRole: "z"; punctuationRole: "b-b"} 59 } 60 61 SortFilterProxyModel { 62 id: testModel 63 sourceModel: dataModel 64 } 65 66 TestCase { 67 name: "StringSorterTests" 68 69 function test_stringSorters_data() { 70 return sorters; 71 } 72 73 function test_stringSorters(sorter) { 74 testModel.sorters = sorter; 75 76 verify(testModel.count === sorter.expectedValues.length, 77 "Expected count " + sorter.expectedValues.length + ", actual count: " + testModel.count); 78 let actualValues = []; 79 for (var i = 0; i < testModel.count; i++) { 80 actualValues.push(testModel.get(i, sorter.roleName)); 81 } 82 compare(actualValues, sorter.expectedValues); 83 } 84 } 85 }