fix(test_lib): support multi matches with deep equality for function calls

This commit is contained in:
Tobias Bosch 2015-04-23 09:13:42 -07:00
parent 623edcd2d8
commit f78406392b
2 changed files with 44 additions and 0 deletions

View File

@ -18,6 +18,8 @@ import 'package:angular2/src/facade/collection.dart' show StringMapWrapper;
import './test_injector.dart';
export './test_injector.dart' show inject;
import 'package:collection/equality.dart';
bool IS_DARTIUM = true;
List _testBindings = [];
@ -76,6 +78,8 @@ Expect expect(actual, [matcher]) {
return expect;
}
const _u = null;
class Expect extends gns.Expect {
Expect(actual) : super(actual);
@ -87,7 +91,32 @@ class Expect extends gns.Expect {
void toImplement(expected) => toBeA(expected);
void toBeNaN() => _expect(double.NAN.compareTo(actual) == 0, equals(true));
void toHaveText(expected) => _expect(elementText(actual), expected);
void toHaveBeenCalledWith([a = _u, b = _u, c = _u, d = _u, e = _u, f = _u]) =>
_expect(_argsMatch(actual, a, b, c, d, e, f), true, reason: 'method invoked with correct arguments');
Function get _expect => gns.guinness.matchers.expect;
// TODO(tbosch): move this hack into Guinness
_argsMatch(spyFn, [a0 = _u, a1 = _u, a2 = _u, a3 = _u, a4 = _u, a5 = _u]) {
var calls = spyFn.calls;
final toMatch = _takeDefined([a0, a1, a2, a3, a4, a5]);
if (calls.isEmpty) {
return false;
} else {
gns.SamePropsMatcher matcher = new gns.SamePropsMatcher(toMatch);
for (var i=0; i<calls.length; i++) {
var call = calls[i];
// TODO: create a better error message, not just 'Expected: <true> Actual: <false>'.
// For hacking this is good:
// print(call.positionalArguments);
if (matcher.matches(call.positionalArguments, null)) {
return true;
}
}
return false;
}
}
List _takeDefined(List iter) => iter.takeWhile((_) => _ != _u).toList();
}
class NotExpect extends gns.NotExpect {

View File

@ -10,6 +10,9 @@ class TestObj {
someFunc():number {
return -1;
}
someComplexFunc(a) {
return a;
}
}
@proxy
@ -80,6 +83,18 @@ export function main() {
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1,2);
});
it("should match multiple function calls", () => {
spyObj.someFunc(1,2);
spyObj.someFunc(3,4);
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1,2);
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(3,4);
});
it("should match using deep equality", () => {
spyObj.someComplexFunc([1]);
expect(spyObj.spy("someComplexFunc")).toHaveBeenCalledWith([1]);
});
it("should support stubs", () => {
var s = SpyObject.stub({"a":1}, {"b":2});