Skip to content

Commit

Permalink
Rebase JUnit references fix for dev/patch (#6057)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLimeGlass authored Oct 10, 2023
1 parent 61201bd commit c34b83a
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void createTestTask(String name, String desc, String environments, int javaVersi
main = 'ch.njol.skript.test.platform.PlatformMain'
args = [
'build/test_runners',
junit ? 'src/test/skript/tests/junit' : 'src/test/skript/tests',
junit ? 'src/test/skript/junit' : 'src/test/skript/tests',
'src/test/resources/runner_data',
environments,
modifiers.contains(Modifiers.DEV_MODE),
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ public void run() {
tainted = true;
try {
getAddonInstance().loadClasses("ch.njol.skript.test.runner");
if (TestMode.JUNIT)
getAddonInstance().loadClasses("org.skriptlang.skript.test.junit.registration");
} catch (IOException e) {
Skript.exception("Failed to load testing environment.");
Bukkit.getServer().shutdown();
Expand Down Expand Up @@ -684,7 +686,6 @@ protected void afterErrors() {
TestTracker.testFailed("exception was thrown during execution");
}
if (TestMode.JUNIT) {
SkriptLogger.setVerbosity(Verbosity.DEBUG);
info("Running all JUnit tests...");
long milliseconds = 0, tests = 0, fails = 0, ignored = 0, size = 0;
try {
Expand Down Expand Up @@ -712,7 +713,7 @@ protected void afterErrors() {
// If JUnit failures are present, add them to the TestTracker.
junit.getFailures().forEach(failure -> {
String message = failure.getMessage() == null ? "" : " " + failure.getMessage();
TestTracker.testFailed("'" + test + "': " + message);
TestTracker.JUnitTestFailed(test, message);
Skript.exception(failure.getException(), "JUnit test '" + failure.getTestHeader() + " failed.");
});
SkriptJUnitTest.clearJUnitTest();
Expand All @@ -734,7 +735,7 @@ protected void afterErrors() {
// Delay server shutdown to stop the server from crashing because the current tick takes a long time due to all the tests
Bukkit.getScheduler().runTaskLater(Skript.this, () -> {
if (TestMode.JUNIT && !EffObjectives.isJUnitComplete())
TestTracker.testFailed(EffObjectives.getFailedObjectivesString());
EffObjectives.fail();

info("Collecting results to " + TestMode.RESULTS_FILE);
String results = new Gson().toJson(TestTracker.collectResults());
Expand Down Expand Up @@ -1261,7 +1262,7 @@ public static boolean isAcceptRegistrations() {
}

public static void checkAcceptRegistrations() {
if (!isAcceptRegistrations())
if (!isAcceptRegistrations() && !Skript.testing())
throw new SkriptAPIException("Registration can only be done during plugin initialization");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.test.runner;
package ch.njol.skript.conditions;

import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/ch/njol/skript/test/runner/CondRunningJUnit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.test.runner;

import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.NoDoc;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;

@Name("Check JUnit")
@Description({
"Returns true if the test runner is currently running a JUnit.",
"Useful for the EvtTestCase of JUnit exclusive syntaxes registered from within the test packages."
})
@NoDoc
public class CondRunningJUnit extends Condition {

static {
Skript.registerCondition(CondRunningJUnit.class, "running junit");
}

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
return true;
}

@Override
public boolean check(Event event) {
return TestMode.JUNIT;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "running JUnit";
}

}
18 changes: 5 additions & 13 deletions src/main/java/ch/njol/skript/test/runner/EffObjectives.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,31 +92,23 @@ public String toString(@Nullable Event event, boolean debug) {
* @return boolean true if the test passed.
*/
public static boolean isJUnitComplete() {
if (requirements.isEmpty())
return true;
if (completeness.isEmpty() && !requirements.isEmpty())
return false;
assert !completeness.isEmpty() || !requirements.isEmpty();
return completeness.equals(requirements);
}

/**
* Returns an array string containing all the objectives that the current
* JUnit test failed to accomplish in the given time.
*
* @return
* Fails the JUnit testing system if any JUnit tests did not complete their checks.
*/
public static String getFailedObjectivesString() {
StringBuilder builder = new StringBuilder();
public static void fail() {
for (String test : requirements.keySet()) {
if (!completeness.containsKey(test)) {
builder.append("JUnit test '" + test + "' didn't complete any objectives.");
TestTracker.JUnitTestFailed("JUnit test '" + test + "'", "didn't complete any objectives.");
continue;
}
List<String> failures = Lists.newArrayList(requirements.get(test));
failures.removeAll(completeness.get(test));
builder.append("JUnit test '" + test + "' failed objectives: " + Arrays.toString(failures.toArray(new String[0])));
TestTracker.JUnitTestFailed("JUnit test '" + test + "'", "failed objectives: " + Arrays.toString(failures.toArray(new String[0])));
}
return builder.toString();
}

}
4 changes: 4 additions & 0 deletions src/main/java/ch/njol/skript/test/runner/TestTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public static void testStarted(String name) {
currentTest = name;
}

public static void JUnitTestFailed(String currentTest, String msg) {
failedTests.put(currentTest, msg);
}

public static void testFailed(String msg) {
failedTests.put(currentTest, msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.test.runner;
package org.skriptlang.skript.test.junit.registration;

import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -29,6 +29,8 @@
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.test.runner.SkriptJUnitTest;
import ch.njol.skript.test.runner.TestMode;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;

Expand All @@ -38,7 +40,7 @@
public class ExprJUnitTest extends SimpleExpression<String> {

static {
if (TestMode.ENABLED)
if (TestMode.JUNIT)
Skript.registerExpression(ExprJUnitTest.class, String.class, ExpressionType.SIMPLE, "[the] [current[[ly] running]] junit test [name]");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*
* Place any and all custom syntaxes relating to the JUnit testJar in here to be exclusively ran on the test runner.
*/
@NonNullByDefault({DefaultLocation.PARAMETER, DefaultLocation.RETURN_TYPE, DefaultLocation.FIELD})
package org.skriptlang.skript.test.junit.registration;

import org.eclipse.jdt.annotation.DefaultLocation;
import org.eclipse.jdt.annotation.NonNullByDefault;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
on script load:
test "SimpleJUnitTest" when running JUnit:
# Setup our objective for this script test to complete with the JUnit test.
ensure junit test "org.skriptlang.skript.test.tests.regression.SimpleJUnitTest" completes "piggy died"

Expand Down

0 comments on commit c34b83a

Please sign in to comment.