refactor: simplify and make tests work in JS and Dart

* remove `wraps` syntax enhancements for imports
  and support new `import * as module from ...` syntax

  - default imports are the wrong construct for importing
    everything from a module

* moved tests from transpiler to jasmine and karma

  - transpiler tests are included when running karma in main project folder
  - transpiler is reloaded after every test run in karma,
    so no need to restart karma when the transpiler has been changed.
  - removed own gulp build for transpiler and `postinstall.sh`
    as they are no more needed.
  - transpiler tests are now executed in Dart AND JavaScript (used to be executed
    only in Dart), which allowed to catch some bugs (see the bug with the
    import specification above).

* made tests work in dart as well by using the following hack:

  - dependencies are loaded from the `build` folder, which makes
    running `gulp build` necessary before running karma for dart
  - for this to work,
    the dependencies are included in main `pubspec.yaml` of project
  - reason for the hack: `karma-dart` loads all `packages` urls
    directly from disc (should rather use the karma file list)

* added explicit annotations `FIELD`, `ABSTRACT`, ... to `facade/lang.*`

  - needed for now that we can run tests and don't get errors for undefined
    annotations.

* added `README.md` with details about the build and tests
This commit is contained in:
Tobias Bosch
2014-09-28 13:55:01 -07:00
parent 817c005845
commit c79f0c3472
47 changed files with 338 additions and 508 deletions

View File

