Skip to content

Commit

Permalink
moved unittests to utest library (#679)
Browse files Browse the repository at this point in the history
* moved unittests to utest library
  • Loading branch information
AlexHaxe authored Mar 5, 2024
1 parent 897f5d1 commit 1f9026c
Show file tree
Hide file tree
Showing 33 changed files with 250 additions and 158 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/formatter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
build:
strategy:
matrix:
haxe-version: ['4.3.3', 'nightly']
haxe-version: ['4.3.4', 'nightly']
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down Expand Up @@ -50,11 +50,12 @@ jobs:
run: npx haxe buildJava.hxml
- name: Build JSON schema
run: npx haxe buildSchema.hxml
- name: Run eval tests
if: (matrix.haxe-version == '4.3.3')
run: npx haxe -D codecov_json test.hxml
- name: Run eval tests (without coverage)
run: npx haxe test.hxml
- name: Run eval tests (with coverage)
run: npx haxe testCoverage.hxml
- name: Run Java tests
run: npx haxe -D codecov_json testJava.hxml
run: npx haxe testJava.hxml
- name: Run Node version with --check
run: node run.js -s . --check
- name: Run Java version with --check
Expand All @@ -73,5 +74,5 @@ jobs:
# name: formatter.js
# path: bin/formatter.js
- name: Upload results to codecov
if: success() && matrix.platform == 'ubuntu-latest' && (matrix.haxe-version == '4.3.3')
if: success() && matrix.platform == 'ubuntu-latest' && (matrix.haxe-version == '4.3.4')
run: bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"
2 changes: 1 addition & 1 deletion .haxerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "4.3.3",
"version": "4.3.4",
"resolveLibs": "scoped"
}
5 changes: 1 addition & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@
"schema",
"test"
],
"haxe.importsSortOrder": "stdlib -> libs -> project",
"coverage-gutters.showLineCoverage": false,
"coverage-gutters.showRulerCoverage": false,
"coverage-gutters.partialHighlightDark": "rgba(120, 120, 120, 0.75)"
"haxe.importsSortOrder": "stdlib -> libs -> project"
}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## dev branch / next version (1.x.x)

