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