Просмотр исходного кода

[display] add a test for #7877

(or at least the part of it that's fixed)
Jens Fischer 6 лет назад
Родитель
Сommit
b20cb70290

+ 1 - 1
tests/display/build.hxml

@@ -2,4 +2,4 @@
 --main Main
 --interp
 -D use-rtti-doc
-# -D test=Issue7326
+# -D test=Issue7877

+ 29 - 0
tests/display/src/Diagnostic.hx

@@ -0,0 +1,29 @@
+// from vshaxe
+
+import haxe.display.Position.Range;
+
+enum abstract UnresolvedIdentifierSuggestion(Int) {
+	var UISImport;
+	var UISTypo;
+}
+
+enum abstract DiagnosticKind<T>(Int) from Int to Int {
+	var DKUnusedImport:DiagnosticKind<Void>;
+	var DKUnresolvedIdentifier:DiagnosticKind<Array<{kind:UnresolvedIdentifierSuggestion, name:String}>>;
+	var DKCompilerError:DiagnosticKind<String>;
+	var DKRemovableCode:DiagnosticKind<{description:String, range:Range}>;
+}
+
+enum abstract DiagnosticSeverity(Int) {
+	var Error = 1;
+	var Warning;
+	var Information;
+	var Hint;
+}
+
+typedef Diagnostic<T> = {
+	var kind:DiagnosticKind<T>;
+	var range:Range;
+	var severity:DiagnosticSeverity;
+	var args:T;
+}

+ 4 - 3
tests/display/src/DisplayTestCase.hx

@@ -20,6 +20,7 @@ class DisplayTestCase {
 	inline function range(pos1, pos2) return ctx.range(pos1, pos2);
 	inline function signature(pos1) return ctx.signature(pos1);
 	inline function metadataDoc(pos1) return ctx.metadataDoc(pos1);
+	inline function diagnostics() return ctx.diagnostics();
 
 	inline function noCompletionPoint(f) return ctx.noCompletionPoint(f);
 
@@ -35,20 +36,20 @@ class DisplayTestCase {
 		}
 	}
 
-	function arrayEq(expected:Array<String>, actual:Array<String>, ?pos:haxe.PosInfos) {
+	function arrayEq<T>(expected:Array<T>, actual:Array<T>, ?pos:haxe.PosInfos) {
 		numTests++;
 		var leftover = expected.copy();
 		for (actual in actual) {
 			if (!leftover.remove(actual)) {
 				numFailures++;
 				report("Result not part of expected Array:", pos);
-				report(actual, pos);
+				report(Std.string(actual), pos);
 			}
 		}
 		for (leftover in leftover) {
 			numFailures++;
 			report("Expected result was not part of actual Array:", pos);
-			report(leftover, pos);
+			report(Std.string(leftover), pos);
 			return;
 		}
 	}

+ 5 - 0
tests/display/src/DisplayTestContext.hx

@@ -77,6 +77,11 @@ class DisplayTestContext {
 		return extractMetadata(callHaxe('$pos@type'));
 	}
 
+	public function diagnostics():Array<Diagnostic<Any>> {
+		var result = haxe.Json.parse(callHaxe('0@diagnostics'))[0];
+		return if (result == null) [] else result.diagnostics;
+	}
+
 	public function noCompletionPoint(f:Void -> Void):Bool {
 		return try {
 			f();

+ 15 - 0
tests/display/src/cases/Issue7877.hx

@@ -0,0 +1,15 @@
+package cases;
+
+class Issue7877 extends DisplayTestCase {
+	/**
+	class Main {
+	    public static function main() {
+	        new misc.issue7877.ProcessedClass(false);
+	        new misc.issue7877.ProcessedClass(true);
+	    }
+	}
+	**/
+	function test() {
+		arrayEq([], diagnostics());
+	}
+}

+ 40 - 0
tests/display/src/misc/issue7877/ProcessMacro.hx

@@ -0,0 +1,40 @@
+package misc.issue7877;
+
+import haxe.macro.Expr;
+import haxe.macro.Context;
+
+class ProcessMacro {
+	public static macro function build():Array<Field> {
+		var fields = Context.getBuildFields();
+		var toInit = [
+			for (field in fields) {
+				switch (field) {
+					case {name: name, kind: FVar(t, e), access: [AFinal]}:
+						{name: name, type: t, def: e};
+					case _:
+						continue;
+				}
+			}
+		];
+
+		var args:Array<FunctionArg> = [];
+		var exprs = [];
+		for (init in toInit) {
+			args.push({
+				name: init.name,
+				opt: init.def != null,
+				type: init.type,
+				value: init.def
+			});
+			var n = init.name;
+			exprs.push(macro this.$n = $i{n});
+		}
+		fields.push({
+			pos: Context.currentPos(),
+			name: 'new',
+			access: [APublic],
+			kind: FFun({ret: null, args: args, expr: {pos: Context.currentPos(), expr: EBlock(exprs)}})
+		});
+		return fields;
+	}
+}

+ 9 - 0
tests/display/src/misc/issue7877/ProcessedClass.hx

@@ -0,0 +1,9 @@
+package misc.issue7877;
+
+@:build(misc.issue7877.ProcessMacro.build()) class ProcessedClass {
+	final foo:Bool; // = false;
+
+	function bar() {
+		trace(foo);
+	}
+}