From 5a54ea664e862c910b1766d8ba0dd5efb6ea0151 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 3 Jun 2017 18:08:04 +0200 Subject: [PATCH] Create Guava optional utility collection --- .../net/pterodactylus/sone/utils/Optionals.kt | 7 ++++ .../pterodactylus/sone/fcp/GetPostCommandTest.kt | 2 +- .../sone/fcp/GetPostFeedCommandTest.kt | 2 +- .../pterodactylus/sone/fcp/GetPostsCommandTest.kt | 2 +- .../pterodactylus/sone/fcp/GetSoneCommandTest.kt | 2 +- .../pterodactylus/sone/fcp/LikePostCommandTest.kt | 2 +- .../pterodactylus/sone/fcp/LikeReplyCommandTest.kt | 2 +- .../pterodactylus/sone/fcp/LockSoneCommandTest.kt | 2 +- .../net/pterodactylus/sone/fcp/SoneCommandTest.kt | 2 +- .../sone/fcp/UnlockSoneCommandTest.kt | 2 +- .../kotlin/net/pterodactylus/sone/test/Guava.kt | 7 ---- .../net/pterodactylus/sone/utils/OptionalsTest.kt | 38 ++++++++++++++++++++++ .../pterodactylus/sone/web/ajax/JsonPageTest.kt | 2 +- .../sone/web/pages/CreatePostPageTest.kt | 2 +- .../pterodactylus/sone/web/pages/NewPageTest.kt | 2 +- .../pterodactylus/sone/web/pages/SearchPageTest.kt | 2 +- .../sone/web/pages/ViewSonePageTest.kt | 2 +- 17 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt delete mode 100644 src/test/kotlin/net/pterodactylus/sone/test/Guava.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt new file mode 100644 index 0000000..35487ed --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Optionals.kt @@ -0,0 +1,7 @@ +package net.pterodactylus.sone.utils + +import com.google.common.base.Optional + +fun Optional.let(block: (T) -> R): R? = if (isPresent) block(get()) else null + +fun T?.asOptional(): Optional = this?.let { Optional.of(it) } ?: Optional.absent() diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostCommandTest.kt index 1b4fa2e..b7f911c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostCommandTest.kt @@ -3,9 +3,9 @@ package net.pterodactylus.sone.fcp import freenet.support.SimpleFieldSet import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.data.Sone -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.containsInAnyOrder import org.hamcrest.Matchers.equalTo diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.kt index b972a53..72daa07 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostFeedCommandTest.kt @@ -2,8 +2,8 @@ package net.pterodactylus.sone.fcp import freenet.support.SimpleFieldSet import net.pterodactylus.sone.core.Core -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.containsInAnyOrder import org.hamcrest.Matchers.equalTo diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostsCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostsCommandTest.kt index d90ec1c..6483085 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostsCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/GetPostsCommandTest.kt @@ -2,8 +2,8 @@ package net.pterodactylus.sone.fcp import freenet.support.SimpleFieldSet import net.pterodactylus.sone.core.Core -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.containsInAnyOrder import org.hamcrest.Matchers.equalTo diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt index 710a5df..e73160b 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/GetSoneCommandTest.kt @@ -2,8 +2,8 @@ package net.pterodactylus.sone.fcp import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.freenet.fcp.FcpException -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.hamcrest.Matchers.nullValue diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt index 86fb586..54f784f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/LikePostCommandTest.kt @@ -3,9 +3,9 @@ package net.pterodactylus.sone.fcp import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.freenet.fcp.FcpException -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.Before diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt index d222c27..69ace0a 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/LikeReplyCommandTest.kt @@ -4,9 +4,9 @@ import net.pterodactylus.sone.core.Core import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.freenet.fcp.FcpException -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.Before diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/LockSoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/LockSoneCommandTest.kt index 1588bfe..1caea96 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/LockSoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/LockSoneCommandTest.kt @@ -1,8 +1,8 @@ package net.pterodactylus.sone.fcp import net.pterodactylus.sone.core.Core -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.Before diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt index f3994c8..dfed710 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/SoneCommandTest.kt @@ -11,9 +11,9 @@ import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.freenet.fcp.FcpException import net.pterodactylus.sone.template.SoneAccessor import net.pterodactylus.sone.test.OneByOneMatcher -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import org.junit.Before import org.junit.Rule import org.junit.rules.ExpectedException diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.kt index 5edc03e..c002db3 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/UnlockSoneCommandTest.kt @@ -1,8 +1,8 @@ package net.pterodactylus.sone.fcp import net.pterodactylus.sone.core.Core -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo import org.junit.Before diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Guava.kt b/src/test/kotlin/net/pterodactylus/sone/test/Guava.kt deleted file mode 100644 index 076962b..0000000 --- a/src/test/kotlin/net/pterodactylus/sone/test/Guava.kt +++ /dev/null @@ -1,7 +0,0 @@ -package net.pterodactylus.sone.test - -import com.google.common.base.Optional -import com.google.common.base.Optional.absent -import com.google.common.base.Optional.of - -fun T?.asOptional(): Optional = if (this == null) absent() else of(this) diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt new file mode 100644 index 0000000..2a49db0 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/utils/OptionalsTest.kt @@ -0,0 +1,38 @@ +package net.pterodactylus.sone.utils + +import com.google.common.base.Optional +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.nullValue +import org.junit.Test + +/** + * Test for [Optional] utils. + */ +class OptionalsTest { + + @Test + fun `present optional can be transformed with let`() { + val optional = Optional.of(1) + assertThat(optional.let { it + 1 }, equalTo(2)) + } + + @Test + fun `empty optional is transform to null with let`() { + val optional = Optional.absent() + assertThat(optional.let { it + 1 }, nullValue()) + } + + @Test + fun `1 as optional is correct optional`() { + val optional = 1.asOptional() + assertThat(optional.get(), equalTo(1)) + } + + @Test + fun `null as optional is asent optional`() { + val optional = null.asOptional() + assertThat(optional.isPresent, equalTo(false)) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt index e719c06..11d5291 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonPageTest.kt @@ -11,10 +11,10 @@ import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.data.Sone.SoneStatus import net.pterodactylus.sone.data.Sone.SoneStatus.idle -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.deepMock import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.sone.web.WebInterface import net.pterodactylus.sone.web.page.FreenetRequest import net.pterodactylus.util.notify.Notification diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt index 657c0ce..4cb6a8c 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt @@ -2,8 +2,8 @@ package net.pterodactylus.sone.web.pages import com.google.common.base.Optional.absent import net.pterodactylus.sone.data.Sone -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.utils.asOptional import net.pterodactylus.util.web.Method.POST import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt index d47560b..67f7811 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/NewPageTest.kt @@ -2,10 +2,10 @@ package net.pterodactylus.sone.web.pages import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever import net.pterodactylus.sone.utils.Pagination +import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.contains import org.hamcrest.Matchers.containsInAnyOrder diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt index a77ffcc..540b8b5 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/SearchPageTest.kt @@ -8,10 +8,10 @@ import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.data.Profile import net.pterodactylus.sone.data.Sone -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.isOnPage import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.contains import org.hamcrest.Matchers.equalTo diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt index 7161684..6850cf1 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt @@ -4,9 +4,9 @@ import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.data.Profile import net.pterodactylus.sone.data.Sone -import net.pterodactylus.sone.test.asOptional import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.contains import org.hamcrest.Matchers.equalTo -- 2.7.4