From f78406392b7c66c280814822415eb355bc570a7a Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Thu, 23 Apr 2015 09:13:42 -0700 Subject: [PATCH] fix(test_lib): support multi matches with deep equality for function calls --- modules/angular2/src/test_lib/test_lib.dart | 29 +++++++++++++++++++ .../angular2/test/test_lib/test_lib_spec.js | 15 ++++++++++ 2 files changed, 44 insertions(+) diff --git a/modules/angular2/src/test_lib/test_lib.dart b/modules/angular2/src/test_lib/test_lib.dart index 9b3417ad07..f30cae7358 100644 --- a/modules/angular2/src/test_lib/test_lib.dart +++ b/modules/angular2/src/test_lib/test_lib.dart @@ -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 Actual: '. + // 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 { diff --git a/modules/angular2/test/test_lib/test_lib_spec.js b/modules/angular2/test/test_lib/test_lib_spec.js index 3d39112d2d..7dff12fbb1 100644 --- a/modules/angular2/test/test_lib/test_lib_spec.js +++ b/modules/angular2/test/test_lib/test_lib_spec.js @@ -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});