Fixes the logic and adds a bunch of tests to prove the logic works.
This commit is contained in:
tastybento 2023-06-20 22:15:53 -07:00 committed by GitHub
parent d6d86d39d2
commit bcb4ed28b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 7 deletions

View File

@ -57,20 +57,19 @@ public class BannedCommands implements Listener {
}
private boolean checkCmd(String cmd, String[] args) {
// Commands are guilty until proven innocent :-)
boolean banned = true;
// Get the elements of the banned command by splitting it
String[] bannedSplit = cmd.toLowerCase(java.util.Locale.ENGLISH).split(" ");
// If the banned command has the same number of elements or less than the entered command then it may be banned
if (bannedSplit.length <= args.length) {
if (bannedSplit.length <= args.length) {
for (int i = 0; i < bannedSplit.length; i++) {
if (!bannedSplit[i].equals(args[i])) {
banned = false;
break;
if (!bannedSplit[i].equalsIgnoreCase(args[i])) {
return false;
}
}
} else {
return false;
}
return banned;
return true;
}
/**

View File

@ -262,6 +262,106 @@ public class BannedCommandsTest {
}
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test
public void testBannedCommandsWithBannedCommand2() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/spawn");
BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>();
banned.add("cmi sethome");
when(iwm.getVisitorBannedCommands(any())).thenReturn(banned);
bvc.onVisitorCommand(e);
verify(iwm).getVisitorBannedCommands(any());
assertFalse(e.isCancelled());
}
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test
public void testBannedCommandsWithBannedCommand3() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/cmi sethome");
BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>();
banned.add("cmi sethome");
when(iwm.getVisitorBannedCommands(any())).thenReturn(banned);
bvc.onVisitorCommand(e);
verify(iwm).getVisitorBannedCommands(any());
assertTrue(e.isCancelled());
}
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test
public void testBannedCommandsWithBannedComman4() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/cmi");
BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>();
banned.add("cmi sethome");
when(iwm.getVisitorBannedCommands(any())).thenReturn(banned);
bvc.onVisitorCommand(e);
verify(iwm).getVisitorBannedCommands(any());
assertFalse(e.isCancelled());
}
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test
public void testBannedCommandsWithBannedCommand5() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/cmi homey");
BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>();
banned.add("cmi sethome");
when(iwm.getVisitorBannedCommands(any())).thenReturn(banned);
bvc.onVisitorCommand(e);
verify(iwm).getVisitorBannedCommands(any());
assertFalse(e.isCancelled());
}
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test
public void testBannedCommandsWithBannedCommand6() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/spawn");
BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>();
banned.add("cmi sethome");
banned.add("spawn sethome now");
when(iwm.getVisitorBannedCommands(any())).thenReturn(banned);
bvc.onVisitorCommand(e);
verify(iwm).getVisitorBannedCommands(any());
assertFalse(e.isCancelled());
}
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test
public void testBannedCommandsWithBannedCommand7() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/spawn");
BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>();
banned.add("cmi sethome");
banned.add("spawn sethome now");
banned.add("cmi multi now");
when(iwm.getVisitorBannedCommands(any())).thenReturn(banned);
bvc.onVisitorCommand(e);
verify(iwm).getVisitorBannedCommands(any());
assertFalse(e.isCancelled());
}
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/