Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1858)

Unified Diff: third_party/pkg/angular/lib/mock/test_injection.dart

Issue 124053002: Adding Angular and dependent packages for testing (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/pkg/angular/lib/mock/test_injection.dart
diff --git a/third_party/pkg/angular/lib/mock/test_injection.dart b/third_party/pkg/angular/lib/mock/test_injection.dart
new file mode 100644
index 0000000000000000000000000000000000000000..ae1542d34ea327e55f236ee524904382ec7f23c2
--- /dev/null
+++ b/third_party/pkg/angular/lib/mock/test_injection.dart
@@ -0,0 +1,134 @@
+part of angular.mock;
+
+_SpecInjector _currentSpecInjector = null;
+
+class _SpecInjector {
+ DynamicInjector moduleInjector;
+ DynamicInjector injector;
+ List<Module> modules = [];
+ List<Function> initFns = [];
+
+ _SpecInjector() {
+ var moduleModule = new Module()
+ ..factory(Module, (Injector injector) => addModule(new Module()));
+ moduleInjector = new DynamicInjector(modules: [moduleModule]);
+ }
+
+ addModule(module) {
+ if (injector != null) {
+ throw ["Injector already crated, can not add more modules."];
+ }
+ modules.add(module);
+ return module;
+ }
+
+ module(fnOrModule, [declarationStack]) {
+ try {
+ if (fnOrModule is Function) {
+ var initFn = moduleInjector.invoke(fnOrModule);
+ if (initFn is Function) {
+ initFns.add(initFn);
+ }
+ } else if (fnOrModule is Module) {
+ addModule(fnOrModule);
+ } else {
+ throw 'Unsupported type: $fnOrModule';
+ }
+ } catch (e, s) {
+ throw "$e\n$s\nDECLARED AT:$declarationStack";
+ }
+ }
+
+ inject(Function fn, [declarationStack]) {
+ try {
+ if (injector == null) {
+ injector = new DynamicInjector(modules: modules); // Implicit injection is disabled.
+ initFns.forEach((fn) {
+ injector.invoke(fn);
+ });
+ }
+ injector.invoke(fn);
+ } catch (e, s) {
+ throw "$e\n$s\nDECLARED AT:$declarationStack";
+ }
+ }
+
+ reset() {
+ injector = null;
+ }
+}
+
+/**
+ * Allows the injection of instances into a test. See [module] on how to install new
+ * types into injector.
+ *
+ * NOTE: Calling inject creates an injector, which prevents any more calls to [module].
+ *
+ * Typical usage:
+ *
+ * test('wrap whole test', inject((TestBed tb) {
+ * tb.compile(...);
+ * });
+ *
+ * test('wrap part of a test', () {
+ * module((Module module) {
+ * module.type(Foo);
+ * });
+ * inject((TestBed tb) {
+ * tb.compile(...);
+ * });
+ * });
+ *
+ */
+inject(Function fn) {
+ try { throw ''; } catch (e, stack) {
+ if (_currentSpecInjector == null ) {
+ return () => _currentSpecInjector.inject(fn, stack);
+ } else {
+ return _currentSpecInjector.inject(fn, stack);
+ }
+ }
+}
+
+/**
+ * Allows the installation of new types/modules into the current test injector.
+ *
+ * This method can be called in declaration or inline in test. The method can be called
+ * repeatedly, as long as [inject] is not called. Invocation of [inject] creates the injector and
+ * hence no more calls to [module] can be made.
+ *
+ * setUp(module((Module model) {
+ * module.type(Foo);
+ * });
+ *
+ * test('foo', () {
+ * module((Module module) {
+ * module.type(Foo);
+ * });
+ * });
+ */
+module(fnOrModule) {
+ try { throw ''; } catch(e, stack) {
+ if (_currentSpecInjector == null ) {
+ return () => _currentSpecInjector.module(fnOrModule, stack);
+ } else {
+ return _currentSpecInjector.module(fnOrModule, stack);
+ }
+ }
+}
+
+/**
+ * Call this method in your test harness [setUp] method to setup the injector.
+ */
+setUpInjector() {
+ _currentSpecInjector = new _SpecInjector();
+ _currentSpecInjector.module((Module m) {
+ m.install(new AngularModule());
+ m.install(new AngularMockModule());
+ });
+}
+
+/**
+ * Call this method in your test harness [tearDown] method to cleanup the injector.
+ */
+tearDownInjector() => _currentSpecInjector = null;

Powered by Google App Engine
This is Rietveld 408576698