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