reconfigure implementation projects

This commit is contained in:
dfsek
2021-11-21 21:41:19 -07:00
parent 22eae6d515
commit 7e0fa4854f
100 changed files with 6 additions and 683 deletions

View File

@@ -0,0 +1,133 @@
import com.dfsek.tectonic.annotations.Value;
import com.dfsek.tectonic.config.ConfigTemplate;
import com.dfsek.tectonic.config.Configuration;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.tectonic.yaml.YamlConfiguration;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.config.preprocessor.MetaListLikePreprocessor;
import com.dfsek.terra.config.preprocessor.MetaMapPreprocessor;
import com.dfsek.terra.config.preprocessor.MetaNumberPreprocessor;
import com.dfsek.terra.config.preprocessor.MetaStringPreprocessor;
import com.dfsek.terra.config.preprocessor.MetaValuePreprocessor;
public class MetaTest {
@Test
public void testMetaList() {
Configuration meta = new YamlConfiguration(MetaTest.class.getResourceAsStream("/meta.yml"), "meta.yml");
Configuration metaTarget = new YamlConfiguration(MetaTest.class.getResourceAsStream("/metaTarget.yml"), "metaTarget.yml");
Map<String, Configuration> configurationMap = new HashMap<>();
configurationMap.put(meta.getName(), meta);
configurationMap.put(metaTarget.getName(), metaTarget);
ConfigLoader loader = new ConfigLoader();
loader.registerPreprocessor(Meta.class, new MetaStringPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaListLikePreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaMapPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaNumberPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurationMap));
loader.load(new MetaListConfig(), meta).list.forEach(System.out::println);
}
@Test
public void testMetaMap() {
Configuration meta = new YamlConfiguration(MetaTest.class.getResourceAsStream("/meta.yml"), "meta.yml");
Configuration metaTarget = new YamlConfiguration(MetaTest.class.getResourceAsStream("/metaTarget.yml"), "metaTarget.yml");
Map<String, Configuration> configurationMap = new HashMap<>();
configurationMap.put(meta.getName(), meta);
configurationMap.put(metaTarget.getName(), metaTarget);
ConfigLoader loader = new ConfigLoader();
loader.registerPreprocessor(Meta.class, new MetaStringPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaListLikePreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaMapPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaNumberPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurationMap));
loader.load(new MetaMapConfig(), meta).map.forEach((k, v) -> System.out.println(k + ": " + v));
}
@Test
public void testMetaString() {
Configuration meta = new YamlConfiguration(MetaTest.class.getResourceAsStream("/meta.yml"), "meta.yml");
Configuration metaTarget = new YamlConfiguration(MetaTest.class.getResourceAsStream("/metaTarget.yml"), "metaTarget.yml");
Map<String, Configuration> configurationMap = new HashMap<>();
configurationMap.put(meta.getName(), meta);
configurationMap.put(metaTarget.getName(), metaTarget);
ConfigLoader loader = new ConfigLoader();
loader.registerPreprocessor(Meta.class, new MetaStringPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaListLikePreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaMapPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaNumberPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurationMap));
System.out.println(loader.load(new MetaStringConfig(), meta).string);
}
@Test
public void testMetaNumber() {
Configuration meta = new YamlConfiguration(MetaTest.class.getResourceAsStream("/meta.yml"), "meta.yml");
Configuration metaTarget = new YamlConfiguration(MetaTest.class.getResourceAsStream("/metaTarget.yml"), "metaTarget.yml");
Map<String, Configuration> configurationMap = new HashMap<>();
configurationMap.put(meta.getName(), meta);
configurationMap.put(metaTarget.getName(), metaTarget);
ConfigLoader loader = new ConfigLoader();
loader.registerPreprocessor(Meta.class, new MetaStringPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaListLikePreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaMapPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaNumberPreprocessor(configurationMap));
loader.registerPreprocessor(Meta.class, new MetaValuePreprocessor(configurationMap));
System.out.println("int: " + loader.load(new MetaNumberConfig(), meta).integer);
System.out.println("double: " + loader.load(new MetaNumberConfig(), meta).aDouble);
}
private static final class MetaListConfig implements ConfigTemplate {
@Value("list")
private @Meta List<@Meta String> list;
}
private static final class MetaMapConfig implements ConfigTemplate {
@Value("map")
private @Meta Map<@Meta String, @Meta String> map;
}
private static final class MetaStringConfig implements ConfigTemplate {
@Value("string")
private @Meta String string;
}
private static final class MetaNumberConfig implements ConfigTemplate {
@Value("int")
private @Meta int integer;
@Value("double")
private @Meta double aDouble;
}
}

View File

