2016-06-08 19:38:52 -04:00
|
|
|
import {beforeEach, ddescribe, describe, expect, iit, it, xit} from '@angular/core/testing';
|
2016-06-20 17:21:01 -04:00
|
|
|
import {NumberWrapper, RegExpMatcherWrapper, RegExpWrapper, StringWrapper, escapeRegExp, hasConstructor, isPresent, resolveEnumToken} from '../src/lang';
|
2015-05-26 20:00:31 -04:00
|
|
|
|
2016-03-08 19:38:50 -05:00
|
|
|
enum UsefulEnum {
|
|
|
|
MyToken,
|
|
|
|
MyOtherToken
|
|
|
|
}
|
|
|
|
|
2016-02-09 20:46:38 -05:00
|
|
|
class MySuperclass {}
|
|
|
|
class MySubclass extends MySuperclass {}
|
|
|
|
|
2015-05-26 20:00:31 -04:00
|
|
|
export function main() {
|
2015-06-02 11:07:13 -04:00
|
|
|
describe('RegExp', () => {
|
|
|
|
it('should expose the index for each match', () => {
|
2015-06-23 06:46:38 -04:00
|
|
|
var re = /(!)/g;
|
2015-06-02 11:07:13 -04:00
|
|
|
var matcher = RegExpWrapper.matcher(re, '0!23!567!!');
|
2016-06-20 17:21:01 -04:00
|
|
|
var indexes: number[] = [];
|
2016-06-08 18:45:15 -04:00
|
|
|
var m: any /** TODO #9100 */;
|
2015-06-02 11:07:13 -04:00
|
|
|
|
|
|
|
while (isPresent(m = RegExpMatcherWrapper.next(matcher))) {
|
2015-06-17 14:17:21 -04:00
|
|
|
indexes.push(m.index);
|
2015-06-02 11:07:13 -04:00
|
|
|
expect(m[0]).toEqual('!');
|
|
|
|
expect(m[1]).toEqual('!');
|
|
|
|
expect(m.length).toBe(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(indexes).toEqual([1, 4, 8, 9]);
|
|
|
|
});
|
2015-07-28 21:38:35 -04:00
|
|
|
|
|
|
|
it('should reset before it is reused', () => {
|
|
|
|
var re = /^['"]/g;
|
2016-06-08 19:38:52 -04:00
|
|
|
var str = '\'';
|
2015-07-28 21:38:35 -04:00
|
|
|
expect(RegExpWrapper.test(re, str)).toEqual(true);
|
|
|
|
// If not reset, the second attempt to test results in false
|
|
|
|
expect(RegExpWrapper.test(re, str)).toEqual(true);
|
|
|
|
});
|
2016-03-23 16:41:18 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should implement replace all', () => {
|
2016-03-23 16:41:18 -04:00
|
|
|
let re = /(\d)+/g;
|
2016-06-08 19:38:52 -04:00
|
|
|
let m =
|
|
|
|
RegExpWrapper.replaceAll(re, 'a1b2c', (match: any /** TODO #9100 */) => `!${match[1]}!`);
|
2016-03-23 16:41:18 -04:00
|
|
|
expect(m).toEqual('a!1!b!2!c');
|
|
|
|
});
|
2016-06-20 17:21:01 -04:00
|
|
|
|
|
|
|
it('should escape regexp', () => {
|
|
|
|
expect(RegExpWrapper.firstMatch(new RegExp(escapeRegExp('b')), 'abc')).toBeTruthy();
|
|
|
|
expect(RegExpWrapper.firstMatch(new RegExp(escapeRegExp('b')), 'adc')).toBeFalsy();
|
|
|
|
expect(RegExpWrapper.firstMatch(new RegExp(escapeRegExp('a.b')), 'a.b')).toBeTruthy();
|
|
|
|
expect(RegExpWrapper.firstMatch(new RegExp(escapeRegExp('axb')), 'a.b')).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
2015-06-02 11:07:13 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('const', () => {
|
|
|
|
it('should support const expressions both in TS and Dart', () => {
|
2016-04-26 00:47:33 -04:00
|
|
|
const numbers = /*@ts2dart_const*/[1, 2, 3];
|
2015-06-02 11:07:13 -04:00
|
|
|
expect(numbers).toEqual([1, 2, 3]);
|
|
|
|
});
|
|
|
|
});
|
2015-05-26 20:00:31 -04:00
|
|
|
|
2016-06-09 17:32:36 -04:00
|
|
|
describe('Number', () => {
|
|
|
|
describe('isNumeric', () => {
|
|
|
|
it('should return true when passing correct numeric string',
|
|
|
|
() => { expect(NumberWrapper.isNumeric('2')).toBe(true); });
|
|
|
|
|
|
|
|
it('should return true when passing correct double string',
|
|
|
|
() => { expect(NumberWrapper.isNumeric('1.123')).toBe(true); });
|
|
|
|
|
|
|
|
it('should return true when passing correct negative string',
|
|
|
|
() => { expect(NumberWrapper.isNumeric('-2')).toBe(true); });
|
|
|
|
|
|
|
|
it('should return true when passing correct scientific notation string',
|
|
|
|
() => { expect(NumberWrapper.isNumeric('1e5')).toBe(true); });
|
|
|
|
|
|
|
|
it('should return false when passing incorrect numeric',
|
|
|
|
() => { expect(NumberWrapper.isNumeric('a')).toBe(false); });
|
|
|
|
|
|
|
|
it('should return false when passing parseable but non numeric',
|
|
|
|
() => { expect(NumberWrapper.isNumeric('2a')).toBe(false); });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-26 20:00:31 -04:00
|
|
|
describe('String', () => {
|
2016-06-20 17:21:01 -04:00
|
|
|
var s: string;
|
2015-08-30 20:04:15 -04:00
|
|
|
|
|
|
|
describe('slice', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
beforeEach(() => { s = 'abcdefghij'; });
|
2015-08-30 20:04:15 -04:00
|
|
|
|
|
|
|
it('should return the whole string if neither start nor end are specified',
|
2016-06-08 19:38:52 -04:00
|
|
|
() => { expect(StringWrapper.slice(s)).toEqual('abcdefghij'); });
|
2015-08-30 20:04:15 -04:00
|
|
|
|
|
|
|
it('should return up to the end if end is not specified',
|
2016-06-08 19:38:52 -04:00
|
|
|
() => { expect(StringWrapper.slice(s, 1)).toEqual('bcdefghij'); });
|
2015-08-30 20:04:15 -04:00
|
|
|
|
|
|
|
it('should support negative start',
|
2016-06-08 19:38:52 -04:00
|
|
|
() => { expect(StringWrapper.slice(s, -1)).toEqual('j'); });
|
2015-08-30 20:04:15 -04:00
|
|
|
|
|
|
|
it('should support negative end',
|
2016-06-08 19:38:52 -04:00
|
|
|
() => { expect(StringWrapper.slice(s, -3, -1)).toEqual('hi'); });
|
2015-08-30 20:04:15 -04:00
|
|
|
|
2015-08-30 20:05:02 -04:00
|
|
|
it('should return empty string if start is greater than end', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(StringWrapper.slice(s, 4, 2)).toEqual('');
|
|
|
|
expect(StringWrapper.slice(s, -2, -4)).toEqual('');
|
2015-08-30 20:05:02 -04:00
|
|
|
});
|
2015-08-30 20:04:15 -04:00
|
|
|
});
|
|
|
|
|
2015-12-05 05:21:38 -05:00
|
|
|
describe('stripLeft', () => {
|
|
|
|
it('should strip the first character of the string if it matches the provided input', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
var input = '~angular2 is amazing';
|
|
|
|
var expectedOutput = 'angular2 is amazing';
|
2015-12-05 05:21:38 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(StringWrapper.stripLeft(input, '~')).toEqual(expectedOutput);
|
2015-12-05 05:21:38 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should keep stripping characters from the start until the first unmatched character',
|
|
|
|
() => {
|
2016-06-08 19:38:52 -04:00
|
|
|
var input = '#####hello';
|
|
|
|
var expectedOutput = 'hello';
|
|
|
|
expect(StringWrapper.stripLeft(input, '#')).toEqual(expectedOutput);
|
2015-12-05 05:21:38 -05:00
|
|
|
});
|
|
|
|
|
2015-12-16 02:47:48 -05:00
|
|
|
it('should not alter the provided input if the first character does not match the provided input',
|
2015-12-05 05:21:38 -05:00
|
|
|
() => {
|
2016-06-08 19:38:52 -04:00
|
|
|
var input = '+angular2 is amazing';
|
|
|
|
expect(StringWrapper.stripLeft(input, '*')).toEqual(input);
|
2015-12-05 05:21:38 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not do any alterations when an empty string or null value is passed in', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(StringWrapper.stripLeft('', 'S')).toEqual('');
|
|
|
|
expect(StringWrapper.stripLeft(null, 'S')).toEqual(null);
|
2015-12-05 05:21:38 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('stripRight', () => {
|
|
|
|
it('should strip the first character of the string if it matches the provided input', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
var input = 'angular2 is amazing!';
|
|
|
|
var expectedOutput = 'angular2 is amazing';
|
2015-12-05 05:21:38 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(StringWrapper.stripRight(input, '!')).toEqual(expectedOutput);
|
2015-12-05 05:21:38 -05:00
|
|
|
});
|
|
|
|
|
2015-12-16 02:47:48 -05:00
|
|
|
it('should not alter the provided input if the first character does not match the provided input',
|
2015-12-05 05:21:38 -05:00
|
|
|
() => {
|
2016-06-08 19:38:52 -04:00
|
|
|
var input = 'angular2 is amazing+';
|
2015-12-05 05:21:38 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(StringWrapper.stripRight(input, '*')).toEqual(input);
|
2015-12-05 05:21:38 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should keep stripping characters from the end until the first unmatched character',
|
|
|
|
() => {
|
2016-06-08 19:38:52 -04:00
|
|
|
var input = 'hi&!&&&&&';
|
|
|
|
var expectedOutput = 'hi&!';
|
|
|
|
expect(StringWrapper.stripRight(input, '&')).toEqual(expectedOutput);
|
2015-12-05 05:21:38 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should not do any alterations when an empty string or null value is passed in', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(StringWrapper.stripRight('', 'S')).toEqual('');
|
|
|
|
expect(StringWrapper.stripRight(null, 'S')).toEqual(null);
|
2015-12-05 05:21:38 -05:00
|
|
|
});
|
|
|
|
});
|
2016-02-09 20:46:38 -05:00
|
|
|
|
2016-03-08 19:38:50 -05:00
|
|
|
describe('resolveEnumToken', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should resolve a token given an enum and index values', () => {
|
2016-03-08 19:38:50 -05:00
|
|
|
var token = UsefulEnum.MyToken;
|
|
|
|
expect(resolveEnumToken(UsefulEnum, token)).toEqual('MyToken');
|
|
|
|
|
|
|
|
token = UsefulEnum.MyOtherToken;
|
|
|
|
expect(resolveEnumToken(UsefulEnum, token)).toEqual('MyOtherToken');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-09 20:46:38 -05:00
|
|
|
describe('hasConstructor', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should be true when the type matches',
|
2016-02-09 20:46:38 -05:00
|
|
|
() => { expect(hasConstructor(new MySuperclass(), MySuperclass)).toEqual(true); });
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should be false for subtypes',
|
2016-02-09 20:46:38 -05:00
|
|
|
() => { expect(hasConstructor(new MySubclass(), MySuperclass)).toEqual(false); });
|
|
|
|
});
|
2015-05-26 20:00:31 -04:00
|
|
|
});
|
|
|
|
}
|