From 75f88d65ac1878a22b68eee8cd1ac7a3d9f4160d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Jun 2022 14:06:28 +0200 Subject: [PATCH] Fix stuff Add tests --- src/de/steamwar/command/CommandPart.java | 2 +- src/de/steamwar/command/SWCommandUtils.java | 2 +- testsrc/de/steamwar/command/CacheCommand.java | 38 +++++++++++++++++++ .../de/steamwar/command/CacheCommandTest.java | 28 ++++++++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 testsrc/de/steamwar/command/CacheCommand.java create mode 100644 testsrc/de/steamwar/command/CacheCommandTest.java diff --git a/src/de/steamwar/command/CommandPart.java b/src/de/steamwar/command/CommandPart.java index 283dd51..d5a5220 100644 --- a/src/de/steamwar/command/CommandPart.java +++ b/src/de/steamwar/command/CommandPart.java @@ -188,7 +188,7 @@ class CommandPart { return; } - Collection strings = typeMapper.tabCompletes(sender, Arrays.copyOf(args, startIndex), args[startIndex]); + Collection strings = tabCompletes(sender, args, startIndex); if (strings != null) { current.addAll(strings); } diff --git a/src/de/steamwar/command/SWCommandUtils.java b/src/de/steamwar/command/SWCommandUtils.java index 86716d2..9620631 100644 --- a/src/de/steamwar/command/SWCommandUtils.java +++ b/src/de/steamwar/command/SWCommandUtils.java @@ -313,7 +313,7 @@ public class SWCommandUtils { } static T[] getAnnotation(Method method, Class annotation) { - if (method.getAnnotations().length != 1) return null; + if (method.getAnnotations().length == 0) return null; return method.getDeclaredAnnotationsByType(annotation); } } diff --git a/testsrc/de/steamwar/command/CacheCommand.java b/testsrc/de/steamwar/command/CacheCommand.java new file mode 100644 index 0000000..870d56d --- /dev/null +++ b/testsrc/de/steamwar/command/CacheCommand.java @@ -0,0 +1,38 @@ +package de.steamwar.command; + +import de.steamwar.command.dto.TestSWCommand; +import de.steamwar.command.dto.TestTypeMapper; + +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.atomic.AtomicInteger; + +public class CacheCommand extends TestSWCommand { + + public CacheCommand() { + super("typemapper"); + } + + @Register + public void test(String sender, int tabCompleteTest) { + } + + private AtomicInteger count = new AtomicInteger(); + + @Cached + @Mapper(value = "int", local = true) + public AbstractTypeMapper typeMapper() { + System.out.println("TypeMapper register"); + return new TestTypeMapper() { + @Override + public Integer map(String sender, String[] previousArguments, String s) { + return Integer.parseInt(s); + } + + @Override + public Collection tabCompletes(String sender, String[] previousArguments, String s) { + return Arrays.asList(count.getAndIncrement() + ""); + } + }; + } +} diff --git a/testsrc/de/steamwar/command/CacheCommandTest.java b/testsrc/de/steamwar/command/CacheCommandTest.java new file mode 100644 index 0000000..5152d0b --- /dev/null +++ b/testsrc/de/steamwar/command/CacheCommandTest.java @@ -0,0 +1,28 @@ +package de.steamwar.command; + +import org.hamcrest.Matchers; +import org.junit.Test; + +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class CacheCommandTest { + + @Test + public void testCaching() { + CacheCommand cmd = new CacheCommand(); + List tabCompletions1 = cmd.tabComplete("test", "", new String[]{""}); + List tabCompletions2 = cmd.tabComplete("test", "", new String[]{""}); + assertThat(tabCompletions1, is(equalTo(tabCompletions2))); + } + + @Test + public void testCachingWithDifferentSenders() { + CacheCommand cmd = new CacheCommand(); + List tabCompletions1 = cmd.tabComplete("test", "", new String[]{""}); + List tabCompletions2 = cmd.tabComplete("test2", "", new String[]{""}); + assertThat(tabCompletions1, is(not(equalTo(tabCompletions2)))); + } +}