@@ -0,0 +1,259 @@
/*
* This file is part of Terra.
*
* Terra 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.
*
* Terra 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 Terra. If not, see <https://www.gnu.org/licenses/>.
*/
package command;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import com.dfsek.terra.api.command.CommandManager;
import com.dfsek.terra.api.command.CommandTemplate;
import com.dfsek.terra.api.command.annotation.Argument;
import com.dfsek.terra.api.command.annotation.Command;
import com.dfsek.terra.api.command.annotation.Subcommand;
import com.dfsek.terra.api.command.annotation.Switch;
import com.dfsek.terra.api.command.annotation.inject.ArgumentTarget;
import com.dfsek.terra.api.command.annotation.inject.SwitchTarget;
import com.dfsek.terra.api.command.arg.DoubleArgumentParser;
import com.dfsek.terra.api.command.arg.IntegerArgumentParser;
import com.dfsek.terra.api.command.exception.CommandException;
import com.dfsek.terra.api.command.exception.InvalidArgumentsException;
import com.dfsek.terra.api.command.exception.MalformedCommandException;
import com.dfsek.terra.api.entity.CommandSender;
import com.dfsek.terra.commands.TerraCommandManager;
import static org.junit.jupiter.api.Assertions.*;
public class CommandTest {
@Test
public void subcommand() throws CommandException {
CommandManager manager = new TerraCommandManager(null);
manager.register("test", DemoParentCommand.class);
manager.execute("test", null, Arrays.asList("subcommand1", "first", "2"));
manager.execute("test", null, Arrays.asList("subcommand2", "first", "2"));
manager.execute("test", null, Arrays.asList("s1", "first", "2", "3.4"));
manager.execute("test", null, Arrays.asList("s2", "first", "2"));
manager.execute("test", null, Arrays.asList("sub1", "first", "2", "3.4"));
manager.execute("test", null, Arrays.asList("sub2", "first", "2"));
manager.execute("test", null, Arrays.asList("first", "2")); // Parent command args
System.out.println("ARGS: " + manager.getMaxArgumentDepth());
}
@Test
public void args() throws CommandException {
CommandManager manager = new TerraCommandManager(null);
manager.register("test", DemoCommand.class);
manager.execute("test", null, Arrays.asList("first", "2"));
manager.execute("test", null, Arrays.asList("first", "2", "3.4"));
}
@Test
public void argsBeforeFlags() throws CommandException {
CommandManager manager = new TerraCommandManager(null);
manager.register("test", DemoCommand.class);
try {
manager.execute("test", null, Arrays.asList("first", "-flag", "2"));
fail();
} catch(InvalidArgumentsException ignore) {
}
}
@Test
public void requiredArgsFirst() throws CommandException {
CommandManager manager = new TerraCommandManager(null);
manager.register("test", DemoInvalidCommand.class);
try {
manager.execute("test", null, Arrays.asList("first", "2"));
fail();
} catch(MalformedCommandException ignore) {
}
}
@Test
public void switches() throws CommandException {
CommandManager manager = new TerraCommandManager(null);
manager.register("test", DemoSwitchCommand.class);
manager.execute("test", null, Arrays.asList("first", "2"));
manager.execute("test", null, Arrays.asList("first", "2", "3.4"));
manager.execute("test", null, Arrays.asList("first", "2", "-a"));
manager.execute("test", null, Arrays.asList("first", "2", "3.4", "-b"));
manager.execute("test", null, Arrays.asList("first", "2", "-aSwitch"));
manager.execute("test", null, Arrays.asList("first", "2", "3.4", "-bSwitch"));
manager.execute("test", null, Arrays.asList("first", "2", "-aSwitch", "-b"));
manager.execute("test", null, Arrays.asList("first", "2", "3.4", "-bSwitch", "-a"));
}
@Command(
arguments = {
@Argument("arg0"),
@Argument(value = "arg1", argumentParser = IntegerArgumentParser.class),
@Argument(value = "arg2", required = false, argumentParser = DoubleArgumentParser.class, defaultValue = "0")
}
)
public static final class DemoCommand implements CommandTemplate {
@ArgumentTarget("arg0")
private String arg0;
@ArgumentTarget("arg1")
private Integer arg1;
@ArgumentTarget("arg2")
private Double arg2;
@Override
public void execute(CommandSender sender) {
System.out.println(arg0);
System.out.println(arg1);
System.out.println(arg2);
}
}
@Command(
arguments = {
@Argument("arg0"),
@Argument("arg1"),
@Argument(value = "arg2", required = false)
},
switches = {
@Switch(value = "a", aliases = "aSwitch"),
@Switch(value = "b", aliases = "bSwitch")
}
)
public static final class DemoSwitchCommand implements CommandTemplate {
@ArgumentTarget("arg0")
private String arg0;
@ArgumentTarget("arg1")
private String arg1;
@ArgumentTarget("arg2")
private String arg2;
@SwitchTarget("a")
private boolean a;
@SwitchTarget("b")
private boolean b;
@Override
public void execute(CommandSender sender) {
System.out.println(arg0);
System.out.println(arg1);
System.out.println(arg2);
System.out.println("A: " + a);
System.out.println("B: " + b);
}
}
@Command(
arguments = {
@Argument("arg0"),
@Argument(value = "arg2", required = false), // optional arguments must be last. this command is invalid.
@Argument("arg1")
}
)
public static final class DemoInvalidCommand implements CommandTemplate {
@Override
public void execute(CommandSender sender) {
throw new Error("this should never be reached");
}
}
@Command(
arguments = {
@Argument("arg0"),
@Argument("arg1"),
@Argument(value = "arg2", required = false),
},
subcommands = {
@Subcommand(
value = "subcommand1",
aliases = { "s1", "sub1" },
clazz = DemoChildCommand.class
),
@Subcommand(
value = "subcommand2",
aliases = { "s2", "sub2" },
clazz = DemoChildCommand.class // Duplicate command intentional.
)
}
)
public static final class DemoParentCommand implements CommandTemplate {
@ArgumentTarget("arg0")
private String arg0;
@ArgumentTarget("arg1")
private String arg1;
@ArgumentTarget("arg2")
private String arg2;
@Override
public void execute(CommandSender sender) {
System.out.println(arg0);
System.out.println(arg1);
System.out.println(arg2);
}
}
@Command(
arguments = {
@Argument("arg0"),
@Argument("arg1"),
@Argument(value = "arg2", required = false),
}
)
public static final class DemoChildCommand implements CommandTemplate {
@ArgumentTarget("arg0")
private String arg0;
@ArgumentTarget("arg1")
private String arg1;
@ArgumentTarget("arg2")
private String arg2;
@Override
public void execute(CommandSender sender) {
System.out.println(arg0);
System.out.println(arg1);
System.out.println(arg2);
}
}
}

