Skip to content

Commit

Permalink
test: Update FMethodTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Clashsoft committed Feb 21, 2021
1 parent 3c83748 commit 4a061bc
Showing 1 changed file with 23 additions and 56 deletions.
79 changes: 23 additions & 56 deletions src/test/java/org/fulib/generator/FMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
import org.fulib.builder.ClassModelManager;
import org.fulib.classmodel.ClassModel;
import org.fulib.classmodel.Clazz;
import org.fulib.classmodel.FMethod;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class FMethodTest
Expand All @@ -29,73 +28,41 @@ public void testFMethods() throws Exception

Tools.removeDirAndFiles(targetFolder);

ClassModelManager mm = new ClassModelManager();
final ClassModelManager mm = new ClassModelManager();
mm.setMainJavaDir(sourceFolder);
mm.setPackageName(packageName);

Clazz party = mm.haveClass("Party");
// mm.haveAttribute(party, "name", "String");
final Clazz party = mm.haveClass("Party");

FMethod method = mm.haveMethod(party, "public void hello()", " System.out.println(\"World!\");\n");
mm.haveMethod(party, "@Deprecated public int theAnswer()", "return 42;");
mm.haveMethod(party, "public int theAnswer(int question)", "return question * 2;");
mm.haveMethod(party, "public <T extends Number> import(java.util.List)<T> generic(T... args)",
"return import(java.util.Arrays).asList(args);");

ClassModel model = mm.getClassModel();
final ClassModel model = mm.getClassModel();
Fulib.generator().generate(model);

int returnCode = Tools.javac(outFolder, model.getPackageSrcFolder());
final int returnCode = Tools.javac(outFolder, model.getPackageSrcFolder());
assertThat("compiler return code: ", returnCode, is(0));

// does removal work?
party.withoutMethods(method);

Fulib.generator().generate(model);

returnCode = Tools.javac(outFolder, model.getPackageSrcFolder());
assertThat("compiler return code: ", returnCode, is(0));

method = mm.haveMethod(party, "public int theAnswer()", " return 42;\n");

Fulib.generator().generate(model);

returnCode = Tools.javac(outFolder, model.getPackageSrcFolder());
assertThat("compiler return code: ", returnCode, is(0));

// add a parameter
method.getParams().put("question", "int");
method.setMethodBody(" return question * 2;\n");

Fulib.generator().generate(model);

returnCode = Tools.javac(outFolder, model.getPackageSrcFolder());
assertThat("compiler return code: ", returnCode, is(0));

mm.haveMethod(party, "@import(org.junit.jupiter.api.Test) public void testQuestion()",
"import(static org.hamcrest.MatcherAssert.assertThat)(theAnswer(21), import(static org.hamcrest.CoreMatchers.equalTo)(42));");

Fulib.generator().generate(model);

mm.haveMethod(party, "public int theAnswer()", " return 42;\n");

// TODO why 3 times?
Fulib.generator().generate(model);
Fulib.generator().generate(model);
Fulib.generator().generate(model);

returnCode = Tools.javac(outFolder, model.getPackageSrcFolder());
assertThat("compiler return code: ", returnCode, is(0));

try (final URLClassLoader classLoader = URLClassLoader
.newInstance(new URL[] { new File(outFolder).toURI().toURL() }))
try (final URLClassLoader classLoader = URLClassLoader.newInstance(
new URL[] { new File(outFolder).toURI().toURL() }))
{
final Class<?> partyClass = Class.forName(model.getPackageName() + ".Party", true, classLoader);
final Method answerMethod = partyClass.getMethod("theAnswer", int.class);
final Method secondMethod = partyClass.getMethod("testQuestion");

final Object theParty = partyClass.newInstance();

final Object answer = answerMethod.invoke(theParty, 23);
assertThat(answer, equalTo(46));
final Method answerMethod = partyClass.getMethod("theAnswer");
assertThat(answerMethod.getAnnotation(Deprecated.class), notNullValue());
assertThat(answerMethod.invoke(theParty), equalTo(42));

final Method answerMethod2 = partyClass.getMethod("theAnswer", int.class);
assertThat(answerMethod2.invoke(theParty, 23), equalTo(46));

final Method genericMethod = partyClass.getMethod("generic", Number[].class);
assertThat(genericMethod.isVarArgs(), is(true));
assertThat(genericMethod.getTypeParameters()[0].getBounds()[0], is(Number.class));

secondMethod.invoke(theParty);
assertThat(genericMethod.invoke(theParty, (Object) new Number[] { 1, 2, 3 }), equalTo(Arrays.asList(1, 2, 3)));
}
}
}

0 comments on commit 4a061bc

Please sign in to comment.