From: David ‘Bombe’ Roden Date: Tue, 7 Apr 2020 08:28:10 +0000 (+0200) Subject: 🎨 Replace test for default option with Kotlin version X-Git-Tag: v82^2~33 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=150dafbd9a3f63ca06e6b53002723b56ec7f3d05;p=Sone.git 🎨 Replace test for default option with Kotlin version --- 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 index 3c82c40..0000000 --- a/src/test/java/net/pterodactylus/sone/utils/DefaultOptionTest.java +++ /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 matchesAcceptedValue = new Predicate() { - @Override - public boolean apply(@Nullable Object object) { - return acceptedValue.equals(object); - } - }; - - @Test - public void defaultOptionReturnsDefaultValueWhenUnset() { - DefaultOption defaultOption = new DefaultOption<>(defaultValue); - assertThat(defaultOption.get(), is(defaultValue)); - } - - @Test - public void defaultOptionReturnsNullForRealWhenUnset() { - DefaultOption defaultOption = new DefaultOption<>(defaultValue); - assertThat(defaultOption.getReal(), nullValue()); - } - - @Test - public void defaultOptionWillReturnSetValue() { - DefaultOption defaultOption = new DefaultOption<>(defaultValue); - Object newValue = new Object(); - defaultOption.set(newValue); - assertThat(defaultOption.get(), is(newValue)); - } - - @Test - public void defaultOptionWithValidatorAcceptsValidValues() { - DefaultOption defaultOption = new DefaultOption<>(defaultValue, matchesAcceptedValue); - defaultOption.set(acceptedValue); - assertThat(defaultOption.get(), is(acceptedValue)); - } - - @Test(expected = IllegalArgumentException.class) - public void defaultOptionWithValidatorRejectsInvalidValues() { - DefaultOption defaultOption = new DefaultOption<>(defaultValue, matchesAcceptedValue); - defaultOption.set(new Object()); - } - - @Test - public void defaultOptionValidatesObjectsCorrectly() { - DefaultOption defaultOption = new DefaultOption<>(defaultValue, matchesAcceptedValue); - assertThat(defaultOption.validate(acceptedValue), is(true)); - assertThat(defaultOption.validate(new Object()), is(false)); - } - - @Test - public void settingToNullWillRestoreDefaultValue() { - DefaultOption defaultOption = new DefaultOption<>(defaultValue); - defaultOption.set(null); - assertThat(defaultOption.get(), is(defaultValue)); - } - - @Test - public void validateWithoutValidatorWillValidateNull() { - DefaultOption defaultOption = new DefaultOption<>(defaultValue); - assertThat(defaultOption.validate(null), is(true)); - } - - @Test - public void validateWithValidatorWillValidateNull() { - DefaultOption 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 index 0000000..f7f4a49 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/utils/DefaultOptionTest.kt @@ -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 { 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)) + } + +}