chore(test): migrate Dart tests to package:test

Instead of running with karma and the karma-dart shim, run dart
tests directly using the new package:test runner. This migrates
away from package:unittest.

Fixes a couple tests, mostly associated with depending on absolute
URLs or editing the test providers after an injector had already
been created.

Remove karma-dart and associated files. Change gupfiles to run tests
via `pub run test` instead.
This commit is contained in:
Julie Ralph
2016-02-10 16:54:32 -08:00
parent 7455b907d1
commit 5a59e44765
39 changed files with 532 additions and 511 deletions

View File

@ -2,7 +2,7 @@ library testing.matchers;
import 'dart:async';
import 'package:guinness/guinness.dart' as gns;
import 'package:guinness2/guinness2.dart' as gns;
import 'package:angular2/src/platform/dom/dom_adapter.dart' show DOM;

View File

@ -1,92 +1,30 @@
library angular2.src.testing.testing_internal;
import 'dart:async';
import 'package:guinness/guinness.dart' as gns;
export 'package:guinness/guinness.dart'
import 'testing_internal_core.dart' as core;
export 'testing_internal_core.dart'
hide
Expect,
expect,
NotExpect,
beforeEachProviders,
beforeEachBindings,
beforeEach,
it,
iit,
xit,
SpyObject,
SpyFunction;
testSetup,
describe,
ddescribe,
xdescribe;
export 'matchers.dart' show expect, Expect, NotExpect;
import 'package:angular2/platform/testing/browser.dart';
import 'package:angular2/src/core/reflection/reflection.dart';
import 'package:angular2/src/core/reflection/reflection_capabilities.dart';
import 'package:angular2/src/core/di/provider.dart' show bind;
import 'package:angular2/src/facade/collection.dart' show StringMapWrapper;
import 'test_injector.dart';
export 'test_injector.dart' show inject;
TestInjector _testInjector = getTestInjector();
bool _isCurrentTestAsync;
Future _currentTestFuture;
bool _inIt = false;
class AsyncTestCompleter {
final _completer = new Completer();
AsyncTestCompleter() {
_currentTestFuture = this.future;
}
void done() {
_completer.complete();
}
Future get future => _completer.future;
}
void testSetup() {
reflector.reflectionCapabilities = new ReflectionCapabilities();
// beforeEach configuration:
// - Priority 3: clear the bindings before each test,
// - Priority 2: collect the bindings before each test, see beforeEachProviders(),
// - Priority 1: create the test injector to be used in beforeEach() and it()
gns.beforeEach(() {
_testInjector.reset();
_currentTestFuture = null;
}, priority: 3);
var completerProvider = 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();
});
gns.beforeEach(() {
_isCurrentTestAsync = false;
_testInjector.addProviders([completerProvider]);
}, priority: 1);
core.setDartBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS);
}
/**
* Allows overriding default providers defined in test_injector.js.
*
* The given function must return a list of DI providers.
*
* Example:
*
* beforeEachProviders(() => [
* bind(Compiler).toClass(MockCompiler),
* bind(SomeToken).toValue(myValue),
* ]);
*/
void beforeEachProviders(Function fn) {
gns.beforeEach(() {
var providers = fn();
if (providers != null) _testInjector.addProviders(providers);
}, priority: 2);
testSetup();
core.beforeEachProviders(fn);
}
@Deprecated('using beforeEachProviders instead')
@ -95,72 +33,33 @@ void beforeEachBindings(Function fn) {
}
void beforeEach(fn) {
if (fn is! FunctionWithParamTokens) fn =
new FunctionWithParamTokens([], fn, false);
gns.beforeEach(() {
_testInjector.execute(fn);
});
}
void _it(gnsFn, name, fn) {
if (fn is! FunctionWithParamTokens) fn =
new FunctionWithParamTokens([], fn, false);
gnsFn(name, () {
_inIt = true;
_testInjector.execute(fn);
_inIt = false;
if (_isCurrentTestAsync) return _currentTestFuture;
});
testSetup();
core.beforeEach(fn);
}
void it(name, fn, [timeOut = null]) {
_it(gns.it, name, fn);
core.it(name, fn, timeOut);
}
void iit(name, fn, [timeOut = null]) {
_it(gns.iit, name, fn);
core.iit(name, fn, timeOut);
}
void xit(name, fn, [timeOut = null]) {
_it(gns.xit, name, fn);
core.xit(name, fn, timeOut);
}
class SpyFunction extends gns.SpyFunction {
SpyFunction(String name) : super(name);
// TODO: vsavkin move to guinness
andReturn(value) {
return andCallFake(([a0, a1, a2, a3, a4, a5]) => value);
}
void describe(name, fn) {
testSetup();
core.describe(name, fn);
}
class SpyObject extends gns.SpyObject {
final Map<String, SpyFunction> _spyFuncs = {};
SpyObject([arg]) {}
SpyFunction spy(String funcName) =>
_spyFuncs.putIfAbsent(funcName, () => new SpyFunction(funcName));
void prop(String funcName, value) {
_spyFuncs
.putIfAbsent("get:${funcName}", () => new SpyFunction(funcName))
.andReturn(value);
}
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);
StringMapWrapper.forEach(m, (value, key) {
object.spy(key).andReturn(value);
});
return object;
}
void ddescribe(name, fn) {
testSetup();
core.ddescribe(name, fn);
}
bool isInInnerZone() => Zone.current['_innerZone'] == true;
void xdescribe(name, fn) {
testSetup();
core.xdescribe(name, fn);
}

