Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests] avoid codebase mixup in display tests #11959

Merged
merged 9 commits into from
Feb 3, 2025
3 changes: 2 additions & 1 deletion tests/display/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dump
dump
test/cases
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package misc;
package;

class PublicClass {}
private class PrivateClass {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package misc.issue10704;
package issue10704;

class Statics {
public static final fooPublic = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package misc.issue11620;
package issue11620;

class Foo {
public static function foo() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package misc.issue7098;
package issue7098;

enum abstract Foo(Int) {
var Value = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package misc.issue7777;
package issue7777;

#if (eval || macro)
import haxe.macro.Expr;
Expand Down
5 changes: 5 additions & 0 deletions tests/display/src-misc/issue7777/Thing.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package issue7777;

enum Thing {
BOO;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package misc.issue7877;
package issue7877;

import haxe.macro.Expr;
import haxe.macro.Context;
Expand Down
9 changes: 9 additions & 0 deletions tests/display/src-misc/issue7877/ProcessedClass.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package issue7877;

@:build(issue7877.ProcessMacro.build()) class ProcessedClass {
final foo:Bool; // = false;

function bar() {
trace(foo);
}
}
2 changes: 2 additions & 0 deletions tests/display/src-misc/issue7911/Test.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package issue7911;

2 changes: 2 additions & 0 deletions tests/display/src-misc/issue7911/import.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package issue7911;

60 changes: 60 additions & 0 deletions tests/display/src-shared/Vfs.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package;

import haxe.io.Path;
import sys.FileSystem;
import sys.io.File;

using DateTools;

class Vfs {
var physicalPath:String;

public function new(physicalPath:String) {
this.physicalPath = physicalPath;
if (!FileSystem.exists(physicalPath)) {
FileSystem.createDirectory(physicalPath);
}
}

public function overwriteContent(path:String, content:String) {
var path = getPhysicalPath(path).toString();
if (!FileSystem.exists(path)) {
throw 'Cannot overwrite content for $path: file does not exist';
}
File.saveContent(path, content);
}

public function putContent(path:String, content:String) {
var path = getPhysicalPath(path);
FileSystem.createDirectory(path.dir);
File.saveContent(path.toString(), content);
}

public function getContent(path:String):String {
var path = getPhysicalPath(path);
FileSystem.createDirectory(path.dir);
return File.getContent(path.toString());
}

public function close() {
removeDir(physicalPath);
}

public function getPhysicalPath(path:String) {
return new Path(Path.join([physicalPath, path]));
}

static public function removeDir(dir:String):Void {
if (FileSystem.exists(dir)) {
for (item in FileSystem.readDirectory(dir)) {
item = haxe.io.Path.join([dir, item]);
if (FileSystem.isDirectory(item)) {
removeDir(item);
} else {
FileSystem.deleteFile(item);
}
}
FileSystem.deleteDirectory(dir);
}
}
}
22 changes: 18 additions & 4 deletions tests/display/src/BaseDisplayTestContext.hx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import haxe.io.Bytes;
import haxe.io.Path;

using StringTools;

Expand All @@ -7,12 +8,20 @@ import Types;
class BaseDisplayTestContext {
static var haxeServer = haxeserver.HaxeServerSync.launch("haxe", []);

var dir:String = ".";
var vfs:Vfs;
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>) {
var test = new Path(path).file;
this.dir = '${Sys.getCwd()}/test/cases/${test}';
this.vfs = new Vfs(dir);
var path = Path.withoutDirectory(path);
vfs.putContent(path, source);

this.fieldName = fieldName;
this.source = new File(path, source);
this.markers = markers;
Expand All @@ -38,13 +47,18 @@ class BaseDisplayTestContext {
}
}

static public function runHaxe(args:Array<String>, ?stdin:String) {
return haxeServer.rawRequest(["-D", "message.reporting=classic"].concat(args), stdin == null ? null : Bytes.ofString(stdin));
public function runHaxe(args:Array<String>, ?stdin:String) {
return haxeServer.rawRequest([
"--cwd", dir,
"-cp", '${Sys.getCwd()}/src-misc',
"-D", "message.reporting=classic",
"--no-output"
].concat(args), stdin == null ? null : Bytes.ofString(stdin));
}

static function normalizePath(p:String):String {
function normalizePath(p:String):String {
if (!haxe.io.Path.isAbsolute(p)) {
p = Sys.getCwd() + p;
p = vfs.getPhysicalPath(p).toString();
}
if (Sys.systemName() == "Windows") {
// on windows, haxe returns paths with backslashes, drive letter uppercased
Expand Down
14 changes: 9 additions & 5 deletions tests/display/src/Macro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class Macro {
if (field.doc == null) {
continue;
}
var doc = (c.pack.length > 0 ? "package " + c.pack.join(".") + ";\n" : "");
if (field.meta.exists(function(meta) return meta.name == ":funcCode")) {
doc += "class Main { static function main() { " + field.doc + "}}";

var doc = if (field.meta.exists(function(meta) return meta.name == ":funcCode")) {
"class Main { static function main() { " + field.doc + "}}";
} else {
doc += field.doc;
}
field.doc;
};

doc = StringTools.replace(doc, "**\\/", "**/");
var transform = Marker.extractMarkers(doc);
var markers = transform.markers.length > 0 ? macro $a{transform.markers} : macro new Map();
Expand All @@ -40,6 +41,9 @@ class Macro {
case FFun(f) if (f.expr != null):
f.expr = macro @:pos(f.expr.pos) {
ctx = new DisplayTestContext($v{filename}, $v{field.name}, $v{transform.source}, $markers);
static var methodArgs = {method: haxe.display.Protocol.Methods.ResetCache, id: 1, params: {}};
var args = ['--display', haxe.Json.stringify(methodArgs)];
ctx.runHaxe(args);
${f.expr}
};
case _:
Expand Down
10 changes: 7 additions & 3 deletions tests/display/src/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ class Main {
report.displaySuccessResults = NeverShowSuccessResults;

var haxeServer = @:privateAccess BaseDisplayTestContext.haxeServer;
haxeServer.setDefaultRequestArguments(["-cp", "src", "-cp", "src-shared", "--no-output"]);
BaseDisplayTestContext.runHaxe([]);

report.setHandler((report) -> {
Sys.println(report.getResults());
haxeServer.close();
});

Vfs.removeDir('${Sys.getCwd()}/test/cases');
runner.run();
haxeServer.close();
}
}
7 changes: 0 additions & 7 deletions tests/display/src/RpcDisplayTestCase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ class RpcDisplayTestCase implements utest.ITest {

public function new() {}

@:timeout(3000)
public function setup() {
static var methodArgs = {method: Methods.ResetCache, id: 1, params: {}};
var args = ['--display', Json.stringify(methodArgs)];
BaseDisplayTestContext.runHaxe(args);
}

// api
inline function pos(name)
return ctx.pos(name);
Expand Down
4 changes: 2 additions & 2 deletions tests/display/src/RpcDisplayTestContext.hx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class RpcDisplayTestContext extends BaseDisplayTestContext {
// Can be removed once module-symbols is migrated to json rpc
function callHaxe(displayPart:String) {
var args = ["--display", source.path + "@" + displayPart];
var result = BaseDisplayTestContext.runHaxe(args, source.content);
var result = runHaxe(args, source.content);
if (result.hasError || result.stderr == "") {
throw new HaxeInvocationException(result.stderr, fieldName, args, source.content);
}
Expand All @@ -111,7 +111,7 @@ class RpcDisplayTestContext extends BaseDisplayTestContext {
var methodArgs = {method: method, id: 1, params: methodArgs};
var args = ['--display', Json.stringify(methodArgs)];

var result = BaseDisplayTestContext.runHaxe(args, source.content);
var result = runHaxe(args, source.content);
if (result.hasError || result.stderr == "") {
throw new HaxeInvocationException(result.stderr, fieldName, args, source.content);
}
Expand Down
7 changes: 0 additions & 7 deletions tests/display/src/XmlDisplayTestCase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ class XmlDisplayTestCase implements utest.ITest {

public function new() {}

@:timeout(3000)
public function setup() {
static var methodArgs = {method: haxe.display.Protocol.Methods.ResetCache, id: 1, params: {}};
var args = ['--display', haxe.Json.stringify(methodArgs)];
BaseDisplayTestContext.runHaxe(args);
}

// api
inline function pos(name)
return ctx.pos(name);
Expand Down
6 changes: 2 additions & 4 deletions tests/display/src/XmlDisplayTestContext.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import haxe.Json;
import haxe.display.Display;
import haxe.display.Protocol;

import BaseDisplayTestContext.normalizePath;

using StringTools;

import Types;
Expand Down Expand Up @@ -60,7 +58,7 @@ class XmlDisplayTestContext extends BaseDisplayTestContext {

function callHaxe(displayPart:String) {
var args = ["--display", source.path + "@" + displayPart];
var result = BaseDisplayTestContext.runHaxe(args, source.content);
var result = runHaxe(args, source.content);
if (result.hasError || result.stderr == "") {
throw new HaxeInvocationException(result.stderr, fieldName, args, source.content);
}
Expand All @@ -76,7 +74,7 @@ class XmlDisplayTestContext extends BaseDisplayTestContext {
return StringTools.trim(xml.firstChild().nodeValue);
}

static function extractPositions(result:String) {
function extractPositions(result:String) {
var xml = Xml.parse(result);
xml = xml.firstElement();
if (xml.nodeName != "list") {
Expand Down
6 changes: 3 additions & 3 deletions tests/display/src/cases/Abstract.hx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class Abstract extends DisplayTestCase {
final fields = toplevel(pos(2));
eq(false, hasToplevel(fields, "literal", "abstract"));
// TODO: improve display hints
eq("cases.MyAbstract", type(pos(3)));
eq("cases.AbGeneric<cases.AbGeneric.T>", type(pos(4)));
eq("cases.AbGeneric.T", type(pos(5)));
eq("MyAbstract", type(pos(3)));
eq("AbGeneric<AbGeneric.T>", type(pos(4)));
eq("AbGeneric.T", type(pos(5)));
}
}
2 changes: 1 addition & 1 deletion tests/display/src/cases/Basic.hx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ class Basic extends DisplayTestCase {
}
**/
function testCtorClosureType() {
eq("(someName : Int) -> cases.Some", type(pos(1)));
eq("(someName : Int) -> Some", type(pos(1)));
}
}
8 changes: 4 additions & 4 deletions tests/display/src/cases/BuildMacro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BuildMacro extends DisplayTestCase {
typedef {-7-}MyString{-8-} = String;

#if !macro
@:build(cases.BuildMacro.MyMacro.build())
@:build(BuildMacro.MyMacro.build())
#end
class Main {
function te{-1-}st({-5-}na{-2-}me{-6-}:MySt{-3-}ring):MyStr{-4-}ing {
Expand All @@ -23,9 +23,9 @@ class BuildMacro extends DisplayTestCase {
}
**/
function test1() {
eq("cases.MyString", type(pos(2)));
eq("cases.MyString", type(pos(3)));
eq("cases.MyString", type(pos(4)));
eq("MyString", type(pos(2)));
eq("MyString", type(pos(3)));
eq("MyString", type(pos(4)));
eq(range(7, 8), position(pos(3)));
eq(range(7, 8), position(pos(4)));
eq(range(5, 6), position(pos(2)));
Expand Down
6 changes: 3 additions & 3 deletions tests/display/src/cases/Completion.hx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Completion extends DisplayTestCase {
**/
@:funcCode function testHaxeUnitPort4() {
eq(true, hasPath(fields(pos(1)), "Expr"));
BaseDisplayTestContext.runHaxe(['haxe.macro.Expr']);
ctx.runHaxe(['haxe.macro.Expr']);
eq(true, hasPath(fields(pos(1)), "Expr"));
}

Expand All @@ -46,7 +46,7 @@ class Completion extends DisplayTestCase {
**/
@:funcCode function testHaxeUnitPort5() {
eq(true, hasPath(fields(pos(1)), "ExprDef"));
BaseDisplayTestContext.runHaxe(['haxe.macro.Expr']);
ctx.runHaxe(['haxe.macro.Expr']);
eq(true, hasPath(fields(pos(1)), "ExprDef"));
}

Expand All @@ -55,7 +55,7 @@ class Completion extends DisplayTestCase {
**/
@:funcCode function testStaticField() {
eq(true, hasPath(fields(pos(1)), "stringify"));
BaseDisplayTestContext.runHaxe(['haxe.Json']);
ctx.runHaxe(['haxe.Json']);
eq(true, hasPath(fields(pos(1)), "stringify"));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/display/src/cases/DocumentSymbols.hx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class DocumentSymbols extends DisplayTestCase {

function checkDocumentSymbols(expected:Array<ModuleSymbolEntry>, actual:Array<ModuleSymbolEntry>, ?pos:haxe.PosInfos) {
for (entry in expected) {
entry.containerName = "cases.DocumentSymbols" + if (entry.containerName == null) {
entry.containerName = "DocumentSymbols" + if (entry.containerName == null) {
"";
} else {
"." + entry.containerName;
Expand Down
Loading
Loading