|
package ; |
|
|
|
import haxe.unit.TestRunner; |
|
import haxe.unit.TestCase; |
|
|
|
/** |
|
* @author Porfirio |
|
*/ |
|
class Test extends TestRunner |
|
{ |
|
|
|
public function new() { |
|
super(); |
|
add(new TestNull()); |
|
add(new TestNatives()); |
|
add(new TestDynamicClass()); |
|
} |
|
|
|
static function main() { |
|
new Test().run(); |
|
} |
|
|
|
} |
|
|
|
enum ETest { |
|
A; |
|
B; |
|
C(a:Int); |
|
} |
|
|
|
interface IBase { |
|
} |
|
|
|
class Base implements IBase { |
|
public static var b:Base; |
|
public function new() { } |
|
public static function __init__() { |
|
b = new Base(); |
|
} |
|
} |
|
class Sub extends Base { |
|
public function new() { super(); } |
|
} |
|
|
|
class TestNull extends TestCase { |
|
var null1 = null; |
|
public function testNatives() { |
|
assertFalse(TypeCheck.is(null, Int)); |
|
assertFalse(TypeCheck.is(null, Float)); |
|
assertFalse(TypeCheck.is(null, Bool)); |
|
assertFalse(TypeCheck.is(null, String)); |
|
assertFalse(TypeCheck.is(null, Class)); |
|
assertFalse(TypeCheck.is(null, Enum)); |
|
assertTrue(TypeCheck.is(null, Dynamic));//This check is always true |
|
assertFalse(TypeCheck.is(null1, Int)); |
|
assertFalse(TypeCheck.is(null1, Float)); |
|
assertFalse(TypeCheck.is(null1, Bool)); |
|
assertFalse(TypeCheck.is(null1, String)); |
|
assertFalse(TypeCheck.is(null1, Class)); |
|
assertFalse(TypeCheck.is(null1, Enum)); |
|
assertTrue(TypeCheck.is(null1, Dynamic));//This check is always true |
|
} |
|
public function testInterface() { |
|
assertFalse(TypeCheck.is(null, IBase)); |
|
assertFalse(TypeCheck.is(null1, IBase)); |
|
} |
|
public function testClass() { |
|
assertFalse(TypeCheck.is(null, Base)); |
|
assertFalse(TypeCheck.is(null1, Base)); |
|
} |
|
public function testUnum() { |
|
assertFalse(TypeCheck.is(null, ETest)); |
|
assertFalse(TypeCheck.is(null1, ETest)); |
|
} |
|
} |
|
|
|
class TestNatives extends TestCase { |
|
|
|
public function testNumbers() { |
|
assertTrue(TypeCheck.is(1, Int)); |
|
assertTrue(TypeCheck.is(1, Float)); |
|
//assertTrue(TypeCheck.is(1.1, Int));//Compile time error: Float should be Int |
|
assertTrue(TypeCheck.is(1.1, Float)); |
|
var i = 1, f = 1.1; |
|
assertTrue(TypeCheck.is(i, Int)); |
|
assertTrue(TypeCheck.is(i, Float)); |
|
//assertTrue(TypeCheck.is(f, Int));//Compile time error: Float should be Int |
|
assertTrue(TypeCheck.is(f, Float)); |
|
|
|
var f2:Dynamic; |
|
f2 = 1; |
|
assertTrue(TypeCheck.is(f2, Int)); |
|
assertTrue(TypeCheck.is(f2, Float)); |
|
f2 = 1.1; |
|
assertFalse(TypeCheck.is(f2, Int)); |
|
assertTrue(TypeCheck.is(f2, Float)); |
|
} |
|
public function testBool() { |
|
assertTrue(TypeCheck.is(true, Bool)); |
|
assertTrue(TypeCheck.is(false, Bool)); |
|
assertFalse(TypeCheck.is(1, Bool)); |
|
var b = false; |
|
assertTrue(TypeCheck.is(b, Bool)); |
|
} |
|
public function testString() { |
|
assertTrue(TypeCheck.is("str", String)); |
|
var str = ""; |
|
assertTrue(TypeCheck.is(str, String)); |
|
assertFalse(TypeCheck.is(null, String)); |
|
assertFalse(TypeCheck.is(1, String)); |
|
assertFalse(TypeCheck.is(true, String)); |
|
} |
|
public function testClass() { |
|
assertTrue(TypeCheck.is(Base, Class)); |
|
assertTrue(TypeCheck.is(IBase, Class)); |
|
assertTrue(TypeCheck.is(Int, Class)); |
|
var c = String; |
|
assertTrue(TypeCheck.is(c, Class)); |
|
} |
|
public function testEnum() { |
|
assertTrue(TypeCheck.is(ETest, Enum)); |
|
var e = ETest; |
|
assertTrue(TypeCheck.is(e, Enum)); |
|
} |
|
public function testDynamic() { |
|
assertTrue(TypeCheck.is(null, Dynamic));//Testing against dynamic always give true no matter tyhe value! |
|
} |
|
} |
|
|
|
class TestDynamicClass extends TestCase { |
|
//In this test what matters is the code generated more than the result... |
|
public function testClass() { |
|
var c:Class<Dynamic>; |
|
c = String; |
|
assertTrue(TypeCheck.is("", c)); |
|
var c:Class<IBase>=null; |
|
var s:Dynamic = null; |
|
assertFalse(TypeCheck.is(s, c)); |
|
s = new Sub(); |
|
c = IBase; |
|
assertTrue(TypeCheck.is(s, c)); |
|
c = Base; |
|
assertTrue(TypeCheck.is(s, c)); |
|
c = Sub; |
|
assertTrue(TypeCheck.is(s, c)); |
|
} |
|
public function testEnum() { |
|
var e:Enum<Dynamic>=null; |
|
var v:ETest=null; |
|
assertFalse(TypeCheck.is(v, e)); |
|
e = ETest; |
|
v = ETest.A; |
|
assertTrue(TypeCheck.is(v, e)); |
|
} |
|
} |