test(matchers): add support for toMatchPattern in tests

This commit is contained in:
Matias Niemelä 2016-03-08 15:37:03 -08:00 committed by Alex Eagle
parent b691da26af
commit 169869a195
3 changed files with 36 additions and 0 deletions

View File

@ -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;
}

View File

@ -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) {

View File

@ -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});