From 622f19bf29f11c2feaf1b86ae962be6498a6bde8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 17 Mar 2019 13:20:50 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Fix=20exclusion=20in=20search=20c?= =?utf8?q?ommand?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/xdcc/ui/stdin/SearchCommand.java | 6 ++-- .../xdcc/ui/stdin/SearchCommandTest.java | 37 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/test/java/net/pterodactylus/xdcc/ui/stdin/SearchCommandTest.java diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java index 746f01c..3b3afe8 100644 --- a/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java +++ b/src/main/java/net/pterodactylus/xdcc/ui/stdin/SearchCommand.java @@ -90,18 +90,18 @@ public class SearchCommand implements Command { return state.setLastResults(lastResult); } - private static class SearchParameter { + static class SearchParameter { private final boolean exclude; private final Pattern pattern; - private SearchParameter(boolean exclude, Pattern pattern) { + SearchParameter(boolean exclude, Pattern pattern) { this.exclude = exclude; this.pattern = pattern; } public boolean matches(String filename) { - return pattern.matcher(filename).find() && !exclude; + return pattern.matcher(filename).find() == !exclude; } public static SearchParameter from(String parameter) { diff --git a/src/test/java/net/pterodactylus/xdcc/ui/stdin/SearchCommandTest.java b/src/test/java/net/pterodactylus/xdcc/ui/stdin/SearchCommandTest.java new file mode 100644 index 0000000..5a8e393 --- /dev/null +++ b/src/test/java/net/pterodactylus/xdcc/ui/stdin/SearchCommandTest.java @@ -0,0 +1,37 @@ +package net.pterodactylus.xdcc.ui.stdin; + +import static java.util.regex.Pattern.compile; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +import net.pterodactylus.xdcc.ui.stdin.SearchCommand.SearchParameter; + +import org.junit.Test; + +public class SearchCommandTest { + + @Test + public void searchParameterMatchesIfRegexMatchesAndExcludeIsFalse() { + SearchParameter searchParameter = new SearchParameter(false, compile("abc")); + assertThat(searchParameter.matches("test abctest"), equalTo(true)); + } + + @Test + public void searchParameterDoesNotMatchIfRegexDoesNotMatchAndExcludeIsFalse() { + SearchParameter searchParameter = new SearchParameter(false, compile("abc")); + assertThat(searchParameter.matches("test deftest"), equalTo(false)); + } + + @Test + public void searchParameterMatchesIfRegexDoesNotMatchAndExcludeIsTrue() { + SearchParameter searchParameter = new SearchParameter(true, compile("abc")); + assertThat(searchParameter.matches("test deftest"), equalTo(true)); + } + + @Test + public void searchParameterDoesNotMatchIfRegexMatchesAndExcludeIsTrue() { + SearchParameter searchParameter = new SearchParameter(true, compile("abc")); + assertThat(searchParameter.matches("test abctest"), equalTo(false)); + } + +} -- 2.7.4