Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Add full class test generator to make testing easier
Browse files Browse the repository at this point in the history
  • Loading branch information
Deamon5550 committed Mar 27, 2018
1 parent 58727cc commit f214119
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -44,6 +44,7 @@ dependencies {
license {
header file('LICENSE')
include '**/*.java'
exclude '**/test/generate/*.java'
newLine = false
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/spongepowered/test/ast/FullClassTests.java
Expand Up @@ -54,6 +54,13 @@ public static void setup() {
LibraryConfiguration.parallel = false;
}

// TODO: auto-detect all test cases and generate a junit test per file

@Test
public void testBasic() throws Exception {
compare("javaclasses/BasicClass", Language.JAVA);
}

@Test
public void testGenerics() throws Exception {
compare("javaclasses/GenericsTestClass", Language.JAVA);
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/org/spongepowered/test/generate/TestGenerator.java
@@ -0,0 +1,74 @@
/*
* The MIT License (MIT)
*
* Copyright (c) Despector <https://despector.voxelgenesis.com>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.test.generate;

import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;

public class TestGenerator {

public static void main(String[] args) {
File tests_dir = new File("src/test/java/org/spongepowered/test/generate");
if (!tests_dir.exists() || !tests_dir.isDirectory()) {
System.err.println("Tests directory " + tests_dir.getAbsolutePath() + " does not exist or is not a directory.");
return;
}
File tests_bin_dir = new File("build/classes/java/test/org/spongepowered/test/generate");
if (!tests_bin_dir.exists() || !tests_bin_dir.isDirectory()) {
System.err.println("Tests directory " + tests_dir.getAbsolutePath()
+ " does not exist or is not a directory. (have you run `./gradlew build` yet?)");
return;
}
File tests_out_dir = new File("src/test/resources/javaclasses");
if (!tests_out_dir.exists()) {
tests_out_dir.mkdirs();
}
for (File f : tests_dir.listFiles()) {
if (f.getName().equals("TestGenerator.java")) {
continue;
}
if (!f.getName().endsWith(".java")) {
continue;
}
String name = f.getName().substring(0, f.getName().length() - 5);
File compiled = new File(tests_bin_dir, name + ".class");
if (!compiled.exists()) {
System.err.println("Test file " + name + " does not exist in the compiled output. (have you run `./gradlew build` yet?)");
continue;
}
System.out.println("Adding test for " + name);
try {
Files.copy(f, new File(tests_out_dir, name + ".java.test"));
Files.copy(compiled, new File(tests_out_dir, name + ".class.test"));
f.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
Binary file added src/test/resources/javaclasses/BasicClass.class.test
Binary file not shown.
25 changes: 25 additions & 0 deletions src/test/resources/javaclasses/BasicClass.java.test
@@ -0,0 +1,25 @@
package org.spongepowered.test.generate;

public class BasicClass {

public int a;
char b;
private String c;

public BasicClass(int a) {
this.a = a;
}

public void foo() {
this.b = 'b';
}

public void bar(char d) {
this.b = d;
}

public String get() {
return this.c;
}

}

0 comments on commit f214119

Please sign in to comment.