From 169869a195438fc6eaa7181242a6a75b0f61a284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matias=20Niemel=C3=A4?= Date: Tue, 8 Mar 2016 15:37:03 -0800 Subject: [PATCH] test(matchers): add support for toMatchPattern in tests --- modules/angular2/src/testing/matchers.dart | 4 ++++ modules/angular2/src/testing/matchers.ts | 24 +++++++++++++++++++ .../test/testing/testing_internal_spec.ts | 8 +++++++ 3 files changed, 36 insertions(+) diff --git a/modules/angular2/src/testing/matchers.dart b/modules/angular2/src/testing/matchers.dart index c3381bbf3d..8e6a856a2a 100644 --- a/modules/angular2/src/testing/matchers.dart +++ b/modules/angular2/src/testing/matchers.dart @@ -55,6 +55,8 @@ class Expect extends gns.Expect { void toHaveCssStyle(styles) { gns.guinness.matchers.toBeTrue(elementContainsStyle(actual, styles)); } + void toMatchPattern(pattern) => + gns.guinness.matchers.toBeTrue(pattern.hasMatch(actual)); void toImplement(expected) => toBeA(expected); void toBeNaN() => gns.guinness.matchers.toBeTrue(double.NAN.compareTo(actual) == 0); @@ -98,6 +100,8 @@ class NotExpect extends gns.NotExpect { void toHaveCssStyle(styles) { gns.guinness.matchers.toBeFalse(elementContainsStyle(actual, styles)); } + void toMatchPattern(pattern) => + gns.guinness.matchers.toBeFalse(pattern.hasMatch(actual)); void toBeNull() => gns.guinness.matchers.toBeFalse(actual == null); Function get _expect => gns.guinness.matchers.expect; } diff --git a/modules/angular2/src/testing/matchers.ts b/modules/angular2/src/testing/matchers.ts index 7061afe180..aec11a8a80 100644 --- a/modules/angular2/src/testing/matchers.ts +++ b/modules/angular2/src/testing/matchers.ts @@ -78,6 +78,15 @@ export interface NgMatchers extends jasmine.Matchers { */ toThrowErrorWith(expectedMessage: any): boolean; + /** + * Expect a string to match the given regular expression. + * + * ## Example + * + * {@example testing/ts/matchers.ts region='toMatchPattern'} + */ + toMatchPattern(expectedMessage: any): boolean; + /** * Invert the matchers. */ @@ -240,6 +249,21 @@ _global.beforeEach(function() { }; }, + toMatchPattern() { + return {compare: buildError(false), negativeCompare: buildError(true)}; + + function buildError(isNot) { + return function(actual, regex) { + return { + pass: regex.test(actual) == !isNot, + get message() { + return `Expected ${actual} ${isNot ? 'not ' : ''}to match ${regex.toString()}`; + } + }; + }; + } + }, + toImplement: function() { return { compare: function(actualObject, expectedInterface) { diff --git a/modules/angular2/test/testing/testing_internal_spec.ts b/modules/angular2/test/testing/testing_internal_spec.ts index 78a04208f3..91785aa538 100644 --- a/modules/angular2/test/testing/testing_internal_spec.ts +++ b/modules/angular2/test/testing/testing_internal_spec.ts @@ -54,6 +54,14 @@ export function main() { }); }); + describe("toMatchPAttern", () => { + it("should assert that a string matches a given pattern", () => { + expect("matias").toMatchPattern(/ias$/g); + expect("tobias").toMatchPattern(/ias$/g); + expect("joonas").not.toMatchPattern(/ias$/g); + }); + }); + describe('toEqual for Maps', () => { it('should detect equality for same reference', () => { var m1 = MapWrapper.createFromStringMap({'a': 1});