-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ac76c2
commit e9b637b
Showing
9 changed files
with
3,894 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...ngineTester/src/main/java/org/openzen/scriptingenginetester/watcher/TestResultStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.openzen.scriptingenginetester.watcher; | ||
|
||
public enum TestResultStatus { | ||
SUCCESSFUL, ABORTED, FAILED, DISABLED; | ||
} |
103 changes: 103 additions & 0 deletions
103
...ineTester/src/main/java/org/openzen/scriptingenginetester/watcher/ZenCodeTestResults.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package org.openzen.scriptingenginetester.watcher; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonObject; | ||
|
||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.TreeMap; | ||
|
||
|
||
public class ZenCodeTestResults { | ||
|
||
public static final ZenCodeTestResults INSTANCE = new ZenCodeTestResults(); | ||
public static final String TEST_SEPARATOR = "ñ"; | ||
|
||
private final JsonObject results = new JsonObject(); | ||
|
||
private ZenCodeTestResults() {} | ||
|
||
public static ZenCodeTestResults getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
public void testSuccessful(String testName) { | ||
saveResult(testName, TestResultStatus.SUCCESSFUL); | ||
} | ||
|
||
public void testDisabled(String testName) { | ||
saveResult(testName, TestResultStatus.DISABLED); | ||
} | ||
|
||
public void testAborted(String testName) { | ||
saveResult(testName, TestResultStatus.ABORTED); | ||
} | ||
|
||
public void testFailed(String testName) { | ||
saveResult(testName, TestResultStatus.FAILED); | ||
} | ||
|
||
private void addChild(JsonObject object, String[] names, int position, TestResultStatus status) { | ||
String parentName = names[position - 1]; | ||
if (position == names.length - 1) { | ||
if (object.has(parentName)) { | ||
object.get(parentName).getAsJsonObject().addProperty(names[position], status.name()); | ||
} | ||
else { | ||
JsonObject child = new JsonObject(); | ||
child.addProperty(names[position], status.name()); | ||
object.add(parentName, child); | ||
} | ||
} | ||
else { | ||
JsonObject child = new JsonObject(); | ||
addChild(child, names, position + 1, status); | ||
if (object.has(parentName)) { | ||
JsonObject parentObject = object.getAsJsonObject(parentName); | ||
if (parentObject.has(names[position])) { | ||
parentObject.getAsJsonArray(names[position]).add(child.getAsJsonObject(names[position])); | ||
} | ||
else { | ||
JsonArray subTests = new JsonArray(); | ||
subTests.add(child.getAsJsonObject(names[position])); | ||
object.get(parentName).getAsJsonObject().add(names[position], subTests); | ||
} | ||
} | ||
else { | ||
JsonObject parentObject = new JsonObject(); | ||
JsonArray nestedTest = new JsonArray(); | ||
nestedTest.add(child.getAsJsonObject(names[position])); | ||
parentObject.add(names[position], nestedTest); | ||
object.add(parentName, parentObject); | ||
} | ||
} | ||
} | ||
|
||
private void saveResult(String name, TestResultStatus status) { | ||
String[] testNames = name.split(TEST_SEPARATOR); | ||
if (testNames.length > 1) addChild(results, testNames, 1, status); | ||
} | ||
|
||
public void saveToDisk() { | ||
Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
try { | ||
FileWriter writer = new FileWriter("results.json"); | ||
String json = gson.toJson(results); | ||
//Sort alphabetically | ||
TreeMap<String, Object> map = gson.fromJson(json, TreeMap.class); | ||
String sortedJson = gson.toJson(map); | ||
writer.write(sortedJson); | ||
writer.flush(); | ||
writer.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
|
75 changes: 75 additions & 0 deletions
75
...ineTester/src/main/java/org/openzen/scriptingenginetester/watcher/ZenCodeTestWatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.openzen.scriptingenginetester.watcher; | ||
|
||
import org.junit.jupiter.api.extension.AfterAllCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.TestWatcher; | ||
import org.junit.platform.engine.TestDescriptor; | ||
|
||
import java.util.*; | ||
|
||
public class ZenCodeTestWatcher implements TestWatcher, AfterAllCallback { | ||
|
||
private static final String JUNIT_EXECUTOR = "JUnit Jupiter"; | ||
|
||
@Override | ||
public void testSuccessful(ExtensionContext context) { | ||
ZenCodeTestResults.getInstance().testSuccessful(getTestName(context)); | ||
} | ||
|
||
@Override | ||
public void testDisabled(ExtensionContext context, Optional<String> reason) { | ||
ZenCodeTestResults.getInstance().testDisabled(getTestName(context)); | ||
} | ||
|
||
@Override | ||
public void testAborted(ExtensionContext context, Throwable cause) { | ||
|
||
ZenCodeTestResults.getInstance().testAborted(getTestName(context)); | ||
} | ||
|
||
@Override | ||
public void testFailed(ExtensionContext context, Throwable cause) { | ||
ZenCodeTestResults.getInstance().testFailed(getTestName(context)); | ||
} | ||
|
||
public static String getTestName(TestDescriptor descriptor) { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append(descriptor.getDisplayName()); | ||
while(hasValidParent(descriptor)) { | ||
TestDescriptor parent = descriptor.getParent().get(); | ||
builder.insert(0, parent.getDisplayName() + ZenCodeTestResults.TEST_SEPARATOR); | ||
descriptor = parent; | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
private static boolean hasValidParent(TestDescriptor descriptor) { | ||
return descriptor.getParent().isPresent() && !"JavaTestingEngineTest".equals(descriptor.getParent().get().getDisplayName()); | ||
} | ||
|
||
private String getTestName(ExtensionContext context) { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append(context.getDisplayName()); | ||
while (hasValidParent(context)) { | ||
ExtensionContext parent = context.getParent().get(); | ||
builder.insert(0, parent.getDisplayName() + ZenCodeTestResults.TEST_SEPARATOR); | ||
context = parent; | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
private boolean hasValidParent(ExtensionContext context) { | ||
return context.getParent().isPresent() && !JUNIT_EXECUTOR.equals(context.getParent().get().getDisplayName()); | ||
} | ||
|
||
/** | ||
* Callback that is invoked once <em>after</em> all tests in the current | ||
* container. | ||
* | ||
* @param context the current extension context; never {@code null} | ||
*/ | ||
@Override | ||
public void afterAll(ExtensionContext context) throws Exception { | ||
ZenCodeTestResults.getInstance().saveToDisk(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.