diff --git a/common/build.gradle b/common/build.gradle index 81580959d..5d6929100 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -1,4 +1,11 @@ +test { + useJUnitPlatform() +} + dependencies { + testCompile 'org.junit.jupiter:junit-jupiter-api:5.7.0' + testCompile 'org.junit.jupiter:junit-jupiter-engine:5.7.0' + compile project(':api') compile 'org.checkerframework:checker-qual:2.5.5' diff --git a/common/src/main/java/me/lucko/luckperms/common/verbose/expression/BooleanExpressionCompiler.java b/common/src/main/java/me/lucko/luckperms/common/verbose/expression/BooleanExpressionCompiler.java index 8c43e66e9..1e7d242ea 100644 --- a/common/src/main/java/me/lucko/luckperms/common/verbose/expression/BooleanExpressionCompiler.java +++ b/common/src/main/java/me/lucko/luckperms/common/verbose/expression/BooleanExpressionCompiler.java @@ -227,8 +227,8 @@ public class BooleanExpressionCompiler { Lexer(String expression) { this.tokenizer = new StreamTokenizer(new StringReader(expression)); this.tokenizer.resetSyntax(); - this.tokenizer.wordChars('!', '~'); // all ascii characters - this.tokenizer.whitespaceChars('\u0000', ' '); + this.tokenizer.whitespaceChars('\u0000', '\u0020'); + this.tokenizer.wordChars('\u0021', '\u007E'); "()&|!".chars().forEach(this.tokenizer::ordinaryChar); } @@ -278,20 +278,4 @@ public class BooleanExpressionCompiler { } } - /* - private static void assertion(String expression, boolean expected) { - if (compile(expression).eval(var -> var.equals("true")) != expected) { - throw new AssertionError(expression + " is not " + expected); - } - } - - public static void main(String[] args) { - assertion("false & false | true", true); - assertion("false & (false | true)", false); - assertion("true | false & false", true); - assertion("(true | false) & false", false); - assertion("(true & ((true | false) & !(true & false)))", true); - } - */ - } diff --git a/common/src/test/java/me/lucko/luckperms/common/util/DurationParserTest.java b/common/src/test/java/me/lucko/luckperms/common/util/DurationParserTest.java new file mode 100644 index 000000000..38a6f1c04 --- /dev/null +++ b/common/src/test/java/me/lucko/luckperms/common/util/DurationParserTest.java @@ -0,0 +1,91 @@ +package me.lucko.luckperms.common.util; + +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.time.temporal.ChronoUnit; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DurationParserTest { + + private static void test(Duration expected, String input) { + assertEquals(expected, DurationParser.parseDuration(input)); + } + + @Test + void testSimple() { + test(ChronoUnit.YEARS.getDuration().multipliedBy(2), "2y"); + test(ChronoUnit.YEARS.getDuration().multipliedBy(3), "3year"); + test(ChronoUnit.YEARS.getDuration().multipliedBy(4), "4years"); + test(ChronoUnit.YEARS.getDuration().multipliedBy(2), "2 y"); + test(ChronoUnit.YEARS.getDuration().multipliedBy(3), "3 year"); + test(ChronoUnit.YEARS.getDuration().multipliedBy(4), "4 years"); + + test(ChronoUnit.MONTHS.getDuration().multipliedBy(2), "2mo"); + test(ChronoUnit.MONTHS.getDuration().multipliedBy(3), "3month"); + test(ChronoUnit.MONTHS.getDuration().multipliedBy(4), "4months"); + test(ChronoUnit.MONTHS.getDuration().multipliedBy(2), "2 mo"); + test(ChronoUnit.MONTHS.getDuration().multipliedBy(3), "3 month"); + test(ChronoUnit.MONTHS.getDuration().multipliedBy(4), "4 months"); + + test(Duration.ofDays(7 * 2), "2w"); + test(Duration.ofDays(7 * 3), "3week"); + test(Duration.ofDays(7 * 4), "4weeks"); + test(Duration.ofDays(7 * 2), "2 w"); + test(Duration.ofDays(7 * 3), "3 week"); + test(Duration.ofDays(7 * 4), "4 weeks"); + + test(Duration.ofDays(2), "2d"); + test(Duration.ofDays(3), "3day"); + test(Duration.ofDays(4), "4days"); + test(Duration.ofDays(2), "2 d"); + test(Duration.ofDays(3), "3 day"); + test(Duration.ofDays(4), "4 days"); + + test(Duration.ofHours(2), "2h"); + test(Duration.ofHours(3), "3hour"); + test(Duration.ofHours(4), "4hours"); + test(Duration.ofHours(2), "2 h"); + test(Duration.ofHours(3), "3 hour"); + test(Duration.ofHours(4), "4 hours"); + + test(Duration.ofMinutes(2), "2m"); + test(Duration.ofMinutes(3), "3min"); + test(Duration.ofMinutes(4), "4mins"); + test(Duration.ofMinutes(5), "5minute"); + test(Duration.ofMinutes(6), "6minutes"); + test(Duration.ofMinutes(2), "2 m"); + test(Duration.ofMinutes(3), "3 min"); + test(Duration.ofMinutes(4), "4 mins"); + test(Duration.ofMinutes(5), "5 minute"); + test(Duration.ofMinutes(6), "6 minutes"); + + test(Duration.ofSeconds(2), "2s"); + test(Duration.ofSeconds(3), "3sec"); + test(Duration.ofSeconds(4), "4secs"); + test(Duration.ofSeconds(5), "5second"); + test(Duration.ofSeconds(6), "6seconds"); + test(Duration.ofSeconds(2), "2 s"); + test(Duration.ofSeconds(3), "3 sec"); + test(Duration.ofSeconds(4), "4 secs"); + test(Duration.ofSeconds(5), "5 second"); + test(Duration.ofSeconds(6), "6 seconds"); + } + + @Test + void testCombined() { + Duration expected = ChronoUnit.YEARS.getDuration().multipliedBy(5) + .plus(ChronoUnit.MONTHS.getDuration().multipliedBy(4)) + .plus(ChronoUnit.WEEKS.getDuration().multipliedBy(3)) + .plusDays(2) + .plusHours(1) + .plusMinutes(6) + .plusSeconds(7); + + test(expected, "5y 4mo 3w 2d 1h 6m 7s"); + test(expected, "5y4mo3w2d1h6m7s"); + test(expected, "5 years 4 months 3 weeks 2 days 1 hour 6 minutes 7 seconds"); + } + +} diff --git a/common/src/test/java/me/lucko/luckperms/common/util/EnumNamerTest.java b/common/src/test/java/me/lucko/luckperms/common/util/EnumNamerTest.java new file mode 100644 index 000000000..7965b1082 --- /dev/null +++ b/common/src/test/java/me/lucko/luckperms/common/util/EnumNamerTest.java @@ -0,0 +1,28 @@ +package me.lucko.luckperms.common.util; + +import com.google.common.collect.ImmutableMap; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class EnumNamerTest { + + @Test + void testSimple() { + EnumNamer namer = new EnumNamer<>( + TestEnum.class, + ImmutableMap.of(TestEnum.THING, "hi"), + v -> v.name().toLowerCase().replace('_', '-') + ); + + assertEquals("test", namer.name(TestEnum.TEST)); + assertEquals("a-test", namer.name(TestEnum.A_TEST)); + assertEquals("hi", namer.name(TestEnum.THING)); + } + + enum TestEnum { + TEST, A_TEST, THING + } + +} diff --git a/common/src/test/java/me/lucko/luckperms/common/util/IteratorsTest.java b/common/src/test/java/me/lucko/luckperms/common/util/IteratorsTest.java new file mode 100644 index 000000000..b387219a3 --- /dev/null +++ b/common/src/test/java/me/lucko/luckperms/common/util/IteratorsTest.java @@ -0,0 +1,49 @@ +package me.lucko.luckperms.common.util; + +import com.google.common.collect.ImmutableList; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class IteratorsTest { + + @Test + void testDivideEmpty() { + assertEquals(ImmutableList.of(), Iterators.divideIterable(ImmutableList.of(), 2)); + } + + @Test + void testDivideSimple() { + List> expected = ImmutableList.of( + ImmutableList.of("one", "two"), + ImmutableList.of("three", "four"), + ImmutableList.of("five") + + ); + List> actual = Iterators.divideIterable( + ImmutableList.of("one", "two", "three", "four", "five"), + 2 + ); + + assertEquals(expected, actual); + } + + @Test + void testDivideBoundary() { + List> expected = ImmutableList.of( + ImmutableList.of("one", "two"), + ImmutableList.of("three", "four") + + ); + List> actual = Iterators.divideIterable( + ImmutableList.of("one", "two", "three", "four"), + 2 + ); + + assertEquals(expected, actual); + } + +} diff --git a/common/src/test/java/me/lucko/luckperms/common/util/PaginatedTest.java b/common/src/test/java/me/lucko/luckperms/common/util/PaginatedTest.java new file mode 100644 index 000000000..ab21e8fbd --- /dev/null +++ b/common/src/test/java/me/lucko/luckperms/common/util/PaginatedTest.java @@ -0,0 +1,45 @@ +package me.lucko.luckperms.common.util; + +import com.google.common.collect.ImmutableList; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class PaginatedTest { + + @Test + void testSimple() { + Paginated paginated = new Paginated<>(ImmutableList.of("one", "two", "three", "four", "five")); + assertEquals(3, paginated.getMaxPages(2)); + assertEquals(1, paginated.getMaxPages(5)); + assertEquals(1, paginated.getMaxPages(6)); + + List> page1 = paginated.getPage(1, 2); + assertEquals(2, page1.size()); + assertEquals("one", page1.get(0).value()); + assertEquals(1, page1.get(0).position()); + assertEquals("two", page1.get(1).value()); + assertEquals(2, page1.get(1).position()); + + List> page2 = paginated.getPage(2, 2); + assertEquals(2, page2.size()); + assertEquals("three", page2.get(0).value()); + assertEquals(3, page2.get(0).position()); + assertEquals("four", page2.get(1).value()); + assertEquals(4, page2.get(1).position()); + + List> page3 = paginated.getPage(3, 2); + assertEquals(1, page3.size()); + assertEquals("five", page3.get(0).value()); + assertEquals(5, page3.get(0).position()); + + assertThrows(IllegalArgumentException.class, () -> paginated.getPage(4, 2)); + assertThrows(IllegalArgumentException.class, () -> paginated.getPage(0, 2)); + assertThrows(IllegalArgumentException.class, () -> paginated.getPage(-1, 2)); + } + +} diff --git a/common/src/test/java/me/lucko/luckperms/common/verbose/BooleanExpressionTest.java b/common/src/test/java/me/lucko/luckperms/common/verbose/BooleanExpressionTest.java new file mode 100644 index 000000000..1dd5d35cb --- /dev/null +++ b/common/src/test/java/me/lucko/luckperms/common/verbose/BooleanExpressionTest.java @@ -0,0 +1,28 @@ +package me.lucko.luckperms.common.verbose; + +import me.lucko.luckperms.common.verbose.expression.BooleanExpressionCompiler; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BooleanExpressionTest { + + private static void test(String expression, boolean expected) { + assertEquals( + expected, + BooleanExpressionCompiler.compile(expression).eval(var -> var.equals("true")), + expression + " is not " + expected + ); + } + + @Test + void testBrackets() { + test("false & false | true", true); + test("false & (false | true)", false); + test("true | false & false", true); + test("(true | false) & false", false); + test("(true & ((true | false) & !(true & false)))", true); + } + +}