New facade. Rudimentary tests.

This commit is contained in:
back2dos
2013-09-25 14:32:19 +02:00
parent 4054939a7c
commit cd15e4b00c
15 changed files with 330 additions and 349 deletions

5
tests/Arrayish.hx Normal file
View File

@@ -0,0 +1,5 @@
package ;
class Arrayish implements ArrayAccess<Int> {
public var length:Int;
}

View File

@@ -13,8 +13,8 @@ abstract PhysicalType<T>(Either<Class<T>, Enum<T>>) {
public function toString()
return
switch this {
case Left(c): return Type.getClassName(c);
case Right(e): return Type.getEnumName(e);
case Left(c): Type.getClassName(c);
case Right(e): Type.getEnumName(e);
}
public function check(v:T)
@@ -37,10 +37,10 @@ class Base extends TestCase {
currentTest.posInfos = c;
throw currentTest;
}
function throws<A>(f:Void->Void, t:PhysicalType<A>, ?check:A->Bool, ?pos:PosInfos):Void {
function throws<A>(f:Void->Void, ?t:PhysicalType<A>, ?check:A->Bool, ?pos:PosInfos):Void {
try f()
catch (e:Dynamic) {
if (!t.check(e)) fail('Exception $e not of type $t', pos);
if (t != null && !t.check(e)) fail('Exception $e not of type $t', pos);
if (check != null && !check(e)) fail('Exception $e does not satisfy condition', pos);
assertTrue(true);
return;

70
tests/Exprs.hx Normal file
View File

@@ -0,0 +1,70 @@
package ;
import haxe.macro.Expr;
using tink.Macro;
class Exprs extends Base {
function exprEq(e1:Expr, e2:Expr) {
assertEquals(e1.toString(), e2.toString());
}
function testGet() {
assertEquals('foo', (macro foo).getIdent().sure());
assertEquals('foo', (macro "foo").getString().sure());
assertEquals('foo', (macro foo).getName().sure());
assertEquals('foo', (macro "foo").getName().sure());
assertEquals(5, (macro 5).getInt().sure());
exprEq(macro [a, b, c], (macro function (a, b, c) [a, b, c]).getFunction().sure().expr);
assertEquals('a,b,c', [for (arg in (macro function (a, b, c) [a, b, c]).getFunction().sure().args) arg.name].join(','));
assertFalse((macro 'foo').getIdent().isSuccess());
assertFalse((macro foo).getString().isSuccess());
assertFalse((macro 5).getName().isSuccess());
assertFalse((macro 5.1).getInt().isSuccess());
assertFalse((macro foo).getFunction().isSuccess());
}
function testShortcuts() {
assertTrue(true);
}
function testIterType() {
assertEquals('Int', (macro [1, 2]).getIterType().sure().getID());
assertEquals('Int', (macro [1, 2].iterator()).getIterType().sure().getID());
assertEquals('Int', ECheckType(macro null, macro: Arrayish).at().getIterType().sure().getID());
}
function testYield() {
function yielder(e) return macro @yield $e;
function test(x:Expr, e:Expr)
exprEq(x, e.yield(yielder));
test(macro @yield foo, macro foo);
test(macro @yield (foo), macro (foo));
test(macro for (_) @yield foo, macro for (_) foo);
test(macro while (_) @yield foo, macro while (_) foo);
test(macro @yield [while (_) foo], macro [while (_) foo]);
}
function testSubstitute() {
exprEq(
macro foo.call(arg1, arg2),
(macro bar.call(x, y)).substitute({ x: macro arg1, y: macro arg2, bar: macro foo })
);
exprEq(
macro {
var x:Map<Int, String> = new Map(),
y:Array<Float> = [];
},
(macro {
var x:Map<A, B> = new Map(),
y:C = [];
}).substParams([
'A' => macro : Int,
'B' => macro : String,
'C' => macro : Array<Float>
])
);
}
}

23
tests/Positions.hx Normal file
View File

@@ -0,0 +1,23 @@
package ;
import haxe.macro.Context;
import haxe.macro.Expr;
using tink.Macro;
class Positions extends Base {
function stringCompare<A>(v1:A, v2:A)
assertEquals(Std.string(v1), Std.string(v2));
function testSanitize() {
var p:Position = null;
stringCompare(Context.currentPos(), p.sanitize());
p = Context.makePosition({ min: 0, max: 10, file: 'foo.txt' });
stringCompare(p, p);
}
function testBlank() {
var p:Position = null;
var t = p.makeBlankType();
stringCompare('TMono(<mono>)', cast t.toType().sure());
}
}

View File

@@ -1,24 +1,28 @@
package ;
#if !macro
import haxe.unit.TestCase;
import haxe.unit.TestRunner;
import neko.Lib;
#else
import tink.macro.Member;
import tink.macro.Constructor;
import tink.macro.ClassBuilder;
using tink.macro.Tools;
#end
import haxe.unit.*;
class Run {
#if !macro
static var tests:Array<TestCase> = [];
static function main() {
test();//it compiles!!!
}
static function main()
test();//It compiles ...
#else
static var cases:Array<TestCase> = [
new Exprs(),
new Types(),
new Positions(),
];
#end
macro static function test() {
return macro null;
var runner = new TestRunner();
for (c in cases)
runner.add(c);
runner.run();
if (!runner.result.success)
haxe.macro.Context.error(runner.result.toString(), haxe.macro.Context.currentPos());
return macro {
trace('Let\'s ship it!');
}
}
}

46
tests/Types.hx Normal file
View File

@@ -0,0 +1,46 @@
package ;
import haxe.macro.Expr;
import haxe.macro.Context;
using tink.Macro;
class Types extends Base {
function type(c:ComplexType)
return c.toType().sure();
function resolve(type:String)
return Context.getType(type);
inline function assertSuccess<S, F>(o:Outcome<S, F>)
assertTrue(o.isSuccess());
inline function assertFailure<S, F>(o:Outcome<S, F>)
assertFalse(o.isSuccess());
function testIs() {
assertSuccess(resolve('Int').isSubTypeOf(resolve('Float')));
assertFailure(resolve('Float').isSubTypeOf(resolve('Int')));
}
function testFields() {
var iterator = type(macro : haxe.ds.StringMap<Arrayish>).getFields(true).sure().filter(function (c) return c.name == 'iterator')[0];
var expected = type(macro : Void -> Iterator<Arrayish>);
assertSuccess(iterator.type.isSubTypeOf(expected));
assertSuccess(expected.isSubTypeOf(iterator.type));
}
function testConvert() {
assertSuccess((macro : Int).toType());
assertFailure((macro : Tni).toType());
function blank()
return type(Macro.pos().makeBlankType());
var bool = type(macro : Bool);
assertTrue(blank().isSubTypeOf(bool).isSuccess());
assertTrue(bool.isSubTypeOf(blank()).isSuccess());
Macro.pos().makeBlankType().toString();
}
}