Skip to content

Commit 950ccc3

Browse files
authored
Merge pull request #489 from Pieter12345/randomtest-optimization
RandomTest optimization
2 parents d79972e + 4fba278 commit 950ccc3

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

src/main/java/com/laytonsmith/annotations/api.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ private Platforms(PlatformResolver resolver, String platformName) {
4747
}
4848

4949
/**
50-
* Returns the platform this is implemented for. The default is {
50+
* Returns the platform this is implemented for. The default is {@link api.Platforms#INTERPRETER_JAVA}.
5151
*
52-
* @see api.Platforms#INTERPRETER_JAVA}.
52+
* @see {@link api.Platforms#INTERPRETER_JAVA}.
5353
* @return
5454
*/
5555
Platforms[] platform() default {api.Platforms.INTERPRETER_JAVA};

src/main/java/com/laytonsmith/core/functions/CompositeFunction.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.laytonsmith.core.exceptions.ConfigCompileGroupException;
1717
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
1818
import com.laytonsmith.core.exceptions.FunctionReturnException;
19+
1920
import java.util.HashMap;
2021
import java.util.Map;
2122

@@ -54,7 +55,12 @@ public Construct exec(Target t, Environment environment, Construct... args) thro
5455
env.SetVarList(newVariables);
5556
Construct ret = CVoid.VOID;
5657
try {
57-
env.GetScript().eval(tree, environment);
58+
if(env.GetScript() != null) {
59+
env.GetScript().eval(tree, environment);
60+
} else {
61+
// This can happen when the environment is not fully setup during tests.
62+
Script.GenerateScript(null, null).eval(tree, environment);
63+
}
5864
} catch (FunctionReturnException ex) {
5965
ret = ex.getReturn();
6066
}

src/test/java/com/laytonsmith/testing/RandomTests.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.laytonsmith.abstraction.bukkit.BukkitMCCommandSender;
1313
import com.laytonsmith.abstraction.bukkit.entities.BukkitMCPlayer;
1414
import com.laytonsmith.annotations.api;
15+
import com.laytonsmith.annotations.api.Platforms;
1516
import com.laytonsmith.commandhelper.CommandHelperPlugin;
1617
import com.laytonsmith.core.MethodScriptComplete;
1718
import com.laytonsmith.core.ObjectGenerator;
@@ -363,26 +364,37 @@ public void testVoidAndReturnedVoidAreSEqualsAndOthers() throws Exception {
363364
public void testFunctionsAreOnlyDefinedOnce() throws Exception {
364365
Set<String> uhohs = new HashSet<>();
365366
Set<Class<Function>> set = ClassDiscovery.getDefaultInstance().loadClassesThatExtend(Function.class);
366-
for(Class<Function> cf1 : set) {
367-
for(Class<Function> cf2 : set) {
368-
if(cf1 == cf2) {
369-
continue;
370-
}
371-
api cf1a = cf1.getAnnotation(api.class);
372-
api cf2a = cf2.getAnnotation(api.class);
373-
if(cf1a == null || cf2a == null) {
374-
continue;
375-
}
376-
if(!Arrays.equals(cf1a.platform(), cf2a.platform())) {
377-
continue;
378-
}
379-
Function f1 = ReflectionUtils.instantiateUnsafe(cf1);
380-
Function f2 = ReflectionUtils.instantiateUnsafe(cf2);
381-
if(f1.getName().equals(f2.getName())) {
382-
uhohs.add(f1.getName() + " is implemented in two places, " + cf1 + " and " + cf2);
383-
}
367+
368+
// Iterate over all function classes, adding a message to "uhohs" if they are double defined.
369+
Map<String, Class<Function>> funcMap = new HashMap<>();
370+
for(Class<Function> funcClass : set) {
371+
372+
// Ignore non-api functions.
373+
api funcClassApi = funcClass.getAnnotation(api.class);
374+
if(funcClassApi == null) {
375+
continue;
376+
}
377+
378+
// Get the function name.
379+
String funcName = ReflectionUtils.instantiateUnsafe(funcClass).getName();
380+
381+
// Create an identifier string of the function name and its platforms.
382+
// Format: "funcName\tplatform1\tplatform2\t...platformN". Platforms are sorted to 'compare as sets'.
383+
StringBuilder idStr = new StringBuilder(funcName);
384+
Platforms[] platforms = funcClassApi.platform();
385+
Arrays.sort(platforms, (Platforms p1, Platforms p2) -> p1.toString().compareTo(p2.toString()));
386+
for(Platforms platform : platforms) {
387+
idStr.append("\t").append(platform.toString());
388+
}
389+
390+
// Store the function in the map by its identifier, adding an message if it is double defined.
391+
Class<Function> replacedFuncClass = funcMap.put(idStr.toString(), funcClass);
392+
if(replacedFuncClass != null) {
393+
uhohs.add(funcName + " is implemented in two places, " + funcClass + " and " + replacedFuncClass);
384394
}
385395
}
396+
397+
// Fail if a function was double defined.
386398
if(!uhohs.isEmpty()) {
387399
fail(StringUtils.Join(uhohs, "\n"));
388400
}

0 commit comments

Comments
 (0)