| 
									
										
										
										
											2015-02-19 10:20:38 -08:00
										 |  |  | library angular2.test; | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import 'dart:io'; | 
					
						
							|  |  |  | import 'package:barback/barback.dart'; | 
					
						
							|  |  |  | import 'package:angular2/transformer.dart'; | 
					
						
							|  |  |  | import 'package:code_transformers/tests.dart'; | 
					
						
							|  |  |  | import 'package:dart_style/dart_style.dart'; | 
					
						
							|  |  |  | import 'package:unittest/unittest.dart'; | 
					
						
							|  |  |  | import 'package:unittest/vm_config.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | main() { | 
					
						
							|  |  |  |   useVMConfiguration(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // TODO(kegluneq): Add a test for generating multiple annotations.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   group('Annotation tests:', _runTests); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var formatter = new DartFormatter(); | 
					
						
							| 
									
										
										
										
											2015-02-19 12:00:09 -08:00
										 |  |  | var transform = new AngularTransformer(new TransformerOptions('web/index.dart', | 
					
						
							| 
									
										
										
										
											2015-02-20 15:16:59 -08:00
										 |  |  |     reflectionEntryPoint: 'web/index.dart', | 
					
						
							|  |  |  |     newEntryPoint: 'web/index.bootstrap.dart')); | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class TestConfig { | 
					
						
							|  |  |  |   final String name; | 
					
						
							|  |  |  |   final Map<String, String> assetPathToInputPath; | 
					
						
							|  |  |  |   final Map<String, String> assetPathToExpectedOutputPath; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   TestConfig(this.name, | 
					
						
							|  |  |  |       {Map<String, String> inputs, Map<String, String> outputs}) | 
					
						
							|  |  |  |       : this.assetPathToInputPath = inputs, | 
					
						
							|  |  |  |         this.assetPathToExpectedOutputPath = outputs; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void _runTests() { | 
					
						
							| 
									
										
										
										
											2015-02-20 15:16:59 -08:00
										 |  |  |   /* | 
					
						
							|  |  |  |    * Each test has its own directory for inputs & an `expected` directory for | 
					
						
							|  |  |  |    * expected outputs. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * In addition to these declared inputs, we inject a set of common inputs for | 
					
						
							|  |  |  |    * every test. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   var commonInputs = { | 
					
						
							|  |  |  |     'angular2|lib/src/core/annotations/annotations.dart': | 
					
						
							|  |  |  |         '../../lib/src/core/annotations/annotations.dart', | 
					
						
							|  |  |  |     'angular2|lib/src/core/application.dart': 'common.dart', | 
					
						
							|  |  |  |     'angular2|lib/src/reflection/reflection_capabilities.dart': | 
					
						
							|  |  |  |         'reflection_capabilities.dart' | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  |   var tests = [ | 
					
						
							|  |  |  |     new TestConfig('Simple', | 
					
						
							|  |  |  |         inputs: { | 
					
						
							|  |  |  |       'a|web/index.dart': 'simple_annotation_files/index.dart', | 
					
						
							| 
									
										
										
										
											2015-02-20 15:16:59 -08:00
										 |  |  |       'a|web/bar.dart': 'simple_annotation_files/bar.dart' | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |         outputs: { | 
					
						
							|  |  |  |       'a|web/index.bootstrap.dart': | 
					
						
							| 
									
										
										
										
											2015-02-20 15:16:59 -08:00
										 |  |  |           'simple_annotation_files/expected/index.bootstrap.dart', | 
					
						
							|  |  |  |       'a|web/index.dart': 'simple_annotation_files/expected/index.dart', | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  |     }), | 
					
						
							|  |  |  |     new TestConfig('Two injected dependencies', | 
					
						
							|  |  |  |         inputs: { | 
					
						
							|  |  |  |       'a|web/index.dart': 'two_deps_files/index.dart', | 
					
						
							|  |  |  |       'a|web/foo.dart': 'two_deps_files/foo.dart', | 
					
						
							| 
									
										
										
										
											2015-02-20 15:16:59 -08:00
										 |  |  |       'a|web/bar.dart': 'two_deps_files/bar.dart' | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |         outputs: { | 
					
						
							|  |  |  |       'a|web/index.bootstrap.dart': | 
					
						
							|  |  |  |           'two_deps_files/expected/index.bootstrap.dart' | 
					
						
							|  |  |  |     }), | 
					
						
							|  |  |  |     new TestConfig('List of types', | 
					
						
							|  |  |  |         inputs: { | 
					
						
							|  |  |  |       'a|web/index.dart': 'list_of_types_files/index.dart', | 
					
						
							|  |  |  |       'a|web/foo.dart': 'list_of_types_files/foo.dart', | 
					
						
							| 
									
										
										
										
											2015-02-23 17:38:34 -08:00
										 |  |  |       'a|web/bar.dart': 'list_of_types_files/bar.dart' | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |         outputs: { | 
					
						
							|  |  |  |       'a|web/index.bootstrap.dart': | 
					
						
							|  |  |  |           'list_of_types_files/expected/index.bootstrap.dart' | 
					
						
							|  |  |  |     }), | 
					
						
							|  |  |  |     new TestConfig('Component with synthetic Constructor', | 
					
						
							|  |  |  |         inputs: { | 
					
						
							|  |  |  |       'a|web/index.dart': 'synthetic_ctor_files/index.dart', | 
					
						
							| 
									
										
										
										
											2015-02-23 17:38:34 -08:00
										 |  |  |       'a|web/bar.dart': 'synthetic_ctor_files/bar.dart' | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |         outputs: { | 
					
						
							|  |  |  |       'a|web/index.bootstrap.dart': | 
					
						
							|  |  |  |           'synthetic_ctor_files/expected/index.bootstrap.dart' | 
					
						
							| 
									
										
										
										
											2015-02-19 12:00:09 -08:00
										 |  |  |     }), | 
					
						
							|  |  |  |     new TestConfig('Component with two annotations', | 
					
						
							|  |  |  |         inputs: { | 
					
						
							|  |  |  |       'a|web/index.dart': 'two_annotations_files/index.dart', | 
					
						
							|  |  |  |       'a|web/bar.dart': 'two_annotations_files/bar.dart', | 
					
						
							|  |  |  |       'angular2|lib/src/core/annotations/template.dart': | 
					
						
							| 
									
										
										
										
											2015-02-23 17:38:34 -08:00
										 |  |  |           '../../lib/src/core/annotations/template.dart' | 
					
						
							| 
									
										
										
										
											2015-02-19 12:00:09 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |         outputs: { | 
					
						
							|  |  |  |       'a|web/index.bootstrap.dart': | 
					
						
							|  |  |  |           'two_annotations_files/expected/index.bootstrap.dart' | 
					
						
							| 
									
										
										
										
											2015-02-23 17:38:34 -08:00
										 |  |  |     }), | 
					
						
							|  |  |  |     new TestConfig('Basic `bind`', | 
					
						
							|  |  |  |         inputs: { | 
					
						
							|  |  |  |       'a|web/index.dart': 'basic_bind_files/index.dart', | 
					
						
							|  |  |  |       'a|web/bar.dart': 'basic_bind_files/bar.dart' | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |         outputs: { | 
					
						
							|  |  |  |       'a|web/index.bootstrap.dart': | 
					
						
							|  |  |  |           'basic_bind_files/expected/index.bootstrap.dart' | 
					
						
							| 
									
										
										
										
											2015-02-20 15:16:59 -08:00
										 |  |  |     }) | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  |   ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var cache = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (var config in tests) { | 
					
						
							| 
									
										
										
										
											2015-02-20 15:16:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  |     // Read in input & output files.
 | 
					
						
							| 
									
										
										
										
											2015-02-20 15:16:59 -08:00
										 |  |  |     config.assetPathToInputPath | 
					
						
							|  |  |  |       ..addAll(commonInputs) | 
					
						
							|  |  |  |       ..forEach((key, value) { | 
					
						
							| 
									
										
										
										
											2015-02-23 17:38:34 -08:00
										 |  |  |         config.assetPathToInputPath[ | 
					
						
							|  |  |  |             key] = cache.putIfAbsent(value, () => _readFile(value)); | 
					
						
							| 
									
										
										
										
											2015-02-20 15:16:59 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  |     config.assetPathToExpectedOutputPath.forEach((key, value) { | 
					
						
							|  |  |  |       config.assetPathToExpectedOutputPath[key] = cache.putIfAbsent(value, () { | 
					
						
							| 
									
										
										
										
											2015-02-23 17:38:34 -08:00
										 |  |  |         var code = _readFile(value); | 
					
						
							| 
									
										
										
										
											2015-02-17 08:38:54 -08:00
										 |  |  |         return value.endsWith('dart') ? formatter.format(code) : code; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     testPhases(config.name, [ | 
					
						
							|  |  |  |       [transform] | 
					
						
							|  |  |  |     ], config.assetPathToInputPath, config.assetPathToExpectedOutputPath, []); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-23 17:38:34 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /// Smoothes over differences in CWD between IDEs and running tests in Travis.
 | 
					
						
							|  |  |  | String _readFile(String path) { | 
					
						
							|  |  |  |   for (var myPath in [path, 'test/transform/${path}']) { | 
					
						
							|  |  |  |     var file = new File(myPath); | 
					
						
							|  |  |  |     if (file.existsSync()) { | 
					
						
							|  |  |  |       return file.readAsStringSync(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return null; | 
					
						
							|  |  |  | } |