mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-22 18:25:27 +01:00
Add tests for lazy tag replacement
This commit is contained in:
parent
a4b440bcca
commit
a2b8ca683d
@ -64,4 +64,10 @@ public class PlayerUtilsTest {
|
||||
// then
|
||||
assertThat(result, equalTo(name));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveHiddenConstructor() {
|
||||
// given / when / then
|
||||
TestHelper.validateHasOnlyPrivateEmptyConstructor(PlayerUtils.class);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package fr.xephi.authme.util;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
@ -68,4 +69,10 @@ public class RandomStringUtilsTest {
|
||||
// then - throw exception
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveHiddenConstructor() {
|
||||
// given / when / then
|
||||
TestHelper.validateHasOnlyPrivateEmptyConstructor(RandomStringUtils.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,50 @@
|
||||
package fr.xephi.authme.util.lazytags;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link TagBuilder}.
|
||||
*/
|
||||
public class TagBuilderTest {
|
||||
|
||||
@Test
|
||||
public void shouldCreateNoArgsTag() {
|
||||
// given
|
||||
Supplier<String> supplier = () -> "hello";
|
||||
|
||||
// when
|
||||
Tag<String> tag = TagBuilder.createTag("hey", supplier);
|
||||
|
||||
// then
|
||||
assertThat(tag, instanceOf(SimpleTag.class));
|
||||
assertThat(tag.getValue(null), equalTo("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateDependentTag() {
|
||||
// given
|
||||
Function<Double, String> function = d -> Double.toString(d + d/10);
|
||||
|
||||
// when
|
||||
Tag<Double> tag = TagBuilder.createTag("%test", function);
|
||||
|
||||
// then
|
||||
assertThat(tag, instanceOf(DependentTag.class));
|
||||
assertThat(tag.getValue(24d), equalTo("26.4"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveHiddenConstructor() {
|
||||
// given / when / then
|
||||
TestHelper.validateHasOnlyPrivateEmptyConstructor(TagBuilder.class);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package fr.xephi.authme.util.lazytags;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.util.lazytags.TagBuilder.createTag;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link TagReplacer}.
|
||||
*/
|
||||
public class TagReplacerTest {
|
||||
|
||||
@Test
|
||||
public void shouldReplaceTags() {
|
||||
// given
|
||||
TestTagService tagService = new TestTagService();
|
||||
List<Tag<Integer>> tags = tagService.getAvailableTags();
|
||||
List<String> messages = Arrays.asList("pi = %PI", "for i = %self, i^2 = %square", "%self %self %PI");
|
||||
|
||||
// when
|
||||
TagReplacer<Integer> tagReplacer = TagReplacer.newReplacer(tags, messages);
|
||||
List<String> result = tagReplacer.getAdaptedMessages(3);
|
||||
|
||||
// then
|
||||
assertThat(tagService.piCount, equalTo(1));
|
||||
assertThat(tagService.selfCount, equalTo(1));
|
||||
assertThat(tagService.doubleCount, equalTo(0));
|
||||
assertThat(tagService.squareCount, equalTo(1));
|
||||
assertThat(result, contains("pi = 3.14159", "for i = 3, i^2 = 9", "3 3 3.14159"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotCallUnusedTags() {
|
||||
// given
|
||||
TestTagService tagService = new TestTagService();
|
||||
List<Tag<Integer>> tags = tagService.getAvailableTags();
|
||||
List<String> messages = Arrays.asList("pi = %PI", "double i = %double");
|
||||
|
||||
// when
|
||||
TagReplacer<Integer> tagReplacer = TagReplacer.newReplacer(tags, messages);
|
||||
List<String> result1 = tagReplacer.getAdaptedMessages(-4);
|
||||
List<String> result2 = tagReplacer.getAdaptedMessages(0);
|
||||
|
||||
// then
|
||||
assertThat(tagService.piCount, equalTo(2));
|
||||
assertThat(tagService.selfCount, equalTo(0));
|
||||
assertThat(tagService.doubleCount, equalTo(2));
|
||||
assertThat(tagService.squareCount, equalTo(0));
|
||||
assertThat(result1, contains("pi = 3.14159", "double i = -8"));
|
||||
assertThat(result2, contains("pi = 3.14159", "double i = 0"));
|
||||
}
|
||||
|
||||
static final class TestTagService {
|
||||
int piCount, selfCount, doubleCount, squareCount;
|
||||
|
||||
String pi() {
|
||||
++piCount;
|
||||
return "3.14159";
|
||||
}
|
||||
|
||||
String self(int i) {
|
||||
++selfCount;
|
||||
return Integer.toString(i);
|
||||
}
|
||||
|
||||
String calcDouble(int i) {
|
||||
++doubleCount;
|
||||
return Integer.toString(2 * i);
|
||||
}
|
||||
|
||||
String calcSquare(int i) {
|
||||
++squareCount;
|
||||
return Integer.toString(i * i);
|
||||
}
|
||||
|
||||
List<Tag<Integer>> getAvailableTags() {
|
||||
return Arrays.asList(
|
||||
createTag("%PI", this::pi),
|
||||
createTag("%self", this::self),
|
||||
createTag("%double", this::calcDouble),
|
||||
createTag("%square", this::calcSquare));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package fr.xephi.authme.util.lazytags;
|
||||
|
||||
import fr.xephi.authme.util.lazytags.TagReplacerTest.TestTagService;
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.TypeSafeMatcher;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link WrappedTagReplacer}.
|
||||
*/
|
||||
public class WrappedTagReplacerTest {
|
||||
|
||||
@Test
|
||||
public void shouldApplyTags() {
|
||||
// given
|
||||
TestTagService tagService = new TestTagService();
|
||||
List<Tag<Integer>> tags = tagService.getAvailableTags();
|
||||
List<SampleClass> objects = Arrays.asList(
|
||||
new SampleClass(3, "pi is %PI"),
|
||||
new SampleClass(5, "no tags here"),
|
||||
new SampleClass(7, "i+i = %double"));
|
||||
|
||||
// when
|
||||
WrappedTagReplacer<SampleClass, Integer> replacer = new WrappedTagReplacer<>(
|
||||
tags, objects, SampleClass::getDescription, (o, s) -> new SampleClass(o.number, s));
|
||||
List<SampleClass> result1 = replacer.getAdaptedItems(8);
|
||||
List<SampleClass> result2 = replacer.getAdaptedItems(1);
|
||||
|
||||
// then
|
||||
assertThat(tagService.piCount, equalTo(2));
|
||||
assertThat(tagService.selfCount, equalTo(0));
|
||||
assertThat(tagService.doubleCount, equalTo(2));
|
||||
assertThat(tagService.squareCount, equalTo(0));
|
||||
assertThat(result1, contains(
|
||||
sampleClass(3, "pi is 3.14159"), sampleClass(5, "no tags here"), sampleClass(7, "i+i = 16")));
|
||||
assertThat(result2, contains(
|
||||
sampleClass(3, "pi is 3.14159"), sampleClass(5, "no tags here"), sampleClass(7, "i+i = 2")));
|
||||
}
|
||||
|
||||
|
||||
private static Matcher<SampleClass> sampleClass(int number, String description) {
|
||||
return new TypeSafeMatcher<SampleClass>() {
|
||||
@Override
|
||||
protected boolean matchesSafely(SampleClass item) {
|
||||
return number == item.number && description.equals(item.description);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("SampleClass[number=" + number + ";description=" + description + "]");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static final class SampleClass {
|
||||
private final int number;
|
||||
private final String description;
|
||||
|
||||
SampleClass(int number, String description) {
|
||||
this.number = number;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
String getDescription() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user