View File

@ -0,0 +1,200 @@
library angular2.src.testing.testing_internal_core;
import 'dart:async';
import 'package:guinness2/guinness2.dart' as gns;
export 'package:guinness2/guinness2.dart'
hide
Expect,
expect,
NotExpect,
beforeEach,
it,
iit,
xit,
describe,
ddescribe,
xdescribe,
SpyObject,
SpyFunction;
export 'matchers.dart' show expect, Expect, NotExpect;
import 'package:angular2/src/core/reflection/reflection.dart';
import 'package:angular2/src/core/reflection/reflection_capabilities.dart';
import 'package:angular2/src/core/di/provider.dart' show bind;
import 'package:angular2/src/facade/collection.dart' show StringMapWrapper;
import 'test_injector.dart';
export 'test_injector.dart' show inject;
TestInjector _testInjector = getTestInjector();
bool _isCurrentTestAsync;
Future _currentTestFuture;
bool _inIt = false;
bool _initialized = false;
List<dynamic> _platformProviders = [];
List<dynamic> _applicationProviders = [];
class AsyncTestCompleter {
final _completer = new Completer();
AsyncTestCompleter() {
_currentTestFuture = this.future;
}
void done() {
_completer.complete();
}
Future get future => _completer.future;
}
void setDartBaseTestProviders(List<dynamic> platform, List<dynamic> application) {
_platformProviders = platform;
_applicationProviders = application;
}
void testSetup() {
if (_initialized) {
return;
}
_initialized = true;
reflector.reflectionCapabilities = new ReflectionCapabilities();
setBaseTestProviders(_platformProviders, _applicationProviders);
// beforeEach configuration:
// - clear the bindings before each test,
// - collect the bindings before each test, see beforeEachProviders(),
// - create the test injector to be used in beforeEach() and it()
gns.beforeEach(() {
_testInjector.reset();
_currentTestFuture = null;
});
var completerProvider = 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();
});
gns.beforeEach(() {
_isCurrentTestAsync = false;
_testInjector.addProviders([completerProvider]);
});
}
/**
* Allows overriding default providers defined in test_injector.js.
*
* The given function must return a list of DI providers.
*
* Example:
*
* beforeEachProviders(() => [
* bind(Compiler).toClass(MockCompiler),
* bind(SomeToken).toValue(myValue),
* ]);
*/
void beforeEachProviders(Function fn) {
testSetup();
gns.beforeEach(() {
var providers = fn();
if (providers != null) _testInjector.addProviders(providers);
});
}
@Deprecated('using beforeEachProviders instead')
void beforeEachBindings(Function fn) {
beforeEachProviders(fn);
}
void beforeEach(fn) {
testSetup();
if (fn is! FunctionWithParamTokens) fn =
new FunctionWithParamTokens([], fn, false);
gns.beforeEach(() {
_testInjector.execute(fn);
});
}
void _it(gnsFn, name, fn) {
testSetup();
if (fn is! FunctionWithParamTokens) fn =
new FunctionWithParamTokens([], fn, false);
gnsFn(name, () {
_inIt = true;
_testInjector.execute(fn);
_inIt = false;
if (_isCurrentTestAsync) return _currentTestFuture;
});
}
void it(name, fn, [timeOut = null]) {
_it(gns.it, name, fn);
}
void iit(name, fn, [timeOut = null]) {
_it(gns.iit, name, fn);
}
void xit(name, fn, [timeOut = null]) {
_it(gns.xit, name, fn);
}
void describe(name, fn) {
testSetup();
gns.describe(name, fn);
}
void ddescribe(name, fn) {
testSetup();
gns.ddescribe(name, fn);
}
void xdescribe(name, fn) {
testSetup();
gns.xdescribe(name, fn);
}
class SpyFunction extends gns.SpyFunction {
SpyFunction(String name) : super(name);
// TODO: vsavkin move to guinness
andReturn(value) {
return andCallFake(([a0, a1, a2, a3, a4, a5]) => value);
}
}
class SpyObject extends gns.SpyObject {
final Map<String, SpyFunction> _spyFuncs = {};
SpyObject([arg]) {}
SpyFunction spy(String funcName) =>
_spyFuncs.putIfAbsent(funcName, () => new SpyFunction(funcName));
void prop(String funcName, value) {
_spyFuncs
.putIfAbsent("get:${funcName}", () => new SpyFunction(funcName))
.andReturn(value);
}
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);
StringMapWrapper.forEach(m, (value, key) {
object.spy(key).andReturn(value);
});
return object;
}
}
bool isInInnerZone() => Zone.current['_innerZone'] == true;