Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
fix(orderBy): ensure the orderBy formatter can handle null values in …
Browse files Browse the repository at this point in the history
…collections
  • Loading branch information
heisaf authored and rkirov committed Feb 11, 2015
1 parent eee5b5e commit 497c487
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/formatter/order_by.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ class OrderBy implements Function {
static _nop(e) => e;
static bool _isNonZero(int n) => (n != 0);
static int _returnZero() => 0;
static int _defaultComparator(a, b) => Comparable.compare(a, b);
static int _defaultComparator(a, b) {
if (a == null && b == null) return 0;
if (a == null) return -1;
if (b == null) return 1;

return Comparable.compare(a, b);
}
static int _reverseComparator(a, b) => _defaultComparator(b, a);

static int _compareLists(List a, List b, List<Comparator> comparators) {
Expand Down
41 changes: 41 additions & 0 deletions test/formatter/order_by_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,45 @@ main() {
});

});

describe('null safe orderBy formatter', () {
var Emily___Bronte = new Name(firstName: 'Emily', lastName: 'Bronte'),
Mark____Twain = {'firstName': 'Mark', 'lastName': 'Twain'},
Jeffrey_Archer = {'firstName': 'Jeffrey', 'lastName': 'Archer'},
Isaac___Asimov = new Name(firstName: 'Isaac', lastName: 'Asimov'),
Oscar___Wilde = {'firstName': 'Oscar', 'lastName': 'Wilde'},
Oscar___Null = {'firstName': 'Oscar', 'lastName': null};
beforeEach((Scope scope, Parser parse, FormatterMap formatters) {
scope.context['authors'] = [
Emily___Bronte,
Mark____Twain,
Jeffrey_Archer,
Isaac___Asimov,
Oscar___Wilde,
Oscar___Null
];
});
it('should allow null values',
(Scope scope, Parser parse, FormatterMap formatters) {
expect(parse('authors | orderBy:"lastName"').eval(scope.context, formatters)).toEqual([
Oscar___Null,
Jeffrey_Archer,
Isaac___Asimov,
Emily___Bronte,
Mark____Twain,
Oscar___Wilde,
]);
});
it('should allow null values in reverse order',
(Scope scope, Parser parse, FormatterMap formatters) {
expect(parse('authors | orderBy:"-lastName"').eval(scope.context, formatters)).toEqual([
Oscar___Wilde,
Mark____Twain,
Emily___Bronte,
Isaac___Asimov,
Jeffrey_Archer,
Oscar___Null,
]);
});
});
}

0 comments on commit 497c487

Please sign in to comment.