🎨 Replace test for default option with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 7 Apr 2020 08:28:10 +0000 (10:28 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 7 Apr 2020 08:28:10 +0000 (10:28 +0200)
src/test/java/net/pterodactylus/sone/utils/DefaultOptionTest.java [deleted file]
src/test/kotlin/net/pterodactylus/sone/utils/DefaultOptionTest.kt [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/utils/DefaultOptionTest.java b/src/test/java/net/pterodactylus/sone/utils/DefaultOptionTest.java
deleted file mode 100644 (file)
index 3c82c40..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-package net.pterodactylus.sone.utils;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-
-import javax.annotation.Nullable;
-
-import com.google.common.base.Predicate;
-import org.junit.Test;
-
-/**
- * Unit test for {@link DefaultOption}.
- */
-public class DefaultOptionTest {
-
-       private final Object defaultValue = new Object();
-       private final Object acceptedValue = new Object();
-       private final Predicate<Object> matchesAcceptedValue = new Predicate<Object>() {
-               @Override
-               public boolean apply(@Nullable Object object) {
-                       return acceptedValue.equals(object);
-               }
-       };
-
-       @Test
-       public void defaultOptionReturnsDefaultValueWhenUnset() {
-               DefaultOption<Object> defaultOption = new DefaultOption<>(defaultValue);
-               assertThat(defaultOption.get(), is(defaultValue));
-       }
-
-       @Test
-       public void defaultOptionReturnsNullForRealWhenUnset() {
-               DefaultOption<Object> defaultOption = new DefaultOption<>(defaultValue);
-               assertThat(defaultOption.getReal(), nullValue());
-       }
-
-       @Test
-       public void defaultOptionWillReturnSetValue() {
-               DefaultOption<Object> defaultOption = new DefaultOption<>(defaultValue);
-               Object newValue = new Object();
-               defaultOption.set(newValue);
-               assertThat(defaultOption.get(), is(newValue));
-       }
-
-       @Test
-       public void defaultOptionWithValidatorAcceptsValidValues() {
-               DefaultOption<Object> defaultOption = new DefaultOption<>(defaultValue, matchesAcceptedValue);
-               defaultOption.set(acceptedValue);
-               assertThat(defaultOption.get(), is(acceptedValue));
-       }
-
-       @Test(expected = IllegalArgumentException.class)
-       public void defaultOptionWithValidatorRejectsInvalidValues() {
-               DefaultOption<Object> defaultOption = new DefaultOption<>(defaultValue, matchesAcceptedValue);
-               defaultOption.set(new Object());
-       }
-
-       @Test
-       public void defaultOptionValidatesObjectsCorrectly() {
-               DefaultOption<Object> defaultOption = new DefaultOption<>(defaultValue, matchesAcceptedValue);
-               assertThat(defaultOption.validate(acceptedValue), is(true));
-               assertThat(defaultOption.validate(new Object()), is(false));
-       }
-
-       @Test
-       public void settingToNullWillRestoreDefaultValue() {
-               DefaultOption<Object> defaultOption = new DefaultOption<>(defaultValue);
-               defaultOption.set(null);
-               assertThat(defaultOption.get(), is(defaultValue));
-       }
-
-       @Test
-       public void validateWithoutValidatorWillValidateNull() {
-               DefaultOption<Object> defaultOption = new DefaultOption<>(defaultValue);
-               assertThat(defaultOption.validate(null), is(true));
-       }
-
-       @Test
-       public void validateWithValidatorWillValidateNull() {
-               DefaultOption<Object> defaultOption = new DefaultOption<>(defaultValue, matchesAcceptedValue);
-               assertThat(defaultOption.validate(null), is(true));
-       }
-
-}
diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/DefaultOptionTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/DefaultOptionTest.kt
new file mode 100644 (file)
index 0000000..f7f4a49
--- /dev/null
@@ -0,0 +1,78 @@
+package net.pterodactylus.sone.utils
+
+import com.google.common.base.Predicate
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.nullValue
+import org.hamcrest.Matchers.sameInstance
+import org.junit.Test
+
+/**
+ * Unit test for [DefaultOption].
+ */
+class DefaultOptionTest {
+
+       private val defaultValue = Any()
+       private val acceptedValue = Any()
+       private val matchesAcceptedValue = Predicate<Any?> { it == acceptedValue }
+
+       @Test
+       fun `default option returns default value when unset`() {
+               val defaultOption = DefaultOption(defaultValue)
+               assertThat(defaultOption.get(), sameInstance(defaultValue))
+       }
+
+       @Test
+       fun `default option returns null for real when unset`() {
+               val defaultOption = DefaultOption(defaultValue)
+               assertThat(defaultOption.real, nullValue())
+       }
+
+       @Test
+       fun `default option will return set value`() {
+               val defaultOption = DefaultOption(defaultValue)
+               val newValue = Any()
+               defaultOption.set(newValue)
+               assertThat(defaultOption.get(), sameInstance(newValue))
+       }
+
+       @Test
+       fun `default option with validator accepts valid values`() {
+               val defaultOption = DefaultOption(defaultValue, matchesAcceptedValue)
+               defaultOption.set(acceptedValue)
+               assertThat(defaultOption.get(), sameInstance(acceptedValue))
+       }
+
+       @Test(expected = IllegalArgumentException::class)
+       fun `default option with validator rejects invalid values`() {
+               val defaultOption = DefaultOption(defaultValue, matchesAcceptedValue)
+               defaultOption.set(Any())
+       }
+
+       @Test
+       fun `default option validates objects correctly`() {
+               val defaultOption = DefaultOption(defaultValue, matchesAcceptedValue)
+               assertThat(defaultOption.validate(acceptedValue), equalTo(true))
+               assertThat(defaultOption.validate(Any()), equalTo(false))
+       }
+
+       @Test
+       fun `setting to null will restore default value`() {
+               val defaultOption = DefaultOption(defaultValue)
+               defaultOption.set(null)
+               assertThat(defaultOption.get(), sameInstance(defaultValue))
+       }
+
+       @Test
+       fun `validate without validator will validate null`() {
+               val defaultOption = DefaultOption(defaultValue)
+               assertThat(defaultOption.validate(null), equalTo(true))
+       }
+
+       @Test
+       fun `validate with validator will validate null`() {
+               val defaultOption = DefaultOption(defaultValue, matchesAcceptedValue)
+               assertThat(defaultOption.validate(null), equalTo(true))
+       }
+
+}