2015-10-13 03:29:13 -04:00
|
|
|
import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/testing_internal';
|
2016-04-12 12:40:37 -04:00
|
|
|
import {
|
|
|
|
isPresent,
|
|
|
|
RegExpWrapper,
|
|
|
|
RegExpMatcherWrapper,
|
|
|
|
StringWrapper,
|
|
|
|
hasConstructor,
|
|
|
|
resolveEnumToken
|
|
|
|
} from 'angular2/src/facade/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!!');
|
|
|
|
var indexes = [];
|
|
|
|
var m;
|
|
|
|
|
|
|
|
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-04-12 12:40:37 -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-04-12 12:40:37 -04:00
|
|
|
it("should implement replace all", () => {
|
2016-03-23 16:41:18 -04:00
|
|
|
let re = /(\d)+/g;
|
|
|
|
let m = RegExpWrapper.replaceAll(re, 'a1b2c', (match) => `!${match[1]}!`);
|
|
|
|
expect(m).toEqual('a!1!b!2!c');
|
|
|
|
});
|
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
|
|
|
|
|
|
|
describe('String', () => {
|
2015-10-31 16:04:26 -04:00
|
|
|
var s;
|
2015-08-30 20:04:15 -04:00
|
|
|
|
|
|
|
describe('slice', () => {
|
2016-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -04:00
|
|
|
() => { expect(StringWrapper.slice(s, 1)).toEqual("bcdefghij"); });
|
2015-08-30 20:04:15 -04:00
|
|
|
|
|
|
|
it('should support negative start',
|
2016-04-12 12:40:37 -04:00
|
|
|
() => { expect(StringWrapper.slice(s, -1)).toEqual("j"); });
|
2015-08-30 20:04:15 -04:00
|
|
|
|
|
|
|
it('should support negative end',
|
2016-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -04:00
|
|
|
var input = "~angular2 is amazing";
|
|
|
|
var expectedOutput = "angular2 is amazing";
|
2015-12-05 05:21:38 -05:00
|
|
|
|
2016-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -04:00
|
|
|
var input = "angular2 is amazing!";
|
|
|
|
var expectedOutput = "angular2 is amazing";
|
2015-12-05 05:21:38 -05:00
|
|
|
|
2016-04-12 12:40:37 -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-04-12 12:40:37 -04:00
|
|
|
var input = "angular2 is amazing+";
|
2015-12-05 05:21:38 -05:00
|
|
|
|
2016-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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-04-12 12:40:37 -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
|
|
|
});
|
|
|
|
}
|