Browse Source

[tests] add tests for the status quo of argument nullability

see #6972
Simon Krajewski 7 years ago
parent
commit
6118ea37b9
2 changed files with 73 additions and 10 deletions
  1. 30 10
      tests/unit/src/unit/HelperMacros.hx
  2. 43 0
      tests/unit/src/unit/issues/Issue6972.hx

+ 30 - 10
tests/unit/src/unit/HelperMacros.hx

@@ -1,6 +1,8 @@
 package unit;
 
 import haxe.macro.Expr;
+import haxe.macro.Context.*;
+import haxe.macro.TypeTools.*;
 
 class HelperMacros {
 	static public macro function getCompilationDate() {
@@ -8,32 +10,50 @@ class HelperMacros {
 	}
 
 	static public macro function typeString(e) {
-		var typed = haxe.macro.Context.typeExpr(e);
+		var typed = typeExpr(e);
 		var s = haxe.macro.TypeTools.toString(typed.t);
 		return macro $v{s};
 	}
 
 	static public macro function typedAs(actual:haxe.macro.Expr, expected:haxe.macro.Expr) {
-		var tExpected = haxe.macro.Context.typeof(expected);
-		var tActual = haxe.macro.Context.typeof(actual);
-		return haxe.macro.Context.parse("eq('" +Std.string(tActual) + "', '" +Std.string(tExpected) + "')", haxe.macro.Context.currentPos());
+		var tExpected = typeof(expected);
+		var tActual = typeof(actual);
+		return parse("eq('" +Std.string(tActual) + "', '" +Std.string(tExpected) + "')", currentPos());
+	}
+
+	static public macro function isNullable(expr:haxe.macro.Expr) {
+		var t = typeof(expr);
+		function isNullable(t:haxe.macro.Type) {
+			return switch (t) {
+				case TMono(null): false;
+				case TMono(t): isNullable(t.get());
+				case TAbstract(_.get() => {pack: [], name: "Null"}, _): true;
+				case TLazy(f): isNullable(f());
+				case TType(_.get() => td, tl): isNullable(applyTypeParameters(td.type, td.params, tl));
+				case TFun(_): false;
+				case TAbstract(_.get() => a, _) if (a.meta.has(":coreType")): !a.meta.has(":notNull");
+				case TAbstract(_.get() => a, tl): !a.meta.has(":notNull") && isNullable(applyTypeParameters(a.type, a.params, tl));
+				case _: true;
+			}
+		}
+		return macro $v{isNullable(t)};
 	}
 
 	static public macro function typeError(e:haxe.macro.Expr) {
 		var result = try {
-			haxe.macro.Context.typeof(e);
+			typeof(e);
 			"false";
 		} catch (e:Dynamic) "true";
-		return { pos: haxe.macro.Context.currentPos(), expr: haxe.macro.Expr.ExprDef.EConst(haxe.macro.Expr.Constant.CIdent(result)) };
+		return { pos: currentPos(), expr: haxe.macro.Expr.ExprDef.EConst(haxe.macro.Expr.Constant.CIdent(result)) };
 	}
 
 	static public macro function typeErrorText(e:haxe.macro.Expr) {
 		var result = try {
-			haxe.macro.Context.typeof(e);
+			typeof(e);
 			null;
 		} catch (e:haxe.macro.Expr.Error) e.message;
 		return {
-			pos: haxe.macro.Context.currentPos(),
+			pos: currentPos(),
 			expr: if (result == null)
 					haxe.macro.Expr.ExprDef.EConst(haxe.macro.Expr.Constant.CIdent("null"))
 				else
@@ -52,14 +72,14 @@ class HelperMacros {
 
 	static public macro function getErrorMessage(e:Expr) {
 		var result = try {
-			haxe.macro.Context.typeof(e);
+			typeof(e);
 			"no error";
 		} catch (e:Dynamic) Std.string(e.message);
 		return macro $v{result};
 	}
 
 	static public macro function parseAndPrint(s:String) {
-		var e = haxe.macro.Context.parse(s, haxe.macro.Context.currentPos());
+		var e = parse(s, currentPos());
 		var s2 = new haxe.macro.Printer().printExpr(e);
 		return macro eq($v{s}, $v{s2});
 	}

+ 43 - 0
tests/unit/src/unit/issues/Issue6972.hx

@@ -0,0 +1,43 @@
+package unit.issues;
+
+import unit.HelperMacros.isNullable as n;
+
+class Issue6972 extends unit.Test {
+	function test() {
+		f1(1);
+		f2(1);
+		f3();
+		f4();
+		f5();
+		f6();
+		f7();
+		f8();
+
+		f11();
+		f12();
+		f13();
+		f14();
+		f15();
+		f16();
+		f17();
+		f18();
+	}
+
+	function f1(x:Int) f(n(x));
+	function f2(x:Null<Int>) t(n(x));
+	function f3(x = null) t(n(x));
+	function f4(x = 1) f(n(x));
+	function f5(x:Int = null) t(n(x));
+	function f6(x:Int = 1) f(n(x));
+	function f7(x:Null<Int> = 1) t(n(x));
+	function f8(x:Null<Int> = null) t(n(x));
+
+	function f11(?x:Int) t(n(x));
+	function f12(?x:Null<Int>) t(n(x));
+	function f13(?x = null) t(n(x));
+	function f14(?x = 1) t(n(x));
+	function f15(?x:Int = null) t(n(x));
+	function f16(?x:Int = 1) t(n(x));
+	function f17(?x:Null<Int> = 1) t(n(x));
+	function f18(?x:Null<Int> = null) t(n(x));
+}