OLD | NEW |
1 library order_by_spec; | 1 library order_by_spec; |
2 | 2 |
3 import '../_specs.dart'; | 3 import '../_specs.dart'; |
4 | 4 |
5 | 5 |
6 class Name { | 6 class Name { |
7 String firstName; | 7 String firstName; |
8 String lastName; | 8 String lastName; |
9 Name({String this.firstName, String this.lastName}); | 9 Name({this.firstName, this.lastName}); |
10 operator ==(Name other) => | 10 operator ==(Name other) => |
11 (firstName == other.firstName && lastName == other.lastName); | 11 (firstName == other.firstName && lastName == other.lastName); |
12 String toString() => 'Name(firstName: $firstName, lastName: $lastName)'; | 12 String toString() => 'Name(firstName: $firstName, lastName: $lastName)'; |
| 13 int get hashCode => firstName.hashCode + lastName.hashCode; |
13 } | 14 } |
14 | 15 |
15 | 16 |
16 main() { | 17 main() { |
17 describe('orderBy filter', () { | 18 describe('orderBy formatter', () { |
18 var Emily___Bronte = new Name(firstName: 'Emily', lastName: 'Bronte'), | 19 var Emily___Bronte = new Name(firstName: 'Emily', lastName: 'Bronte'), |
19 Mark____Twain = {'firstName': 'Mark', 'lastName': 'Twain'}, | 20 Mark____Twain = {'firstName': 'Mark', 'lastName': 'Twain'}, |
20 Jeffrey_Archer = {'firstName': 'Jeffrey', 'lastName': 'Archer'}, | 21 Jeffrey_Archer = {'firstName': 'Jeffrey', 'lastName': 'Archer'}, |
21 Isaac___Asimov = new Name(firstName: 'Isaac', lastName: 'Asimov'), | 22 Isaac___Asimov = new Name(firstName: 'Isaac', lastName: 'Asimov'), |
22 Oscar___Wilde = {'firstName': 'Oscar', 'lastName': 'Wilde'}; | 23 Oscar___Wilde = {'firstName': 'Oscar', 'lastName': 'Wilde'}; |
23 beforeEach(() => inject((Scope scope, Parser parse, FilterMap filters) { | 24 beforeEach((Scope scope, Parser parse, FormatterMap formatters) { |
24 scope.context['authors'] = [ | 25 scope.context['authors'] = [ |
25 Emily___Bronte, | 26 Emily___Bronte, |
26 Mark____Twain, | 27 Mark____Twain, |
27 Jeffrey_Archer, | 28 Jeffrey_Archer, |
28 Isaac___Asimov, | 29 Isaac___Asimov, |
29 Oscar___Wilde, | 30 Oscar___Wilde, |
30 ]; | 31 ]; |
31 scope.context['items'] = [ | 32 scope.context['items'] = [ |
32 {'a': 10, 'b': 10}, | 33 {'a': 10, 'b': 10}, |
33 {'a': 10, 'b': 20}, | 34 {'a': 10, 'b': 20}, |
34 {'a': 20, 'b': 10}, | 35 {'a': 20, 'b': 10}, |
35 {'a': 20, 'b': 20}, | 36 {'a': 20, 'b': 20}, |
36 ]; | 37 ]; |
37 })); | 38 }); |
38 | 39 |
39 it('should pass through null list when input list is null', inject((Scope sc
ope, Parser parse, FilterMap filters) { | 40 it('should pass through null list when input list is null', (Scope scope, Pa
rser parse, FormatterMap formatters) { |
40 var list = null; | 41 var list = null; |
41 expect(parse('list | orderBy:"foo"').eval(scope.context, filters)).toBe(nu
ll); | 42 expect(parse('list | orderBy:"foo"').eval(scope.context, formatters)).toBe
(null); |
42 })); | 43 }); |
43 | 44 |
44 it('should pass through argument when expression is null', inject((Scope sco
pe, Parser parse, FilterMap filters) { | 45 it('should pass through argument when expression is null', (Scope scope, Par
ser parse, FormatterMap formatters) { |
45 var list = scope.context['list'] = [1, 3, 2]; | 46 var list = scope.context['list'] = [1, 3, 2]; |
46 expect(parse('list | orderBy:thisIsNull').eval(scope.context, filters)).to
Be(list); | 47 expect(parse('list | orderBy:thisIsNull').eval(scope.context, formatters))
.toBe(list); |
47 })); | 48 }); |
48 | 49 |
49 it('should sort with "empty" expression using default comparator', inject((S
cope scope, Parser parse, FilterMap filters) { | 50 it('should sort with "empty" expression using default comparator', (Scope sc
ope, Parser parse, FormatterMap formatters) { |
50 scope.context['list'] = [1, 3, 2]; | 51 scope.context['list'] = [1, 3, 2]; |
51 expect(parse('list | orderBy:""').eval(scope.context, filters)).toEqual([1
, 2, 3]); | 52 expect(parse('list | orderBy:""').eval(scope.context, formatters)).toEqual
([1, 2, 3]); |
52 expect(parse('list | orderBy:"+"').eval(scope.context, filters)).toEqual([
1, 2, 3]); | 53 expect(parse('list | orderBy:"+"').eval(scope.context, formatters)).toEqua
l([1, 2, 3]); |
53 expect(parse('list | orderBy:"-"').eval(scope.context, filters)).toEqual([
3, 2, 1]); | 54 expect(parse('list | orderBy:"-"').eval(scope.context, formatters)).toEqua
l([3, 2, 1]); |
54 })); | 55 }); |
55 | 56 |
56 it('should sort by expression', inject((Scope scope, Parser parse, FilterMap
filters) { | 57 it('should sort by expression', (Scope scope, Parser parse, FormatterMap for
matters) { |
57 expect(parse('authors | orderBy:"firstName"').eval(scope.context, filters)
).toEqual([ | 58 expect(parse('authors | orderBy:"firstName"').eval(scope.context, formatte
rs)).toEqual([ |
58 Emily___Bronte, | 59 Emily___Bronte, |
59 Isaac___Asimov, | 60 Isaac___Asimov, |
60 Jeffrey_Archer, | 61 Jeffrey_Archer, |
61 Mark____Twain, | 62 Mark____Twain, |
62 Oscar___Wilde, | 63 Oscar___Wilde, |
63 ]); | 64 ]); |
64 expect(parse('authors | orderBy:"lastName"').eval(scope.context, filters))
.toEqual([ | 65 expect(parse('authors | orderBy:"lastName"').eval(scope.context, formatter
s)).toEqual([ |
65 Jeffrey_Archer, | 66 Jeffrey_Archer, |
66 Isaac___Asimov, | 67 Isaac___Asimov, |
67 Emily___Bronte, | 68 Emily___Bronte, |
68 Mark____Twain, | 69 Mark____Twain, |
69 Oscar___Wilde, | 70 Oscar___Wilde, |
70 ]); | 71 ]); |
71 | 72 |
72 scope.context['sortKey'] = 'firstName'; | 73 scope.context['sortKey'] = 'firstName'; |
73 expect(parse('authors | orderBy:sortKey').eval(scope.context, filters)).to
Equal([ | 74 expect(parse('authors | orderBy:sortKey').eval(scope.context, formatters))
.toEqual([ |
74 Emily___Bronte, | 75 Emily___Bronte, |
75 Isaac___Asimov, | 76 Isaac___Asimov, |
76 Jeffrey_Archer, | 77 Jeffrey_Archer, |
77 Mark____Twain, | 78 Mark____Twain, |
78 Oscar___Wilde, | 79 Oscar___Wilde, |
79 ]); | 80 ]); |
80 | 81 |
81 })); | 82 }); |
82 | 83 |
83 it('should reverse order when passed the additional descending param', injec
t((Scope scope, Parser parse, FilterMap filters) { | 84 it('should reverse order when passed the additional descending param', (Scop
e scope, Parser parse, FormatterMap formatters) { |
84 expect(parse('authors | orderBy:"lastName":true').eval(scope.context, filt
ers)).toEqual([ | 85 expect(parse('authors | orderBy:"lastName":true').eval(scope.context, form
atters)).toEqual([ |
85 Oscar___Wilde, | 86 Oscar___Wilde, |
86 Mark____Twain, | 87 Mark____Twain, |
87 Emily___Bronte, | 88 Emily___Bronte, |
88 Isaac___Asimov, | 89 Isaac___Asimov, |
89 Jeffrey_Archer, | 90 Jeffrey_Archer, |
90 ]); | 91 ]); |
91 })); | 92 }); |
92 | 93 |
93 it('should reverse order when expression is prefixed with "-"', inject((Scop
e scope, Parser parse, FilterMap filters) { | 94 it('should reverse order when expression is prefixed with "-"', (Scope scope
, Parser parse, FormatterMap formatters) { |
94 expect(parse('authors | orderBy:"-lastName"').eval(scope.context, filters)
).toEqual([ | 95 expect(parse('authors | orderBy:"-lastName"').eval(scope.context, formatte
rs)).toEqual([ |
95 Oscar___Wilde, | 96 Oscar___Wilde, |
96 Mark____Twain, | 97 Mark____Twain, |
97 Emily___Bronte, | 98 Emily___Bronte, |
98 Isaac___Asimov, | 99 Isaac___Asimov, |
99 Jeffrey_Archer, | 100 Jeffrey_Archer, |
100 ]); | 101 ]); |
101 })); | 102 }); |
102 | 103 |
103 it('should NOT reverse order when BOTH expression is prefixed with "-" AND a
dditional parameter also asks reversal', | 104 it('should NOT reverse order when BOTH expression is prefixed with "-" AND a
dditional parameter also asks reversal', |
104 inject((Scope scope, Parser parse, FilterMap filters) { | 105 (Scope scope, Parser parse, FormatterMap formatters) { |
105 expect(parse('authors | orderBy:"-lastName":true').eval(scope.context, fil
ters)).toEqual([ | 106 expect(parse('authors | orderBy:"-lastName":true').eval(scope.context, for
matters)).toEqual([ |
106 Jeffrey_Archer, | 107 Jeffrey_Archer, |
107 Isaac___Asimov, | 108 Isaac___Asimov, |
108 Emily___Bronte, | 109 Emily___Bronte, |
109 Mark____Twain, | 110 Mark____Twain, |
110 Oscar___Wilde, | 111 Oscar___Wilde, |
111 ]); | 112 ]); |
112 })); | 113 }); |
113 | 114 |
114 it('should allow a "+" prefix on the expression that should be a nop (ascend
ing order)', | 115 it('should allow a "+" prefix on the expression that should be a nop (ascend
ing order)', |
115 inject((Scope scope, Parser parse, FilterMap filters) { | 116 (Scope scope, Parser parse, FormatterMap formatters) { |
116 expect(parse('authors | orderBy:"+lastName"').eval(scope.context, filters)
).toEqual([ | 117 expect(parse('authors | orderBy:"+lastName"').eval(scope.context, formatte
rs)).toEqual([ |
117 Jeffrey_Archer, | 118 Jeffrey_Archer, |
118 Isaac___Asimov, | 119 Isaac___Asimov, |
119 Emily___Bronte, | 120 Emily___Bronte, |
120 Mark____Twain, | 121 Mark____Twain, |
121 Oscar___Wilde, | 122 Oscar___Wilde, |
122 ]); | 123 ]); |
123 expect(parse('authors | orderBy:"+lastName":false').eval(scope.context, fi
lters)).toEqual([ | 124 expect(parse('authors | orderBy:"+lastName":false').eval(scope.context, fo
rmatters)).toEqual([ |
124 Jeffrey_Archer, | 125 Jeffrey_Archer, |
125 Isaac___Asimov, | 126 Isaac___Asimov, |
126 Emily___Bronte, | 127 Emily___Bronte, |
127 Mark____Twain, | 128 Mark____Twain, |
128 Oscar___Wilde, | 129 Oscar___Wilde, |
129 ]); | 130 ]); |
130 expect(parse('authors | orderBy:"+lastName":true').eval(scope.context, fil
ters)).toEqual([ | 131 expect(parse('authors | orderBy:"+lastName":true').eval(scope.context, for
matters)).toEqual([ |
131 Oscar___Wilde, | 132 Oscar___Wilde, |
132 Mark____Twain, | 133 Mark____Twain, |
133 Emily___Bronte, | 134 Emily___Bronte, |
134 Isaac___Asimov, | 135 Isaac___Asimov, |
135 Jeffrey_Archer, | 136 Jeffrey_Archer, |
136 ]); | 137 ]); |
137 })); | 138 }); |
138 | 139 |
139 it('should support an array of expressions', | 140 it('should support an array of expressions', |
140 inject((Scope scope, Parser parse, FilterMap filters) { | 141 (Scope scope, Parser parse, FormatterMap formatters) { |
141 expect(parse('items | orderBy:["-a", "-b"]').eval(scope.context, filters))
.toEqual([ | 142 expect(parse('items | orderBy:["-a", "-b"]').eval(scope.context, formatter
s)).toEqual([ |
142 {'a': 20, 'b': 20}, | 143 {'a': 20, 'b': 20}, |
143 {'a': 20, 'b': 10}, | 144 {'a': 20, 'b': 10}, |
144 {'a': 10, 'b': 20}, | 145 {'a': 10, 'b': 20}, |
145 {'a': 10, 'b': 10}, | 146 {'a': 10, 'b': 10}, |
146 ]); | 147 ]); |
147 expect(parse('items | orderBy:["-b", "-a"]').eval(scope.context, filters))
.toEqual([ | 148 expect(parse('items | orderBy:["-b", "-a"]').eval(scope.context, formatter
s)).toEqual([ |
148 {'a': 20, 'b': 20}, | 149 {'a': 20, 'b': 20}, |
149 {'a': 10, 'b': 20}, | 150 {'a': 10, 'b': 20}, |
150 {'a': 20, 'b': 10}, | 151 {'a': 20, 'b': 10}, |
151 {'a': 10, 'b': 10}, | 152 {'a': 10, 'b': 10}, |
152 ]); | 153 ]); |
153 expect(parse('items | orderBy:["a", "-b"]').eval(scope.context, filters)).
toEqual([ | 154 expect(parse('items | orderBy:["a", "-b"]').eval(scope.context, formatters
)).toEqual([ |
154 {'a': 10, 'b': 20}, | 155 {'a': 10, 'b': 20}, |
155 {'a': 10, 'b': 10}, | 156 {'a': 10, 'b': 10}, |
156 {'a': 20, 'b': 20}, | 157 {'a': 20, 'b': 20}, |
157 {'a': 20, 'b': 10}, | 158 {'a': 20, 'b': 10}, |
158 ]); | 159 ]); |
159 expect(parse('items | orderBy:["a", "-b"]:true').eval(scope.context, filte
rs)).toEqual([ | 160 expect(parse('items | orderBy:["a", "-b"]:true').eval(scope.context, forma
tters)).toEqual([ |
160 {'a': 20, 'b': 10}, | 161 {'a': 20, 'b': 10}, |
161 {'a': 20, 'b': 20}, | 162 {'a': 20, 'b': 20}, |
162 {'a': 10, 'b': 10}, | 163 {'a': 10, 'b': 10}, |
163 {'a': 10, 'b': 20}, | 164 {'a': 10, 'b': 20}, |
164 ]); | 165 ]); |
165 })); | 166 }); |
166 | 167 |
167 it('should support function expressions', | 168 it('should support function expressions', |
168 inject((Scope scope, Parser parse, FilterMap filters) { | 169 (Scope scope, Parser parse, FormatterMap formatters) { |
169 scope.context['func'] = (e) => -(e['a'] + e['b']); | 170 scope.context['func'] = (e) => -(e['a'] + e['b']); |
170 expect(parse('items | orderBy:[func, "a", "-b"]').eval(scope.context, filt
ers)).toEqual([ | 171 expect(parse('items | orderBy:[func, "a", "-b"]').eval(scope.context, form
atters)).toEqual([ |
171 {'a': 20, 'b': 20}, | 172 {'a': 20, 'b': 20}, |
172 {'a': 10, 'b': 20}, | 173 {'a': 10, 'b': 20}, |
173 {'a': 20, 'b': 10}, | 174 {'a': 20, 'b': 10}, |
174 {'a': 10, 'b': 10}, | 175 {'a': 10, 'b': 10}, |
175 ]); | 176 ]); |
176 scope.context['func'] = (e) => (e is Name) ? e.lastName : e['lastName']; | 177 scope.context['func'] = (e) => (e is Name) ? e.lastName : e['lastName']; |
177 expect(parse('authors | orderBy:func').eval(scope.context, filters)).toEqu
al([ | 178 expect(parse('authors | orderBy:func').eval(scope.context, formatters)).to
Equal([ |
178 Jeffrey_Archer, | 179 Jeffrey_Archer, |
179 Isaac___Asimov, | 180 Isaac___Asimov, |
180 Emily___Bronte, | 181 Emily___Bronte, |
181 Mark____Twain, | 182 Mark____Twain, |
182 Oscar___Wilde, | 183 Oscar___Wilde, |
183 ]); | 184 ]); |
184 })); | 185 }); |
185 | 186 |
186 }); | 187 }); |
187 } | 188 } |
OLD | NEW |