From 23d59df81a4705d65357c44279088d67fc9e3d0b Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Tue, 26 May 2015 13:57:13 -0700 Subject: [PATCH] feat(test_lib): add `containsRegex` --- modules/angular2/src/test_lib/utils.ts | 9 ++++++++- .../angular2/test/test_lib/test_lib_spec.js | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/modules/angular2/src/test_lib/utils.ts b/modules/angular2/src/test_lib/utils.ts index f939c5fd6b..9e2d8a86a8 100644 --- a/modules/angular2/src/test_lib/utils.ts +++ b/modules/angular2/src/test_lib/utils.ts @@ -1,6 +1,6 @@ import {List, ListWrapper} from 'angular2/src/facade/collection'; 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'; export class Log { @@ -41,3 +41,10 @@ export function dispatchEvent(element, eventType) { export function el(html: string) { 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]}`)); +} + diff --git a/modules/angular2/test/test_lib/test_lib_spec.js b/modules/angular2/test/test_lib/test_lib_spec.js index 069fa6cfd8..25515e03d2 100644 --- a/modules/angular2/test/test_lib/test_lib_spec.js +++ b/modules/angular2/test/test_lib/test_lib_spec.js @@ -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 {IMPLEMENTS} from 'angular2/src/facade/lang'; +import {IMPLEMENTS, RegExpWrapper} from 'angular2/src/facade/lang'; class TestObj { prop; @@ -118,5 +118,19 @@ export function main() { 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(); + }); + + }); }); }