aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorExternalTime <84183548+ExternalTime@users.noreply.github.com>2021-09-21 13:13:36 +0200
committerExternalTime <84183548+ExternalTime@users.noreply.github.com>2021-09-21 13:22:29 +0200
commitd5b5439a558a05b46399793839cb210ded83699e (patch)
treea7173deae8be566e479cb9460c6608c245a9e56c
parent00cd3eb2401599f8e53bc3ffa211aa4ddb39791f (diff)
downloadSkyblocker-d5b5439a558a05b46399793839cb210ded83699e.tar.gz
Skyblocker-d5b5439a558a05b46399793839cb210ded83699e.tar.bz2
Skyblocker-d5b5439a558a05b46399793839cb210ded83699e.zip
Added abstract test for chat listeners
-rw-r--r--src/test/java/me/xmrvizzy/skyblocker/chat/ChatListenerTest.java31
-rw-r--r--src/test/java/me/xmrvizzy/skyblocker/chat/filters/AdFilterTest.java32
-rw-r--r--src/test/java/me/xmrvizzy/skyblocker/skyblock/dwarven/FetchurTest.java15
3 files changed, 51 insertions, 27 deletions
diff --git a/src/test/java/me/xmrvizzy/skyblocker/chat/ChatListenerTest.java b/src/test/java/me/xmrvizzy/skyblocker/chat/ChatListenerTest.java
new file mode 100644
index 00000000..a38779be
--- /dev/null
+++ b/src/test/java/me/xmrvizzy/skyblocker/chat/ChatListenerTest.java
@@ -0,0 +1,31 @@
+package me.xmrvizzy.skyblocker.chat;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public abstract class ChatListenerTest<T extends ChatListener> {
+ private final Pattern pattern;
+
+ public ChatListenerTest(T chatListener) {
+ pattern = chatListener.getPattern();
+ }
+
+ protected void assertMatches(String text) {
+ assertTrue(pattern.matcher(text).matches());
+ }
+
+ protected void assertNotMatches(String text) {
+ assertFalse(pattern.matcher(text).matches());
+ }
+
+ protected String[] getGroups(String text) {
+ Matcher matcher = pattern.matcher(text);
+ assertTrue(matcher.matches());
+ String[] groups = new String[matcher.groupCount() + 1];
+ for (int i = 0; i < groups.length; i++)
+ groups[i] = matcher.group(i);
+ return groups;
+ }
+} \ No newline at end of file
diff --git a/src/test/java/me/xmrvizzy/skyblocker/chat/filters/AdFilterTest.java b/src/test/java/me/xmrvizzy/skyblocker/chat/filters/AdFilterTest.java
index 8c0bf245..54e1643d 100644
--- a/src/test/java/me/xmrvizzy/skyblocker/chat/filters/AdFilterTest.java
+++ b/src/test/java/me/xmrvizzy/skyblocker/chat/filters/AdFilterTest.java
@@ -1,56 +1,50 @@
package me.xmrvizzy.skyblocker.chat.filters;
+import me.xmrvizzy.skyblocker.chat.ChatListenerTest;
import org.junit.jupiter.api.Test;
-import java.util.regex.Pattern;
-
-import static org.junit.jupiter.api.Assertions.*;
-
-class AdFilterTest {
-
- private final static Pattern AD_PATTERN = new AdFilter().getPattern();
+class AdFilterTest extends ChatListenerTest<AdFilter> {
+ public AdFilterTest() {
+ super(new AdFilter());
+ }
@Test
void noRank() {
- testAd("§7Advertiser§7: buy");
+ assertMatches("§7Advertiser§7: buy");
}
@Test
void vip() {
- testAd("§a[VIP] Advertiser§f: buy");
+ assertMatches("§a[VIP] Advertiser§f: buy");
}
@Test
void mvp() {
- testAd("§b[MVP§c+§b] Advertiser§f: buy");
+ assertMatches("§b[MVP§c+§b] Advertiser§f: buy");
}
@Test
void plusPlus() {
- testAd("§6[MVP§c++§6] Advertiser§f: buy");
+ assertMatches("§6[MVP§c++§6] Advertiser§f: buy");
}
@Test
void simpleAd() {
- testAd("§b[MVP§c+§b] b2dderr§f: buying prismapump");
+ assertMatches("§b[MVP§c+§b] b2dderr§f: buying prismapump");
}
@Test
void uppercaseAd() {
- testAd("§a[VIP] Tecnoisnoob§f: SELLING REJUVENATE 5 Book on ah!");
+ assertMatches("§a[VIP] Tecnoisnoob§f: SELLING REJUVENATE 5 Book on ah!");
}
@Test
void characterSpam() {
- testAd("§a[VIP] Benyyy_§f: Hey, Visit my Island, i spent lots of time to build it! I also made donate room! <<<<<<<<<<<<<<<<<<<");
+ assertMatches("§a[VIP] Benyyy_§f: Hey, Visit my Island, i spent lots of time to build it! I also made donate room! <<<<<<<<<<<<<<<<<<<");
}
@Test
void notAd() {
- assertFalse(AD_PATTERN.matcher("§a[VIP] NotMatching§f: This message shouldn't match!").matches());
- }
-
- public void testAd(String ad) {
- assertTrue(AD_PATTERN.matcher(ad).matches());
+ assertNotMatches("§a[VIP] NotMatching§f: This message shouldn't match!");
}
} \ No newline at end of file
diff --git a/src/test/java/me/xmrvizzy/skyblocker/skyblock/dwarven/FetchurTest.java b/src/test/java/me/xmrvizzy/skyblocker/skyblock/dwarven/FetchurTest.java
index a7426504..1ae1ac70 100644
--- a/src/test/java/me/xmrvizzy/skyblocker/skyblock/dwarven/FetchurTest.java
+++ b/src/test/java/me/xmrvizzy/skyblocker/skyblock/dwarven/FetchurTest.java
@@ -1,18 +1,17 @@
package me.xmrvizzy.skyblocker.skyblock.dwarven;
+import me.xmrvizzy.skyblocker.chat.ChatListenerTest;
import org.junit.jupiter.api.Test;
-import java.util.regex.Matcher;
-
import static org.junit.jupiter.api.Assertions.*;
-class FetchurTest {
- private static final Fetchur fetchur = new Fetchur();
+class FetchurTest extends ChatListenerTest<Fetchur> {
+ public FetchurTest() {
+ super(new Fetchur());
+ }
@Test
public void patternCaptures() {
- Matcher m = fetchur.getPattern().matcher("§e[NPC] Fetchur§f: its a hint");
- assertTrue(m.matches());
- assertEquals(m.group(1), "a hint");
+ assertEquals(getGroups("§e[NPC] Fetchur§f: its a hint")[1], "a hint");
}
-} \ No newline at end of file
+}