@ -1,19 +1,26 @@
import annotations from './fixtures/annotations';
import {describe, it, expect} from 'test_lib/test_lib';
import {Provide, readFirstAnnotation} from './fixtures/annotations';
class Inject {}
class Bar {}
@annotations.Provide('Foo')
@Provide('Foo')
class Foo {
@Inject
constructor() {}
}
@annotations.Provide(Foo)
@Provide(Foo)
function baz() {}
function annotatedParams(@Inject(Foo) f, @Inject(Bar) b) {}
function main() {
annotations.main();
export function main() {
describe('annotations', function() {
it('should work', function() {
// Assert `Foo` class has `Provide` annotation.
var clazz = readFirstAnnotation(Foo);
expect(clazz instanceof Provide).toBe(true);
});
});
}

View File

@ -1,3 +1,5 @@
import {describe, it, expect} from 'test_lib/test_lib';
// Constructor
// Define fields
class Foo {
@ -11,10 +13,14 @@ class Foo {
}
}
function main() {
var foo = new Foo(2, 3);
export function main() {
describe('classes', function() {
it('should work', function() {
var foo = new Foo(2, 3);
assert(foo.a == 2);
assert(foo.b == 3);
assert(foo.sum() == 5);
expect(foo.a).toBe(2);
expect(foo.b).toBe(3);
expect(foo.sum()).toBe(5);
});
});
}

View File

@ -1,3 +1,5 @@
import {describe, it, expect} from 'test_lib/test_lib';
function same(a, b) {
return a === b;
}
@ -7,10 +9,14 @@ function notSame(a, b) {
return a !== b;
}
function main() {
var obj = {};
assert(same({}, {}) == false);
assert(same(obj, obj) == true);
assert(notSame({}, {}) == true);
assert(notSame(obj, obj) == false);
export function main() {
describe('equals', function() {
it('should work', function() {
var obj = {};
expect(same({}, {}) == false).toBe(true);
expect(same(obj, obj) == true).toBe(true);
expect(notSame({}, {}) == true).toBe(true);
expect(notSame(obj, obj) == false).toBe(true);
});
});
}

View File

@ -1,26 +1,18 @@
// This file is not generated,
import 'dart:mirrors';
// This class is not generated,
// but should be in the future.
//
// Problems:
// - Dart requires annotations to be const (which makes sense).
// Right now, I can't describe that in ES6.
// - operator mapping `is`/`instanceof` is not yet implemented
import 'dart:mirrors';
import '../annotations_spec.dart';
class Provide {
final token;
const Provide(this.token);
}
readAnnotation(clazz) {
// TODO: this api does not yet return an array as we don't have
// a nice array wrapper for Dart
readFirstAnnotation(clazz) {
return reflectClass(clazz).metadata.first.reflectee;
}
main() {
// Assert `Foo` class has `Provide` annotation.
// TODO(vojta): test this more.
var clazz = readAnnotation(Foo);
assert(clazz is Provide);
}

View File

@ -0,0 +1,18 @@
// This class is not generated,
// but should be in the future.
//
// Problems:
// - Dart requires annotations to be const (which makes sense).
// Right now, I can't describe that in ES6.
export class Provide {
constructor(token) {
this.token = token;
}
}
// TODO: this api does not yet return an array as we don't have
// a nice array wrapper for Dart
export function readFirstAnnotation(clazz) {
return clazz.annotations[0];
}

View File

@ -0,0 +1,3 @@
export class MapWrapper {
}

View File

@ -1,7 +1,13 @@
import {describe, it, expect} from 'test_lib/test_lib';
function sum(a, b) {
return a + b;
}
function main() {
assert(sum(1, 2) == 3);
export function main() {
describe('functions', function() {
it('should work', function() {
expect(sum(1, 2)).toBe(3);
});
});
}

View File

@ -1,22 +1,28 @@
import {describe, it, expect} from 'test_lib/test_lib';
import {Foo, Bar} from './foo';
// TODO: Does not work, as dart does not support renaming imports
// import {Foo as F} from './fixtures/foo';
import fooModule from './foo';
import * as fooModule from './foo';
import {MapWrapper wraps Map} from './fixtures/facade';
import * as exportModule from './export';
import exportModule from './export';
import {Type} from 'facade/lang';
import unittest from 'unittest/unittest';
export function main() {
describe('imports', function() {
it('should work', function() {
expect(Foo).toBe('FOO');
expect(Bar).toBe('BAR');
// TODO: Does not work
// assert(F == 'FOO');
expect(fooModule.Foo).toBe('FOO');
expect(fooModule.Bar).toBe('BAR');
function main() {
assert(Foo == 'FOO');
assert(Bar == 'BAR');
// assert(F == 'FOO');
assert(fooModule.Foo == 'FOO');
assert(fooModule.Bar == 'BAR');
expect(exportModule.Foo).toBe('FOO');
expect(exportModule.Bar).toBe('BAR');
assert(exportModule.Foo == 'FOO');
assert(exportModule.Bar == 'BAR');
assert(unittest.PASS != null);
expect(Type).toBeTruthy();
});
});
}

View File

@ -1,13 +0,0 @@
import 'package:unittest/unittest.dart';
<% files.forEach(function(file, index) { %>
import '<%= file %>' as spec<%=index%>;
<% }); %>
void main() {
<% files.forEach(function(file, index) { %>
test('<%= file %>', () {
spec<%= index %>.main();
});
<% }); %>
}

View File

@ -1,3 +1,8 @@
import {describe, it, expect} from 'test_lib/test_lib';
class A {}
class B {}
function sum(a: number, b: number): number {
return a + b;
}
@ -6,7 +11,7 @@ function not(a: boolean): boolean {
return !a;
}
function generics(a: A<Test>) {
function generics(a: A<B>) {
}
@ -37,17 +42,21 @@ class Foo {
typedVariables() {
// TODO(vojta): test this
var foo:string = 'foo';
var typed:bool, untyped;
var typed:boolean, untyped;
var oneTyped:string = 'one',
another: bool = true;
another: boolean = true;
}
}
function main() {
// TODO(vojta): test this better.
var f = new Foo(1, 2);
assert(f.sum() == 3);
assert(f instanceof Foo);
export function main() {
describe('types', function() {
it('should work', function() {
// TODO(vojta): test this better.
var f = new Foo(1, 2);
assert(f.sum() == 3);
assert(f instanceof Foo);
f.typedVariables();
f.typedVariables();
});
});
}