- Refactored unittests to work with utest library ([#679](https://github.com/HaxeCheckstyle/haxe-formatter/issues/679))
- Removed munit library ([#679](https://github.com/HaxeCheckstyle/haxe-formatter/issues/679))

## version 1.16.0 (2024-02-10)

- Added `wrapping.mapWrap`, fixes [#675](https://github.com/HaxeCheckstyle/haxe-formatter/issues/675) ([#677](https://github.com/HaxeCheckstyle/haxe-formatter/issues/677))
Expand Down
1 change: 1 addition & 0 deletions buildCommon.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

-dce full
-D analyzer-optimize
-D haxe-next

-cp src

Expand Down
5 changes: 2 additions & 3 deletions display.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
-lib hxparse
-lib json2object
-lib hxargs
-lib mcover
-lib munit
-lib safety
-lib test-adapter
-lib instrument
-lib utest
-lib test-adapter
3 changes: 0 additions & 3 deletions haxe_libraries/mconsole.hxml

This file was deleted.

3 changes: 0 additions & 3 deletions haxe_libraries/mcover.hxml

This file was deleted.

4 changes: 0 additions & 4 deletions haxe_libraries/mlib.hxml

This file was deleted.

4 changes: 0 additions & 4 deletions haxe_libraries/munit.hxml

This file was deleted.

6 changes: 3 additions & 3 deletions haxe_libraries/test-adapter.hxml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# @install: lix --silent download "haxelib:/test-adapter#2.0.6" into test-adapter/2.0.6/haxelib
# @install: lix --silent download "haxelib:/test-adapter#2.0.7" into test-adapter/2.0.7/haxelib
-lib json2object
-cp ${HAXE_LIBCACHE}/test-adapter/2.0.6/haxelib/
-D test-adapter=2.0.6
-cp ${HAXE_LIBCACHE}/test-adapter/2.0.7/haxelib/
-D test-adapter=2.0.7
--macro _testadapter.Macro.init()
4 changes: 4 additions & 0 deletions haxe_libraries/utest.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# @install: lix --silent download "gh://github.com/haxe-utest/utest#bdb5fec4b8e77d9a3c079d9cfb108f29f153721a" into utest/2.0.0-alpha/github/bdb5fec4b8e77d9a3c079d9cfb108f29f153721a
-cp ${HAXE_LIBCACHE}/utest/2.0.0-alpha/github/bdb5fec4b8e77d9a3c079d9cfb108f29f153721a/src
-D utest=2.0.0-alpha
--macro utest.utils.Macro.importEnvSettings()
9 changes: 5 additions & 4 deletions test.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
-lib hxparse
-lib json2object
-lib hxargs
-lib mcover
-lib munit
# -lib safety
-lib utest
-lib test-adapter

# -D debugLog
# -D debugIndent
# -D detailed_coverage
# -D debugWrapping
-D unittest
-D haxe-next
-x TestMain

--macro mcover.MCover.coverage(['formatter'], ['src'], ['formatter.Cli'])
-D UTEST_PRINT_TESTS

# -D dump=pretty
7 changes: 5 additions & 2 deletions test/GoldBaseTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import haxe.Template;
import sys.io.File;
import formatter.Formatter;
import formatter.config.Config;
import massive.munit.Assert;
import tokentree.TokenTreeBuilder.TokenTreeEntryPoint;

class GoldBaseTest {
var entryPoint:TokenTreeEntryPoint = null;

public function new() {}

function goldCheck(fileName:String, unformatted:String, goldCode:String, lineSeparator:String, ?configString:String, ?pos:PosInfos) {
var config = new Config();
config.readConfigFromString(configString, "goldhxformat.json");
Expand Down Expand Up @@ -41,14 +42,16 @@ class GoldBaseTest {
if (goldCode != formattedCode) {
File.saveContent("test/formatter-result.txt", '$goldCode\n---\n$formattedCode');
}
Assert.areEqual(goldCode, formattedCode, pos);
Assert.equals(goldCode, formattedCode, pos);
case Failure(errorMessage):
if (isFailing) {
Assert.isTrue(isFailing, "testcase is expected to fail");
return;
}
Assert.fail(errorMessage, pos);
case Disabled:
if (isDisabled) {
Assert.isTrue(isDisabled, "testcase is disabled");
return;
}
Assert.fail("Formatting is disabled", pos);
Expand Down
16 changes: 6 additions & 10 deletions test/SelfTest.hx
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
import haxe.PosInfos;
import haxe.io.Bytes;
import haxe.io.Path;
import massive.munit.Assert;
import formatter.Formatter;
import formatter.codedata.ParseFile;
import sys.io.File;
import sys.FileSystem;
import sys.io.File;
import formatter.Formatter;

class SelfTest implements ITest {
public function new() {}

class SelfTest {
@Test
public function testSelfSrc() {
var files:Array<String> = collectAllFileNames("src");
for (file in files) {
checkFile(file);
}
}

@Test
public function testSelfTest() {
var files:Array<String> = collectAllFileNames("test");
for (file in files) {
checkFile(file);
}
}

@Test
public function testSelfSchema() {
var files:Array<String> = collectAllFileNames("schema");
for (file in files) {
Expand All @@ -40,7 +36,7 @@ class SelfTest {
if (code != formattedCode) {
File.saveContent("test/formatter-result.txt", '$code\n---\n$formattedCode');
}
Assert.areEqual(code, formattedCode, 'Format failed for $fileName', pos);
Assert.equals(code, formattedCode, 'Format failed for $fileName', pos);
case Failure(errorMessage):
Assert.fail(errorMessage);
case Disabled:
Expand Down
37 changes: 34 additions & 3 deletions test/TestCaseMacro.hx
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import haxe.io.Path;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.PositionTools;
import haxe.macro.Type.ClassType;
import sys.FileSystem;
import sys.io.File;

using haxe.macro.ExprTools;

class TestCaseMacro {
#if macro
public macro static function build(folder:String):Array<Field> {
var fields:Array<Field> = Context.getBuildFields();
var cls:ClassType = Context.getLocalClass().get();

if (!shouldApply(cls, ":testcases")) {
return fields;
}
cls.meta.add(":testcases", [], cls.pos);

// revert the effects of implementing ITest interface
//
// unfortunately utest initialises before this macro runs, making our testcases invisible to utest.
// additionally test-adapter needs ITest interface to be able to collect position info of testcases
// which means we have to implement utests ITest interface to keep test explorer working.
//
// solution:
// we remove the field and meta info added by @:autoBuild (through ITest), add our testcases and
// then re-apply utest's test builder macro a second time
fields = fields.filter(f -> f.name != "__initializeUtest__");
cls.meta.remove(":utestProcessed");

var testCases:Array<String> = collectAllFileNames(folder);
var singleRun:TestSingleRun = new TestSingleRun();
for (testCase in testCases) {
Expand Down Expand Up @@ -52,14 +68,29 @@ class TestCaseMacro {
var unformatted:String = segments[1];
var gold:String = segments[2];
var fieldName:String = new haxe.io.Path(fileName).file;
fieldName = 'test_$fieldName';
var lineSeparator:String = detectLineSeparator(content);

return (macro class {
@Test
var field = (macro class {
public function $fieldName() {
goldCheck($v{fileName}, $v{unformatted}, $v{gold}, $v{lineSeparator}, $v{config});
};
}).fields[0];

// make assertion failures show up in testfile
field.pos = PositionTools.make({file: fileName, min: content.length, max: content.length});
switch (field.kind) {
case FFun(f):
relocateExpr(f.expr, field.pos);
default:
}

return field;
}

static function relocateExpr(expr:Expr, pos:Position):Expr {
expr.pos = pos;
return expr.map((e) -> relocateExpr(e, pos));
}

static function collectAllFileNames(path:String):Array<String> {
Expand Down
83 changes: 54 additions & 29 deletions test/TestMain.hx
Original file line number Diff line number Diff line change
@@ -1,42 +1,67 @@
import haxe.EntryPoint;
import sys.io.File;
import massive.munit.TestRunner;
import mcover.coverage.MCoverage;
import mcover.coverage.munit.client.MCoverPrintClient;
#if codecov_json
import mcover.coverage.client.CodecovJsonPrintClient;
#else
import mcover.coverage.client.LcovPrintClient;
#end
import formatter.EmptyLinesTest;
import formatter.FormatStatsTest;
import formatter.codedata.TokenListTest;
import testcases.EmptyLinesTestCases;
import testcases.ExpressionLevelTestCases;
import testcases.FormatRangeTestCases;
import testcases.IndentationTestCases;
import testcases.LineEndsTestCases;
import testcases.MissingTestCases;
import testcases.Other;
import testcases.SameLineTestCases;
import testcases.WhitespaceTestCases;
import testcases.WrappingTestCases;
import utest.Runner;
import utest.ui.text.DiagnosticsReport;

class TestMain {
public function new() {
var suites:Array<Class<massive.munit.TestSuite>> = [TestSuite];
var client:MCoverPrintClient = new MCoverPrintClient();
#if codecov_json
MCoverage.getLogger().addClient(new CodecovJsonPrintClient());
#else
MCoverage.getLogger().addClient(new LcovPrintClient("Formatter Unittests"));
#end
var tests:Array<() -> ITest> = [];

#if detailed_coverage
client.includeClassAndPackageBreakdowns = true;
client.includeMissingBlocks = true;
#end
var runner = new TestRunner(client);
runner.completionHandler = completionHandler;
#if (neko || cpp || hl || java)
EntryPoint.addThread(function() {
while (true) {
Sys.sleep(1.0);
var singleRun:TestSingleRun = new TestSingleRun();
if (!singleRun.isSingleRun()) {
tests.push(SelfTest.new);
tests.push(FormatStatsTest.new);
tests.push(TokenListTest.new);
tests.push(EmptyLinesTest.new);
}

tests.push(cast EmptyLinesTestCases.new);
tests.push(cast ExpressionLevelTestCases.new);
tests.push(cast FormatRangeTestCases.new);
tests.push(cast IndentationTestCases.new);
tests.push(cast LineEndsTestCases.new);
tests.push(cast MissingTestCases.new);
tests.push(cast Other.new);
tests.push(cast SameLineTestCases.new);
tests.push(cast WhitespaceTestCases.new);
tests.push(cast WrappingTestCases.new);

var runner:Runner = new Runner();

var failed = false;
runner.onProgress.add(r -> {
if (!r.result.allOk()) {
failed = true;
}
});
#end
runner.run(suites);
EntryPoint.run();
runner.onComplete.add(_ -> {
completionHandler(!failed);
});

new DiagnosticsReport(runner);
for (test in tests) {
runner.addCase(test());
}
runner.run();
}

function completionHandler(success:Bool) {
#if instrument
instrument.coverage.Coverage.endCoverage();
#end

if (success) {
File.saveContent("test/formatter-result.txt", "\n---\n");
}
Expand Down
Loading

0 comments on commit 1f9026c

Please sign in to comment.