feat(test_lib): add `containsRegex`

This commit is contained in:
Tobias Bosch 2015-05-26 13:57:13 -07:00
parent ef3e12e803
commit 23d59df81a
2 changed files with 24 additions and 3 deletions

View File

@ -1,6 +1,6 @@
import {List, ListWrapper} from 'angular2/src/facade/collection'; import {List, ListWrapper} from 'angular2/src/facade/collection';
import {DOM} from 'angular2/src/dom/dom_adapter'; import {DOM} from 'angular2/src/dom/dom_adapter';
import {isPresent} from 'angular2/src/facade/lang'; import {isPresent, RegExpWrapper, StringWrapper, RegExp} from 'angular2/src/facade/lang';
import {resolveInternalDomView} from 'angular2/src/render/dom/view/view'; import {resolveInternalDomView} from 'angular2/src/render/dom/view/view';
export class Log { export class Log {
@ -41,3 +41,10 @@ export function dispatchEvent(element, eventType) {
export function el(html: string) { export function el(html: string) {
return DOM.firstChild(DOM.content(DOM.createTemplate(html))); return DOM.firstChild(DOM.content(DOM.createTemplate(html)));
} }
var _RE_SPECIAL_CHARS = ['-', '[', ']', '/', '{', '}', '\\', '(', ')', '*', '+', '?', '.', '^', '$', '|'];
var _ESCAPE_RE = RegExpWrapper.create(`[\\${_RE_SPECIAL_CHARS.join('\\')}]`);
export function containsRegexp(input: string): RegExp {
return RegExpWrapper.create(StringWrapper.replaceAllMapped(input, _ESCAPE_RE, (match) => `\\${match[0]}`));
}

View File

@ -1,6 +1,6 @@
import {describe, it, iit, ddescribe, expect, tick, async, SpyObject, beforeEach, proxy} from 'angular2/test_lib'; import {describe, it, iit, ddescribe, expect, tick, async, SpyObject, beforeEach, proxy, containsRegexp} from 'angular2/test_lib';
import {MapWrapper} from 'angular2/src/facade/collection'; import {MapWrapper} from 'angular2/src/facade/collection';
import {IMPLEMENTS} from 'angular2/src/facade/lang'; import {IMPLEMENTS, RegExpWrapper} from 'angular2/src/facade/lang';
class TestObj { class TestObj {
prop; prop;
@ -118,5 +118,19 @@ export function main() {
expect(spyObj.someFunc()).toBe(null); expect(spyObj.someFunc()).toBe(null);
}); });
}); });
describe('containsRegexp', () => {
it('should allow any prefix and suffix', () => {
expect(RegExpWrapper.firstMatch(containsRegexp('b'), 'abc')).toBeTruthy();
expect(RegExpWrapper.firstMatch(containsRegexp('b'), 'adc')).toBeFalsy();
});
it('should match various special characters', () => {
expect(RegExpWrapper.firstMatch(containsRegexp('a.b'), 'a.b')).toBeTruthy();
expect(RegExpWrapper.firstMatch(containsRegexp('axb'), 'a.b')).toBeFalsy();
});
});
}); });
} }