| Index: third_party/pkg/angular/test/filter/filter_spec.dart
|
| diff --git a/third_party/pkg/angular/test/filter/filter_spec.dart b/third_party/pkg/angular/test/filter/filter_spec.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ca8552e187580b85febcda7f5b09c866defc7b03
|
| --- /dev/null
|
| +++ b/third_party/pkg/angular/test/filter/filter_spec.dart
|
| @@ -0,0 +1,188 @@
|
| +library filter_spec;
|
| +
|
| +import 'dart:mirrors';
|
| +import 'dart:collection';
|
| +import '../_specs.dart';
|
| +
|
| +// Helper to simulate some real objects. Purposefully doesn't implement Map.
|
| +class DynamicObject {
|
| + Map<String, dynamic> __map__;
|
| + DynamicObject([Map init]): __map__ = (init == null) ? {} : init;
|
| + toString() => Maps.mapToString(__map__);
|
| + operator ==(DynamicObject other) {
|
| + return (__map__.length == other.__map__.length &&
|
| + __map__.keys.toSet().difference(other.__map__.keys.toSet()).length == 0 &&
|
| + __map__.keys.every((key) => __map__[key] == other.__map__[key]));
|
| + }
|
| + noSuchMethod(Invocation invocation) {
|
| + String name = MirrorSystem.getName(invocation.memberName);
|
| + if (invocation.isGetter) {
|
| + return __map__[name];
|
| + } else if (invocation.isSetter) {
|
| + return __map__[name.substring(0, name.length - 1)] = invocation.positionalArguments[0];
|
| + } else if (invocation.isMethod) {
|
| + return (reflect(__map__[name]) as ClosureMirror).apply(
|
| + invocation.positionalArguments, invocation.namedArguments);
|
| + } else {
|
| + throw "DynamicObject: Unexpected invocation.";
|
| + }
|
| + }
|
| +}
|
| +
|
| +main() {
|
| + D([Map init]) => new DynamicObject(init);
|
| +
|
| + describe('filter filter', () {
|
| + var filter;
|
| +
|
| + beforeEach(() => inject((Injector injector, FilterMap filterMap) {
|
| + filter = injector.get(filterMap[new NgFilter(name: 'filter')]);
|
| + }));
|
| +
|
| + it('should filter by string', () {
|
| + List items = ['MIsKO',
|
| + {'name': 'shyam'},
|
| + ['adam'],
|
| + [],
|
| + 1234,
|
| + D({'name': 'shyam'})];
|
| + expect(filter.call(items, null).length).toBe(6);
|
| + expect(filter.call(items, '').length).toBe(4);
|
| +
|
| + expect(filter.call(items, 'iSk').length).toBe(1);
|
| + expect(filter.call(items, 'isk')[0]).toBe('MIsKO');
|
| +
|
| + expect(filter.call(items, 'yam').length).toBe(1);
|
| + expect(filter.call(items, 'yam')[0]).toEqual(items[1]);
|
| +
|
| + expect(filter.call(items, 'da').length).toBe(1);
|
| + expect(filter.call(items, 'da')[0]).toEqual(items[2]);
|
| +
|
| + expect(filter.call(items, 34).length).toBe(0);
|
| + expect(filter.call(items, 1234)).toEqual([1234]);
|
| +
|
| + expect(filter.call(items, '34').length).toBe(1);
|
| + expect(filter.call(items, '34')[0]).toBe(1234);
|
| +
|
| + expect(filter.call(items, "I don't exist").length).toBe(0);
|
| + });
|
| +
|
| + it('should filter bool items', () {
|
| + List items = ['truefalse', true, false, null];
|
| + // true
|
| + expect(filter.call(items, true)).toEqual([true]);
|
| + expect(filter.call(items, 'true')).toEqual(['truefalse', true]);
|
| + expect(filter.call(items, 'TrUe')).toEqual(['truefalse', true]);
|
| + expect(filter.call(items, 'yes')).toEqual([true]);
|
| + expect(filter.call(items, 'on')).toEqual([true]);
|
| + // false
|
| + expect(filter.call(items, false)).toEqual([false]);
|
| + expect(filter.call(items, 'FaLSe')).toEqual(['truefalse', false]);
|
| + expect(filter.call(items, 'no')).toEqual([false]);
|
| + expect(filter.call(items, 'off')).toEqual([false]);
|
| + });
|
| +
|
| + it(r'should not read $ properties', () {
|
| + List items = [{r'$name': 'misko'}];
|
| + expect(filter(items, 'misko').length).toBe(0);
|
| + });
|
| +
|
| + it('should filter on specific property', () {
|
| + List items = [{'name': 'a', 'ignore': 'a'},
|
| + {'name': 'abc', 'ignore': 'a'},
|
| + D({'name': 'abd'})];
|
| + expect(filter(items, {}).length).toBe(3);
|
| +
|
| + expect(filter(items, {'name': 'a'}).length).toBe(3);
|
| +
|
| + expect(filter(items, {'name': 'ab'}).length).toBe(2);
|
| + expect(filter(items, {'name': 'ab'})[0]['name']).toBe('abc');
|
| + expect(filter(items, {'name': 'ab'})[1].name).toBe('abd');
|
| +
|
| + expect(filter(items, {'name': 'c'}).length).toBe(1);
|
| + expect(filter(items, {'name': 'c'})[0]['name']).toBe('abc');
|
| + });
|
| +
|
| + it('should take function as predicate', () {
|
| + List items = [{'name': 'a'},
|
| + {'name': 'abc', 'done': true},
|
| + D({'name': 'abc', 'done': true})];
|
| + fn(i) => (i is Map) ? i['done']: i.done;
|
| + expect(filter(items, fn).length).toBe(2);
|
| + });
|
| +
|
| + it('should take object as predicate', () {
|
| + List items = [{'first': 'misko', 'last': 'hevery'},
|
| + D({'first': 'adam', 'last': 'abrons'})];
|
| +
|
| + expect(filter(items, {'first':'', 'last':''}).length).toBe(2);
|
| + expect(filter(items, {'first':'', 'last':'hevery'}).length).toBe(1);
|
| + expect(filter(items, {'first':'adam', 'last':'hevery'}).length).toBe(0);
|
| + expect(filter(items, {'first':'misko', 'last':'hevery'}).length).toBe(1);
|
| + expect(filter(items, {'first':'misko', 'last':'hevery'})[0]).toEqual(items[0]);
|
| + });
|
| +
|
| + it('should support boolean properties', () {
|
| + List items = [{'name': 'tom', 'current': true},
|
| + D({'name': 'demi', 'current': false}),
|
| + {'name': 'sofia'}];
|
| +
|
| + expect(filter(items, {'current':true}).length).toBe(1);
|
| + expect(filter(items, {'current':true})[0]['name']).toBe('tom');
|
| + expect(filter(items, {'current':false}).length).toBe(1);
|
| + expect(filter(items, {'current':false})[0].name).toBe('demi');
|
| + });
|
| +
|
| + it('should support negation operator', () {
|
| + List items = ['misko', 'adam'];
|
| +
|
| + expect(filter(items, '!isk').length).toBe(1);
|
| + expect(filter(items, '!isk')[0]).toEqual(items[1]);
|
| + });
|
| +
|
| + describe('should support comparator', () {
|
| +
|
| + it('as equality when true', () {
|
| + List items = ['misko', 'adam', 'adamson'];
|
| + var expr = 'adam';
|
| +
|
| + expect(filter(items, expr, true)).toEqual([items[1]]);
|
| + expect(filter(items, expr, false)).toEqual([items[1], items[2]]);
|
| +
|
| + items = [{'key': 'value1', 'nonkey': 1},
|
| + {'key': 'value2', 'nonkey': 2},
|
| + {'key': 'value12', 'nonkey': 3},
|
| + D({'key': 'value1', 'nonkey': 4}),
|
| + D({'key': 'Value1', 'nonkey': 5})];
|
| + expr = {'key': 'value1'};
|
| + expect(filter(items, expr, true)).toEqual([items[0], items[3]]);
|
| +
|
| + items = [{'key': 1, 'nonkey': 1},
|
| + {'key': 2, 'nonkey': 2},
|
| + {'key': 12, 'nonkey': 3},
|
| + {'key': 1, 'nonkey': 4}];
|
| + expr = {'key': 1};
|
| + expect(filter(items, expr, true)).toEqual([items[0], items[3]]);
|
| +
|
| + expr = 12;
|
| + expect(filter(items, expr, true)).toEqual([items[2]]);
|
| + });
|
| +
|
| + it('and use the function given to compare values', () {
|
| + List items = [{'key': 1, 'nonkey': 1},
|
| + {'key': 2, 'nonkey': 2},
|
| + D({'key': 12, 'nonkey': 3}),
|
| + {'key': 1, 'nonkey': 14},
|
| + {'key': 13, 'nonkey': 14}];
|
| + var expr = {'key': 10};
|
| + var comparator = (obj, value) => obj is num && obj > value;
|
| + expect(filter(items, expr, comparator)).toEqual([items[2], items[4]]);
|
| +
|
| + expr = 10;
|
| + // DynamicObject doesn't match!
|
| + expect(filter(items, expr, comparator)).toEqual([items[3], items[4]]);
|
| + });
|
| +
|
| + });
|
| + });
|
| +}
|
|
|