Skip to content

Commit f784063

Browse files
committed
fix(test_lib): support multi matches with deep equality for function calls
1 parent 623edcd commit f784063

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

modules/angular2/src/test_lib/test_lib.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import 'package:angular2/src/facade/collection.dart' show StringMapWrapper;
1818
import './test_injector.dart';
1919
export './test_injector.dart' show inject;
2020

21+
import 'package:collection/equality.dart';
22+
2123
bool IS_DARTIUM = true;
2224

2325
List _testBindings = [];
@@ -76,6 +78,8 @@ Expect expect(actual, [matcher]) {
7678
return expect;
7779
}
7880

81+
const _u = null;
82+
7983
class Expect extends gns.Expect {
8084
Expect(actual) : super(actual);
8185

@@ -87,7 +91,32 @@ class Expect extends gns.Expect {
8791
void toImplement(expected) => toBeA(expected);
8892
void toBeNaN() => _expect(double.NAN.compareTo(actual) == 0, equals(true));
8993
void toHaveText(expected) => _expect(elementText(actual), expected);
94+
void toHaveBeenCalledWith([a = _u, b = _u, c = _u, d = _u, e = _u, f = _u]) =>
95+
_expect(_argsMatch(actual, a, b, c, d, e, f), true, reason: 'method invoked with correct arguments');
9096
Function get _expect => gns.guinness.matchers.expect;
97+
98+
// TODO(tbosch): move this hack into Guinness
99+
_argsMatch(spyFn, [a0 = _u, a1 = _u, a2 = _u, a3 = _u, a4 = _u, a5 = _u]) {
100+
var calls = spyFn.calls;
101+
final toMatch = _takeDefined([a0, a1, a2, a3, a4, a5]);
102+
if (calls.isEmpty) {
103+
return false;
104+
} else {
105+
gns.SamePropsMatcher matcher = new gns.SamePropsMatcher(toMatch);
106+
for (var i=0; i<calls.length; i++) {
107+
var call = calls[i];
108+
// TODO: create a better error message, not just 'Expected: <true> Actual: <false>'.
109+
// For hacking this is good:
110+
// print(call.positionalArguments);
111+
if (matcher.matches(call.positionalArguments, null)) {
112+
return true;
113+
}
114+
}
115+
return false;
116+
}
117+
}
118+
119+
List _takeDefined(List iter) => iter.takeWhile((_) => _ != _u).toList();
91120
}
92121

93122
class NotExpect extends gns.NotExpect {

modules/angular2/test/test_lib/test_lib_spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class TestObj {
1010
someFunc():number {
1111
return -1;
1212
}
13+
someComplexFunc(a) {
14+
return a;
15+
}
1316
}
1417

1518
@proxy
@@ -80,6 +83,18 @@ export function main() {
8083
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1,2);
8184
});
8285

86+
it("should match multiple function calls", () => {
87+
spyObj.someFunc(1,2);
88+
spyObj.someFunc(3,4);
89+
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1,2);
90+
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(3,4);
91+
});
92+
93+
it("should match using deep equality", () => {
94+
spyObj.someComplexFunc([1]);
95+
expect(spyObj.spy("someComplexFunc")).toHaveBeenCalledWith([1]);
96+
});
97+
8398
it("should support stubs", () => {
8499
var s = SpyObject.stub({"a":1}, {"b":2});
85100

0 commit comments

Comments
 (0)