-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathSelectOptionComparatorTest.cls
172 lines (154 loc) · 7.39 KB
/
SelectOptionComparatorTest.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* ============================================================
* This code is part of the "apex-lang" open source project avaiable at:
*
* http://code.google.com/p/apex-lang/
*
* This code is licensed under the Apache License, Version 2.0. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
* ============================================================
*/
@IsTest
private class SelectOptionComparatorTest {
private static testmethod void testSortOnLabelDescending(){
SelectOptionWrapper o1 = new SelectOptionWrapper(new SelectOption('xyz','abc'));
SelectOptionWrapper o2 = new SelectOptionWrapper(new SelectOption('uvw','def'));
SelectOptionComparator comp = new SelectOptionComparator();
System.assertEquals(true,comp.sortDescending);
System.assertEquals(false,comp.sortAscending);
System.assertEquals(true,comp.sortOnLabel);
System.assertEquals(false,comp.sortOnValue);
System.assertEquals(-1,comp.compare(o1,o2));
System.assertEquals(1,comp.compare(o2,o1));
System.assertEquals(0,comp.compare(o1,o1));
System.assertEquals(0,comp.compare(o2,o2));
comp = new SelectOptionComparator(true);
System.assertEquals(-1,comp.compare(o1,o2));
System.assertEquals(1,comp.compare(o2,o1));
System.assertEquals(0,comp.compare(o1,o1));
System.assertEquals(0,comp.compare(o2,o2));
comp = new SelectOptionComparator(true,true);
System.assertEquals(-1,comp.compare(o1,o2));
System.assertEquals(1,comp.compare(o2,o1));
System.assertEquals(0,comp.compare(o1,o1));
System.assertEquals(0,comp.compare(o2,o2));
}
private static testmethod void testSortOnValueDescending(){
SelectOptionWrapper o1 = new SelectOptionWrapper(new SelectOption('xyz','abc'));
SelectOptionWrapper o2 = new SelectOptionWrapper(new SelectOption('uvw','def'));
SelectOptionComparator comp = new SelectOptionComparator(true,false);
System.assertEquals(1,comp.compare(o1,o2));
System.assertEquals(-1,comp.compare(o2,o1));
System.assertEquals(0,comp.compare(o1,o1));
System.assertEquals(0,comp.compare(o2,o2));
}
private static testmethod void testSortOnValueAscending(){
SelectOptionWrapper o1 = new SelectOptionWrapper(new SelectOption('xyz','abc'));
SelectOptionWrapper o2 = new SelectOptionWrapper(new SelectOption('uvw','def'));
SelectOptionComparator comp = new SelectOptionComparator(false,false);
System.assertEquals(-1,comp.compare(o1,o2));
System.assertEquals(1,comp.compare(o2,o1));
System.assertEquals(0,comp.compare(o1,o1));
System.assertEquals(0,comp.compare(o2,o2));
}
private static testmethod void testSortOnLabelAscending(){
SelectOptionWrapper o1 = new SelectOptionWrapper(new SelectOption('xyz','abc'));
SelectOptionWrapper o2 = new SelectOptionWrapper(new SelectOption('uvw','def'));
SelectOptionComparator comp = new SelectOptionComparator(false,true);
System.assertEquals(1,comp.compare(o1,o2));
System.assertEquals(-1,comp.compare(o2,o1));
System.assertEquals(0,comp.compare(o1,o1));
System.assertEquals(0,comp.compare(o2,o2));
}
private static testmethod void testNulls(){
SelectOptionWrapper o1 = new SelectOptionWrapper(new SelectOption('xyz','abc'));
SelectOptionComparator comp = new SelectOptionComparator(true,true);
System.assertEquals(-1,comp.compare(null,o1));
System.assertEquals(1,comp.compare(o1,null));
comp = new SelectOptionComparator(false,true);
System.assertEquals(1,comp.compare(null,o1));
System.assertEquals(-1,comp.compare(o1,null));
}
private static testmethod void testInvalidType(){
Boolean exceptionCaught = false;
try{
(new SelectOptionComparator()).compare(1,1);
}catch(IllegalArgumentException e){
exceptionCaught = true;
}
System.assert(exceptionCaught==true,'IllegalArgumentException was not thrown');
}
private static testmethod void testSortDescendingOnLabel(){
List<SelectOption> theList = new List<SelectOption>{
new SelectOption('XXXXX','ghi')
,new SelectOption('XXXXX','jkl')
,new SelectOption('XXXXX','abc')
,new SelectOption('XXXXX','def')
};
theList = SelectOptionComparator.sort(theList);
System.assertNotEquals(null,theList);
System.assertNotEquals(null,theList.get(0));
System.assertNotEquals(null,theList.get(1));
System.assertNotEquals(null,theList.get(2));
System.assertNotEquals(null,theList.get(3));
System.assertEquals('abc',theList.get(0).getLabel());
System.assertEquals('def',theList.get(1).getLabel());
System.assertEquals('ghi',theList.get(2).getLabel());
System.assertEquals('jkl',theList.get(3).getLabel());
}
private static testmethod void testSortAscendingOnLabel(){
List<SelectOption> theList = new List<SelectOption>{
new SelectOption('XXXXX','ghi')
,new SelectOption('XXXXX','jkl')
,new SelectOption('XXXXX','abc')
,new SelectOption('XXXXX','def')
};
theList = SelectOptionComparator.sort(theList,false,true);
System.assertNotEquals(null,theList);
System.assertNotEquals(null,theList.get(0));
System.assertNotEquals(null,theList.get(1));
System.assertNotEquals(null,theList.get(2));
System.assertNotEquals(null,theList.get(3));
System.assertEquals('abc',theList.get(3).getLabel());
System.assertEquals('def',theList.get(2).getLabel());
System.assertEquals('ghi',theList.get(1).getLabel());
System.assertEquals('jkl',theList.get(0).getLabel());
}
private static testmethod void testSortDescendingOnValue(){
List<SelectOption> theList = new List<SelectOption>{
new SelectOption('ghi','XXXXX')
,new SelectOption('abc','XXXXX')
,new SelectOption('jkl','XXXXX')
,new SelectOption('def','XXXXX')
};
theList = SelectOptionComparator.sort(theList,true,false);
System.assertNotEquals(null,theList);
System.assertNotEquals(null,theList.get(0));
System.assertNotEquals(null,theList.get(1));
System.assertNotEquals(null,theList.get(2));
System.assertNotEquals(null,theList.get(3));
System.assertEquals('abc',theList.get(0).getValue());
System.assertEquals('def',theList.get(1).getValue());
System.assertEquals('ghi',theList.get(2).getValue());
System.assertEquals('jkl',theList.get(3).getValue());
}
private static testmethod void testSortAscendingOnValue(){
List<SelectOption> theList = new List<SelectOption>{
new SelectOption('ghi','XXXXX')
,new SelectOption('abc','XXXXX')
,new SelectOption('jkl','XXXXX')
,new SelectOption('def','XXXXX')
};
theList = SelectOptionComparator.sort(theList,false,false);
System.assertNotEquals(null,theList);
System.assertNotEquals(null,theList.get(0));
System.assertNotEquals(null,theList.get(1));
System.assertNotEquals(null,theList.get(2));
System.assertNotEquals(null,theList.get(3));
System.assertEquals('abc',theList.get(3).getValue());
System.assertEquals('def',theList.get(2).getValue());
System.assertEquals('ghi',theList.get(1).getValue());
System.assertEquals('jkl',theList.get(0).getValue());
}
}