2014-10-02 15:27:01 -04:00
|
|
|
library test_lib.test_lib;
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-07-17 17:01:55 -04:00
|
|
|
import 'dart:async';
|
|
|
|
|
2015-02-27 17:50:06 -05:00
|
|
|
import 'package:guinness/guinness.dart' as gns;
|
2015-05-08 22:51:19 -04:00
|
|
|
export 'package:guinness/guinness.dart'
|
2015-06-02 15:33:08 -04:00
|
|
|
hide
|
|
|
|
Expect,
|
|
|
|
expect,
|
|
|
|
NotExpect,
|
|
|
|
beforeEach,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
xit,
|
|
|
|
SpyObject,
|
|
|
|
SpyFunction;
|
2015-03-13 06:10:11 -04:00
|
|
|
|
2015-10-08 18:33:17 -04:00
|
|
|
export 'matchers.dart' show expect, Expect, NotExpect;
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-08-20 17:28:25 -04:00
|
|
|
import 'package:angular2/src/core/reflection/reflection.dart';
|
|
|
|
import 'package:angular2/src/core/reflection/reflection_capabilities.dart';
|
2015-03-13 06:10:11 -04:00
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
import 'package:angular2/src/core/di/provider.dart' show bind;
|
2015-08-20 17:28:25 -04:00
|
|
|
import 'package:angular2/src/core/di/injector.dart' show Injector;
|
|
|
|
import 'package:angular2/src/core/facade/collection.dart' show StringMapWrapper;
|
2015-03-13 06:10:11 -04:00
|
|
|
|
2015-07-17 17:01:55 -04:00
|
|
|
import 'test_injector.dart';
|
|
|
|
export 'test_injector.dart' show inject;
|
2015-03-13 06:10:11 -04:00
|
|
|
|
|
|
|
List _testBindings = [];
|
|
|
|
Injector _injector;
|
|
|
|
bool _isCurrentTestAsync;
|
|
|
|
bool _inIt = false;
|
|
|
|
|
|
|
|
class AsyncTestCompleter {
|
2015-05-05 15:59:54 -04:00
|
|
|
final _completer = new Completer();
|
2015-03-13 06:10:11 -04:00
|
|
|
|
2015-05-05 15:59:54 -04:00
|
|
|
void done() {
|
2015-03-13 06:10:11 -04:00
|
|
|
_completer.complete();
|
|
|
|
}
|
|
|
|
|
2015-05-05 15:59:54 -04:00
|
|
|
Future get future => _completer.future;
|
2015-03-13 06:10:11 -04:00
|
|
|
}
|
|
|
|
|
2015-05-05 15:59:54 -04:00
|
|
|
void testSetup() {
|
2015-03-13 06:10:11 -04:00
|
|
|
reflector.reflectionCapabilities = new ReflectionCapabilities();
|
|
|
|
// beforeEach configuration:
|
|
|
|
// - Priority 3: clear the bindings before each test,
|
|
|
|
// - Priority 2: collect the bindings before each test, see beforeEachBindings(),
|
|
|
|
// - Priority 1: create the test injector to be used in beforeEach() and it()
|
|
|
|
|
2015-05-08 22:51:19 -04:00
|
|
|
gns.beforeEach(() {
|
|
|
|
_testBindings.clear();
|
|
|
|
}, priority: 3);
|
2015-03-13 06:10:11 -04:00
|
|
|
|
|
|
|
var completerBinding = bind(AsyncTestCompleter).toFactory(() {
|
|
|
|
// Mark the test as async when an AsyncTestCompleter is injected in an it(),
|
|
|
|
if (!_inIt) throw 'AsyncTestCompleter can only be injected in an "it()"';
|
|
|
|
_isCurrentTestAsync = true;
|
|
|
|
return new AsyncTestCompleter();
|
|
|
|
});
|
|
|
|
|
2015-05-08 22:51:19 -04:00
|
|
|
gns.beforeEach(() {
|
|
|
|
_isCurrentTestAsync = false;
|
|
|
|
_testBindings.add(completerBinding);
|
|
|
|
_injector = createTestInjector(_testBindings);
|
|
|
|
}, priority: 1);
|
2015-03-13 06:10:11 -04:00
|
|
|
}
|
|
|
|
|
2014-11-21 18:13:01 -05:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
/**
|
|
|
|
* Allows overriding default bindings defined in test_injector.js.
|
|
|
|
*
|
|
|
|
* The given function must return a list of DI bindings.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* beforeEachBindings(() => [
|
|
|
|
* bind(Compiler).toClass(MockCompiler),
|
|
|
|
* bind(SomeToken).toValue(myValue),
|
|
|
|
* ]);
|
|
|
|
*/
|
2015-10-11 01:11:13 -04:00
|
|
|
void beforeEachProviders(Function fn) {
|
2015-05-08 22:51:19 -04:00
|
|
|
gns.beforeEach(() {
|
|
|
|
var bindings = fn();
|
|
|
|
if (bindings != null) _testBindings.addAll(bindings);
|
|
|
|
}, priority: 2);
|
2014-09-30 14:56:33 -04:00
|
|
|
}
|
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
@Deprecated('using beforeEachProviders instead')
|
|
|
|
void beforeEachBindings(Function fn) {
|
|
|
|
beforeEachProviders(fn);
|
|
|
|
}
|
|
|
|
|
2015-10-08 18:33:17 -04:00
|
|
|
void beforeEach(fn) {
|
|
|
|
if (fn is! FunctionWithParamTokens) fn = new FunctionWithParamTokens([], fn, false);
|
|
|
|
gns.beforeEach(() {
|
|
|
|
fn.execute(_injector);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-05 15:59:54 -04:00
|
|
|
void _it(gnsFn, name, fn) {
|
2015-10-08 18:33:17 -04:00
|
|
|
if (fn is! FunctionWithParamTokens) fn = new FunctionWithParamTokens([], fn, false);
|
2015-03-13 06:10:11 -04:00
|
|
|
gnsFn(name, () {
|
|
|
|
_inIt = true;
|
|
|
|
fn.execute(_injector);
|
|
|
|
_inIt = false;
|
|
|
|
if (_isCurrentTestAsync) return _injector.get(AsyncTestCompleter).future;
|
|
|
|
});
|
2014-11-20 15:07:48 -05:00
|
|
|
}
|
|
|
|
|
2015-06-02 10:29:09 -04:00
|
|
|
void it(name, fn, [timeOut = null]) {
|
2015-03-13 06:10:11 -04:00
|
|
|
_it(gns.it, name, fn);
|
|
|
|
}
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-06-02 10:29:09 -04:00
|
|
|
void iit(name, fn, [timeOut = null]) {
|
2015-03-13 06:10:11 -04:00
|
|
|
_it(gns.iit, name, fn);
|
|
|
|
}
|
2014-09-30 14:56:33 -04:00
|
|
|
|
2015-06-02 10:29:09 -04:00
|
|
|
void xit(name, fn, [timeOut = null]) {
|
2015-03-13 06:10:11 -04:00
|
|
|
_it(gns.xit, name, fn);
|
2015-02-17 17:30:08 -05:00
|
|
|
}
|
|
|
|
|
2015-04-17 19:08:59 -04:00
|
|
|
class SpyFunction extends gns.SpyFunction {
|
2015-05-08 22:51:19 -04:00
|
|
|
SpyFunction(String name) : super(name);
|
2015-04-17 19:08:59 -04:00
|
|
|
|
|
|
|
// TODO: vsavkin move to guinness
|
|
|
|
andReturn(value) {
|
|
|
|
return andCallFake(([a0, a1, a2, a3, a4, a5]) => value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-14 21:01:44 -04:00
|
|
|
class SpyObject extends gns.SpyObject {
|
2015-04-17 19:08:59 -04:00
|
|
|
final Map<String, SpyFunction> _spyFuncs = {};
|
|
|
|
|
2015-06-18 18:40:12 -04:00
|
|
|
SpyObject([arg]) {}
|
2015-04-17 19:08:59 -04:00
|
|
|
|
|
|
|
SpyFunction spy(String funcName) =>
|
2015-05-08 22:51:19 -04:00
|
|
|
_spyFuncs.putIfAbsent(funcName, () => new SpyFunction(funcName));
|
2015-04-17 19:08:59 -04:00
|
|
|
|
2015-08-26 14:41:41 -04:00
|
|
|
void prop(String funcName, value) {
|
|
|
|
_spyFuncs.putIfAbsent("get:${funcName}", () => new SpyFunction(funcName)).andReturn(value);
|
|
|
|
}
|
|
|
|
|
2015-04-17 19:08:59 -04:00
|
|
|
static stub([object = null, config = null, overrides = null]) {
|
|
|
|
if (object is! SpyObject) {
|
|
|
|
overrides = config;
|
|
|
|
config = object;
|
|
|
|
object = new SpyObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = StringMapWrapper.merge(config, overrides);
|
2015-05-08 22:51:19 -04:00
|
|
|
StringMapWrapper.forEach(m, (value, key) {
|
2015-04-17 19:08:59 -04:00
|
|
|
object.spy(key).andReturn(value);
|
|
|
|
});
|
|
|
|
return object;
|
2015-04-14 21:01:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:59 -04:00
|
|
|
bool isInInnerZone() => Zone.current['_innerZone'] == true;
|