X Tutup
Skip to content

Commit 169869a

Browse files
matskoalexeagle
authored andcommitted
test(matchers): add support for toMatchPattern in tests
1 parent b691da2 commit 169869a

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

modules/angular2/src/testing/matchers.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class Expect extends gns.Expect {
5555
void toHaveCssStyle(styles) {
5656
gns.guinness.matchers.toBeTrue(elementContainsStyle(actual, styles));
5757
}
58+
void toMatchPattern(pattern) =>
59+
gns.guinness.matchers.toBeTrue(pattern.hasMatch(actual));
5860
void toImplement(expected) => toBeA(expected);
5961
void toBeNaN() =>
6062
gns.guinness.matchers.toBeTrue(double.NAN.compareTo(actual) == 0);
@@ -98,6 +100,8 @@ class NotExpect extends gns.NotExpect {
98100
void toHaveCssStyle(styles) {
99101
gns.guinness.matchers.toBeFalse(elementContainsStyle(actual, styles));
100102
}
103+
void toMatchPattern(pattern) =>
104+
gns.guinness.matchers.toBeFalse(pattern.hasMatch(actual));
101105
void toBeNull() => gns.guinness.matchers.toBeFalse(actual == null);
102106
Function get _expect => gns.guinness.matchers.expect;
103107
}

modules/angular2/src/testing/matchers.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ export interface NgMatchers extends jasmine.Matchers {
7878
*/
7979
toThrowErrorWith(expectedMessage: any): boolean;
8080

81+
/**
82+
* Expect a string to match the given regular expression.
83+
*
84+
* ## Example
85+
*
86+
* {@example testing/ts/matchers.ts region='toMatchPattern'}
87+
*/
88+
toMatchPattern(expectedMessage: any): boolean;
89+
8190
/**
8291
* Invert the matchers.
8392
*/
@@ -240,6 +249,21 @@ _global.beforeEach(function() {
240249
};
241250
},
242251

252+
toMatchPattern() {
253+
return {compare: buildError(false), negativeCompare: buildError(true)};
254+
255+
function buildError(isNot) {
256+
return function(actual, regex) {
257+
return {
258+
pass: regex.test(actual) == !isNot,
259+
get message() {
260+
return `Expected ${actual} ${isNot ? 'not ' : ''}to match ${regex.toString()}`;
261+
}
262+
};
263+
};
264+
}
265+
},
266+
243267
toImplement: function() {
244268
return {
245269
compare: function(actualObject, expectedInterface) {

modules/angular2/test/testing/testing_internal_spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ export function main() {
5454
});
5555
});
5656

57+
describe("toMatchPAttern", () => {
58+
it("should assert that a string matches a given pattern", () => {
59+
expect("matias").toMatchPattern(/ias$/g);
60+
expect("tobias").toMatchPattern(/ias$/g);
61+
expect("joonas").not.toMatchPattern(/ias$/g);
62+
});
63+
});
64+
5765
describe('toEqual for Maps', () => {
5866
it('should detect equality for same reference', () => {
5967
var m1 = MapWrapper.createFromStringMap({'a': 1});

0 commit comments

Comments
 (0)
X Tutup