2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, inject,} from '@angular/core/testing/testing_internal';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
|
2015-12-27 01:05:25 -05:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {ReplacePipe} from '@angular/common';
|
|
|
|
import {RegExpWrapper, StringJoiner} from '../../src/facade/lang';
|
2015-12-27 01:05:25 -05:00
|
|
|
|
|
|
|
export function main() {
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('ReplacePipe', () => {
|
2015-12-27 01:05:25 -05:00
|
|
|
var someNumber: number;
|
2016-06-17 13:57:32 -04:00
|
|
|
var str: string;
|
|
|
|
var pipe: ReplacePipe;
|
2015-12-27 01:05:25 -05:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
someNumber = 42;
|
|
|
|
str = 'Douglas Adams';
|
|
|
|
pipe = new ReplacePipe();
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('transform', () => {
|
2015-12-27 01:05:25 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should not support input other than strings and numbers', () => {
|
|
|
|
expect(() => pipe.transform({}, 'Douglas', 'Hugh')).toThrow();
|
|
|
|
expect(() => pipe.transform([1, 2, 3], 'Douglas', 'Hugh')).toThrow();
|
2015-12-27 01:05:25 -05:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should not support patterns other than strings and regular expressions', () => {
|
2016-06-17 13:57:32 -04:00
|
|
|
expect(() => pipe.transform(str, <any>{}, 'Hugh')).toThrow();
|
|
|
|
expect(() => pipe.transform(str, <any>null, 'Hugh')).toThrow();
|
|
|
|
expect(() => pipe.transform(str, <any>123, 'Hugh')).toThrow();
|
2015-12-27 01:05:25 -05:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should not support replacements other than strings and functions', () => {
|
2016-06-17 13:57:32 -04:00
|
|
|
expect(() => pipe.transform(str, 'Douglas', <any>{})).toThrow();
|
|
|
|
expect(() => pipe.transform(str, 'Douglas', <any>null)).toThrow();
|
|
|
|
expect(() => pipe.transform(str, 'Douglas', <any>123)).toThrow();
|
2015-12-27 01:05:25 -05:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should return a new string with the pattern replaced', () => {
|
|
|
|
var result1 = pipe.transform(str, 'Douglas', 'Hugh');
|
2015-12-27 01:05:25 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
var result2 = pipe.transform(str, RegExpWrapper.create('a'), '_');
|
2015-12-27 01:05:25 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
var result3 = pipe.transform(str, RegExpWrapper.create('a', 'i'), '_');
|
2015-12-27 01:05:25 -05:00
|
|
|
|
2016-06-17 13:57:32 -04:00
|
|
|
var f = ((x: any) => { return 'Adams!'; });
|
2015-12-27 01:05:25 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
var result4 = pipe.transform(str, 'Adams', f);
|
2015-12-27 01:05:25 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
var result5 = pipe.transform(someNumber, '2', '4');
|
2015-12-27 01:05:25 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(result1).toEqual('Hugh Adams');
|
|
|
|
expect(result2).toEqual('Dougl_s Ad_ms');
|
|
|
|
expect(result3).toEqual('Dougl_s _d_ms');
|
|
|
|
expect(result4).toEqual('Douglas Adams!');
|
|
|
|
expect(result5).toEqual('44');
|
2015-12-27 01:05:25 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|