View File

@@ -0,0 +1,82 @@
/*
* This file is part of Terra.
*
* Terra 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.
*
* Terra 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 Terra. If not, see <https://www.gnu.org/licenses/>.
*/
package profiler;
import org.junit.jupiter.api.Test;
import com.dfsek.terra.api.profiler.Profiler;
import com.dfsek.terra.profiler.ProfilerImpl;
public class ProfilerTest {
private static final Profiler PROFILER = new ProfilerImpl();
private static void doThing() throws InterruptedException {
PROFILER.push("thing");
Thread.sleep(1);
doOtherThing();
thing4();
PROFILER.pop("thing");
}
private static void doOtherThing() throws InterruptedException {
PROFILER.push("thing2");
Thread.sleep(2);
doThirdOtherThing();
thing4();
PROFILER.pop("thing2");
}
private static void doThirdOtherThing() throws InterruptedException {
PROFILER.push("thing3");
Thread.sleep(2);
PROFILER.pop("thing3");
}
private static void thing4() throws InterruptedException {
PROFILER.push("thing4");
Thread.sleep(2);
PROFILER.pop("thing4");
}
@Test
public void testProfiler() throws InterruptedException {
//PROFILER.start();
for(int i = 0; i < 100; i++) {
doThing();
}
for(int i = 0; i < 100; i++) {
doThirdOtherThing();
}
for(int i = 0; i < 100; i++) {
doOtherThing();
}
PROFILER.stop();
PROFILER.push("thing");
PROFILER.push("thing2");
PROFILER.start();
PROFILER.pop("thing2");
PROFILER.pop("thing");
PROFILER.push("thing4");
PROFILER.pop("thing4");
PROFILER.getTimings().forEach((id, timings) -> System.out.println(id + ": " + timings.toString()));
}
}

View File

@@ -0,0 +1,70 @@
/*
* This file is part of Terra.
*
* Terra 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.
*
* Terra 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 Terra. If not, see <https://www.gnu.org/licenses/>.
*/
package registry;
import org.junit.jupiter.api.Test;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.registry.OpenRegistry;
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
import com.dfsek.terra.registry.CheckedRegistryImpl;
import com.dfsek.terra.registry.OpenRegistryImpl;
import static org.junit.jupiter.api.Assertions.*;
public class RegistryTest {
@Test
public void openRegistry() {
OpenRegistry<String> test = new OpenRegistryImpl<>();
test.register("test", "bazinga");
assertEquals(test.get("test"), "bazinga");
}
@Test
public void openRegistryChecked() {
OpenRegistry<String> test = new OpenRegistryImpl<>();
test.registerChecked("test", "bazinga");
try {
test.registerChecked("test", "bazinga2");
fail("Shouldn't be able to re-register with #registerChecked!");
} catch(DuplicateEntryException ignore) {
}
}
@Test
public void checkedRegistry() {
CheckedRegistry<String> test = new CheckedRegistryImpl<>(new OpenRegistryImpl<>());
test.register("test", "bazinga");
assertEquals(test.get("test"), "bazinga");
try {
test.register("test", "bazinga2");
fail("Shouldn't be able to re-register in CheckedRegistry!");
} catch(DuplicateEntryException ignore) {
}
}
}