Przeglądaj źródła

[display] add a test, closes #7932

Jens Fischer 6 lat temu
rodzic
commit
e412e075db

+ 9 - 0
tests/display/src/DisplayTestCase.hx

@@ -1,3 +1,4 @@
+import haxe.display.Position.Range;
 import utest.Assert;
 import Types;
 
@@ -60,6 +61,14 @@ class DisplayTestCase implements utest.ITest {
 		return a.exists(function(t) return t.name == name);
 	}
 
+	function diagnosticsRange(start:Position, end:Position):Range {
+		var range = ctx.source.findRange(start, end);
+		// this is probably correct...?
+		range.start.character--;
+		range.end.character--;
+		return range;
+	}
+
 	function sigEq(arg:Int, params:Array<Array<String>>, sig:SignatureHelp, ?pos:haxe.PosInfos) {
 		eq(arg, sig.activeParameter, pos);
 		eq(params.length, sig.signatures.length, pos);

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

@@ -21,10 +21,11 @@ class HaxeInvocationException {
 }
 
 class DisplayTestContext {
-	var source:File;
 	var markers:Map<Int,Int>;
 	var fieldName:String;
 
+	public final source:File;
+
 	public function new(path:String, fieldName:String, source:String, markers:Map<Int,Int>) {
 		this.fieldName = fieldName;
 		this.source = new File(path, source);
@@ -38,7 +39,7 @@ class DisplayTestContext {
 	}
 
 	public function range(pos1:Int, pos2:Int) {
-		return normalizePath(source.formatPosition(pos(pos1), pos(pos2)));
+		return normalizePath(source.formatRange(pos(pos1), pos(pos2)));
 	}
 
 	public function fields(pos:Position):Array<FieldElement> {
@@ -81,7 +82,7 @@ class DisplayTestContext {
 		return extractMetadata(callHaxe('$pos@type'));
 	}
 
-	public function diagnostics():Array<Diagnostic<Any>> {
+	public function diagnostics():Array<Diagnostic<Dynamic>> {
 		var result = haxe.Json.parse(callHaxe('0@diagnostics'))[0];
 		return if (result == null) [] else result.diagnostics;
 	}

+ 18 - 8
tests/display/src/File.hx

@@ -1,3 +1,5 @@
+import haxe.display.Position;
+
 class File {
 	public var content(default,null):String;
 	public var path(default,null):String;
@@ -26,13 +28,13 @@ class File {
 		}
 	}
 
-	function findLine(pos:Int):{line:Int, pos:Int} {
+	function findPosition(pos:Int):Position {
 		function loop(min, max) {
 			var mid = (min + max) >> 1;
 			var start = lines[mid];
 			return
 				if (mid == min)
-					{line: mid, pos: pos - start + 1};
+					{line: mid, character: pos - start + 1};
 				else if (start > pos)
 					loop(min, mid);
 				else
@@ -41,15 +43,23 @@ class File {
 		return loop(0, lines.length);
 	}
 
-	public function formatPosition(min:Int, max:Int):String {
-		var start = findLine(min);
-		var end = findLine(max);
+	public function findRange(min:Int, max:Int):Range {
+		return {
+			start: findPosition(min),
+			end: findPosition(max)
+		}
+	}
+
+	public function formatRange(min:Int, max:Int):String {
+		var range = findRange(min, max);
+		var start = range.start;
+		var end = range.end;
 		var pos =
 			if (start.line == end.line) {
-				if (start.pos == end.pos)
-					'character ${start.pos}';
+				if (start.character == end.character)
+					'character ${start.character}';
 				else
-					'characters ${start.pos}-${end.pos}';
+					'characters ${start.character}-${end.character}';
 			} else {
 				'lines ${start.line + 1}-${end.line + 1}';
 			}

+ 4 - 4
tests/display/src/cases/Issue7877.hx

@@ -3,10 +3,10 @@ package cases;
 class Issue7877 extends DisplayTestCase {
 	/**
 	class Main {
-	    public static function main() {
-	        new misc.issue7877.ProcessedClass(false);
-	        new misc.issue7877.ProcessedClass(true);
-	    }
+		public static function main() {
+			new misc.issue7877.ProcessedClass(false);
+			new misc.issue7877.ProcessedClass(true);
+		}
 	}
 	**/
 	function test() {

+ 17 - 0
tests/display/src/cases/Issue7932.hx

@@ -0,0 +1,17 @@
+package cases;
+
+class Issue7932 extends DisplayTestCase {
+	/**
+	class Main< {-1-}{{-2-}
+		public static function main() {}
+	}
+	**/
+	function test() {
+		arrayEq([{
+			kind: DKCompilerError,
+			range: diagnosticsRange(pos(1), pos(2)),
+			severity: Error,
+			args: "Expected type parameter"
+		}], diagnostics());
+	}
+}

+ 1 - 0
tests/display/src/import.hx

@@ -0,0 +1 @@
+import Diagnostic;