Permalink
Browse files

feature: Set injector in ComponenHandler via mockComponentHandler

  • Loading branch information...
MikeMitterer committed Dec 11, 2015
1 parent 3d9ca1f commit 783bc1263853a8d134cf6c3d8ebe0d44bed3d850
Showing with 81 additions and 9 deletions.
  1. +3 −1 lib/mdlcore.dart
  2. +40 −7 lib/mdlmock.dart
  3. +1 −0 lib/src/core/MdlComponentHandler.dart
  4. +36 −0 lib/src/core/mock.dart
  5. +1 −1 tool/grind.dart
View
@@ -33,9 +33,10 @@ import 'package:validate/validate.dart';
import 'package:di/di.dart' as di;
part "src/core/annotations.dart";
part "src/core/ConvertValue.dart";
part "src/core/interfaces.dart";
part "src/core/mock.dart";
part "src/core/utils.dart";
part "src/core/ConvertValue.dart";
part "src/core/MdlComponentHandler.dart";
part "src/core/MdlConfig.dart";
@@ -46,3 +47,4 @@ abstract class MdlDataConsumer {
}
View
@@ -17,6 +17,23 @@
* limitations under the License.
*/
/// Helps testing MDL/Dart + DI
///
/// import 'package:mdl/mdlmock.dart' as mdlmock;
///
/// main() {
/// ...
/// setUp( () {
///
/// mdlmock.setUpInjector();
/// mdlmock.module((final di.Module module) {
/// module.bind(SignalService, toImplementation: SignalServiceImpl);
/// module.bind(Translator, toValue: _translator);
/// });
///
/// mdlmock.mockComponentHandler(mdlmock.injector(),mdlmock.componentFactory());
/// }
/// }
library mdlmock;
//import 'package:logging/logging.dart';
@@ -26,6 +43,8 @@ import 'dart:mirrors';
import 'package:di/di.dart' as di;
import 'package:mdl/mdlapplication.dart';
export 'package:mdl/mdlcore.dart' show mockComponentHandler;
export 'package:mdl/mdlapplication.dart' show componentFactory;
_MdlInjector _mdlInjector = null;
@@ -62,6 +81,7 @@ class _MdlInjector {
// - private -------------------------------------------------------------------------------------------------------
/// Creates the "Injector" also called by the global [injector()] Function
void _create() {
if(_injector == null) {
_injector = new di.ModuleInjector(_modules);
@@ -76,10 +96,7 @@ class MdlMockModule extends di.Module {
}
}
/**
* Call this method in your test harness [setUp] method to setup the injector.
*/
/// Call this method in your test harness [setUp] method to setup the injector.
void setUpInjector() {
_mdlInjector = new _MdlInjector();
@@ -106,10 +123,26 @@ void inject(Function fn) {
_mdlInjector.inject(fn);
}
/**
* Call this method in your test harness [tearDown] method to cleanup the injector.
*/
/// Call this method in your test harness [tearDown] method to cleanup the injector.
void tearDownInjector() {
_mdlInjector = null;
}
/// Returns this mocked injector
///
/// setUp(() {
/// mdlmock.setUpInjector();
/// mdlmock.module((final di.Module module) {
/// module.bind(SignalService, toImplementation: SignalServiceImpl);
/// module.bind(Translator, toValue: _translator);
/// });
/// mockComponentHandler(mdlmock.injector(),componentFactory());
/// });
di.Injector injector() {
if(_mdlInjector == null) {
setUpInjector();
}
_mdlInjector._create();
return _mdlInjector._injector;
}
@@ -81,6 +81,7 @@ class MdlComponentHandler {
bool _enableVisualDebugging = false;
/// The injector for this module.
/// Can also set via [mockComponentHandler]
di.Injector _injector;
/**
View
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2015, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
part of mdlcore;
/// Purpose of [mockComponentHandler] is to inject the componentHandler
/// with a mocked Injector for testing
///
/// setUp(() {
/// mdlmock.setUpInjector();
/// mdlmock.module((final di.Module module) {
/// module.bind(SignalService, toImplementation: SignalServiceImpl);
/// module.bind(Translator, toValue: _translator);
/// });
/// mockComponentHandler(mdlmock.injector(),componentFactory());
/// });
///
void mockComponentHandler(final di.Injector injector,final MdlComponentHandler componentHandler) {
componentHandler._injector = injector;
}
View
@@ -42,7 +42,7 @@ analyze() {
"lib/mdltemplate.dart",
"lib/mdlutils.dart",
"lib/transformer.dart",
"lib/src/grinder.dart"
"lib/src/grinder/grinder.dart"
];
libs.forEach((final String lib) => Analyzer.analyze(lib));

0 comments on commit 783bc12

Please sign in to comment.