Repository navigation
Expand file tree
/
Copy pathtest_lib.dart
More file actions
138 lines (110 loc) · 3.91 KB
/
Copy pathtest_lib.dart
File metadata and controls
138 lines (110 loc) · 3.91 KB
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
library test_lib.test_lib;
import 'package:guinness/guinness_html.dart' as gns;
export 'package:guinness/guinness_html.dart';
import 'package:unittest/unittest.dart' hide expect;
import 'dart:mirrors';
import 'dart:async';
import 'package:angular2/src/reflection/reflection.dart';
import 'package:angular2/src/reflection/reflection_capabilities.dart';
import 'package:collection/equality.dart';
bool IS_DARTIUM = true;
Expect expect(actual, [matcher]) {
final expect = new Expect(actual);
if (matcher != null) expect.to(matcher);
return expect;
}
class Expect extends gns.Expect {
Expect(actual) : super(actual);
NotExpect get not => new NotExpect(actual);
// TODO(tbosch) change back when https://github.com/vsavkin/guinness/issues/41 is fixed
// void toEqual(expected) => toHaveSameProps(expected);
void toEqual(expected) => _expect(actual, new FixedSamePropsMatcher(expected));
void toThrowError([message=""]) => this.toThrowWith(message: message);
void toBePromise() => _expect(actual is Future, equals(true));
void toImplement(expected) => toBeA(expected);
void toBeNaN() => _expect(double.NAN.compareTo(actual) == 0, equals(true));
Function get _expect => gns.guinness.matchers.expect;
}
class NotExpect extends gns.NotExpect {
NotExpect(actual) : super(actual);
// TODO(tbosch) change back when https://github.com/vsavkin/guinness/issues/41 is fixed
// void toEqual(expected) => toHaveSameProps(expected);
void toEqual(expected) => _expect(actual, isNot(new FixedSamePropsMatcher(expected)));
Function get _expect => gns.guinness.matchers.expect;
}
beforeEach(fn) {
gns.guinnessEnableHtmlMatchers();
gns.beforeEach(_enableReflection(fn));
}
it(name, fn) {
gns.it(name, _enableReflection(_handleAsync(fn)));
}
iit(name, fn) {
gns.iit(name, _enableReflection(_handleAsync(fn)));
}
_enableReflection(fn) {
return () {
reflector.reflectionCapabilities = new ReflectionCapabilities();
return fn();
};
}
_handleAsync(fn) {
ClosureMirror cm = reflect(fn);
MethodMirror mm = cm.function;
var completer = new Completer();
if (mm.parameters.length == 1) {
return () {
cm.apply([completer.complete]);
return completer.future;
};
}
return fn;
}
// TODO(tbosch): remove when https://github.com/vsavkin/guinness/issues/41
// is fixed
class FixedSamePropsMatcher extends Matcher {
final Object _expected;
const FixedSamePropsMatcher(this._expected);
bool matches(actual, Map matchState) {
return compare(toData(_expected), toData(actual));
}
Description describeMismatch(item, Description mismatchDescription,
Map matchState, bool verbose) =>
mismatchDescription.add('is equal to ${toData(item)}. Expected: ${toData(_expected)}');
Description describe(Description description) =>
description.add('has different properties');
toData(obj) => new _FixedObjToData().call(obj);
compare(d1, d2) => new DeepCollectionEquality().equals(d1, d2);
}
// TODO(tbosch): remove when https://github.com/vsavkin/guinness/issues/41
// is fixed
class _FixedObjToData {
final visitedObjects = new Set();
call(obj) {
if (visitedObjects.contains(obj)) return null;
visitedObjects.add(obj);
if (obj is num || obj is String || obj is bool) return obj;
if (obj is Iterable) return obj.map(call).toList();
if (obj is Map) return mapToData(obj);
return toDataUsingReflection(obj);
}
mapToData(obj) {
var res = {};
obj.forEach((k,v) {
res[call(k)] = call(v);
});
return res;
}
toDataUsingReflection(obj) {
final clazz = reflectClass(obj.runtimeType);
final instance = reflect(obj);
return clazz.declarations.values.fold({}, (map, decl) {
if (decl is VariableMirror && !decl.isPrivate && !decl.isStatic) {
final field = instance.getField(decl.simpleName);
final name = MirrorSystem.getName(decl.simpleName);
map[name] = call(field.reflectee);
}
return map;
});
}
}