浏览代码

add some tests

Simon Krajewski 12 年之前
父节点
当前提交
a16b22429d
共有 4 个文件被更改,包括 36 次插入8 次删除
  1. 1 2
      matcher.ml
  2. 9 3
      std/haxe/macro/Build.hx
  3. 11 3
      tests/unit/MyAbstract.hx
  4. 15 0
      tests/unit/TestMatch.hx

+ 1 - 2
matcher.ml

@@ -737,9 +737,8 @@ let rec all_ctors mctx t =
 		List.iter (fun cf ->
 			ignore(follow cf.cf_type);
 			if not (Meta.has Meta.Impl cf.cf_meta) then match cf.cf_expr with
-				| None -> ()
 				| Some {eexpr = TConst c | TCast ({eexpr = TConst c},None)} -> h := PMap.add (CConst c) cf.cf_pos !h
-				| _ -> assert false
+				| _ -> ()
 		) c.cl_ordered_statics;
 		h,false
 	| TAbstract(a,pl) -> all_ctors mctx (Codegen.Abstract.get_underlying_type a pl)

+ 9 - 3
std/haxe/macro/Build.hx

@@ -30,14 +30,20 @@ using haxe.macro.Tools;
 class Build {
 	macro static public function buildFakeEnum():Array<Field> {
 		var fields = Context.getBuildFields();
-		var ct = switch(Context.getLocalClass().get().kind) {
-			case KAbstractImpl(a): TAbstract(a, []).toComplexType();
+		var a = switch(Context.getLocalClass().get().kind) {
+			case KAbstractImpl(a): a;
 			case _: throw "";
 		}
+		var tThis = a.get().type;
+		var ctA = TAbstract(a, []).toComplexType();
 		for (field in fields) {
 			field.access = [AStatic,APublic,AInline];
 			switch(field.kind) {
-				case FVar(t, e): field.kind = FVar(ct, macro cast $e);
+				case FVar(t, e):
+					if (e == null) Context.error("Value required", field.pos);
+					var tE = Context.typeof(e);
+					if (!Context.unify(tE, tThis)) Context.error('${tE.toString()} should be ${tThis.toString()}', e.pos);
+					field.kind = FVar(ctA, macro cast $e);
 				case _:
 			}
 		}

+ 11 - 3
tests/unit/MyAbstract.hx

@@ -297,7 +297,7 @@ abstract MyDebugString(String) to String {
 }
 
 @:multiType abstract MySpecialString(String) {
-	public function new(value:String);	
+	public function new(value:String);
 
 	public inline function substr(i:Int, ?len:Int) {
 		return len == null ? this.substr(i) : this.substr(i, len);
@@ -305,5 +305,13 @@ abstract MyDebugString(String) to String {
 	
 	@:to static inline public function toNormal(t:String, value:String) {
 		return new MyDebugString(value);
-	}	
-}
+	}
+}
+
+#if !macro
+@:fakeEnum
+abstract FakeEnumAbstract(Int) {
+	var NotFound = 404;
+	var MethodNotAllowed = 405;
+}
+#end

+ 15 - 0
tests/unit/TestMatch.hx

@@ -427,6 +427,21 @@ class TestMatch extends Test {
 		eq(f({a: 1}), 1);
 		eq(f({a: 2}), 3);
 	}
+	
+	function testFakeEnumAbstract() {
+		#if !macro
+		var a = unit.MyAbstract.FakeEnumAbstract.NotFound;
+		var r = switch(a) {
+			case unit.MyAbstract.FakeEnumAbstract.NotFound: 1;
+			case _: 2;
+		}
+		eq(r, 1);
+		
+		eq("Unmatched patterns: 405", getErrorMessage(switch(a) {
+			case unit.MyAbstract.FakeEnumAbstract.NotFound:
+		}));
+		#end
+	}
 
 	#if false
 	 //all lines marked as // unused should give an error