From c1e55a58963febbad2bd171b44366ad525bd9a42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Kalici=C5=84ski?= Date: Wed, 7 Aug 2024 09:42:35 +0200 Subject: [PATCH] Operator fun plus and tests --- .../commonMain/kotlin/com/pubnub/chat/Channel.kt | 2 +- .../src/commonMain/kotlin/com/pubnub/chat/User.kt | 2 +- .../kotlin/com/pubnub/chat/internal/UserImpl.kt | 2 +- .../com/pubnub/chat/internal/channel/BaseChannel.kt | 2 +- .../commonTest/kotlin/com/pubnub/kmp/ChannelTest.kt | 13 +++++++++++++ .../commonTest/kotlin/com/pubnub/kmp/UserTest.kt | 13 +++++++++++++ 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt index cdf86f25..7387beb6 100644 --- a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt +++ b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/Channel.kt @@ -132,7 +132,7 @@ interface Channel { /** * Get a new `Channel` instance that is a copy of this `Channel` with its properties updated with information coming from `update`. */ - fun plus(update: PNChannelMetadata): Channel + operator fun plus(update: PNChannelMetadata): Channel // Companion object required for extending this class elsewhere companion object diff --git a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/User.kt b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/User.kt index f3626f51..2d912ff9 100644 --- a/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/User.kt +++ b/pubnub-chat-api/src/commonMain/kotlin/com/pubnub/chat/User.kt @@ -71,7 +71,7 @@ interface User { /** * Get a new `User` instance that is a copy of this `User` with its properties updated with information coming from `update`. */ - fun plus(update: PNUUIDMetadata): User + operator fun plus(update: PNUUIDMetadata): User companion object } diff --git a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/UserImpl.kt b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/UserImpl.kt index 3aeddf23..370dc383 100644 --- a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/UserImpl.kt +++ b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/UserImpl.kt @@ -190,7 +190,7 @@ data class UserImpl( return chat.emitEvent(channelId = INTERNAL_ADMIN_CHANNEL, payload = payload) } - override fun plus(update: PNUUIDMetadata): User { + override operator fun plus(update: PNUUIDMetadata): User { return fromDTO(chat, toUUIDMetadata() + update) } diff --git a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt index 7d62dcac..3cd77e81 100644 --- a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt +++ b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/channel/BaseChannel.kt @@ -671,7 +671,7 @@ abstract class BaseChannel( ) } - override fun plus(update: PNChannelMetadata): Channel { + override operator fun plus(update: PNChannelMetadata): Channel { return channelFactory(chat, toPNChannelMetadata() + update) } diff --git a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/ChannelTest.kt b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/ChannelTest.kt index e93356f4..f8c755da 100644 --- a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/ChannelTest.kt +++ b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/ChannelTest.kt @@ -14,7 +14,9 @@ import com.pubnub.api.models.consumer.history.PNFetchMessagesResult import com.pubnub.api.models.consumer.objects.PNMemberKey import com.pubnub.api.models.consumer.objects.PNPage import com.pubnub.api.models.consumer.objects.PNSortKey +import com.pubnub.api.models.consumer.objects.channel.PNChannelMetadata import com.pubnub.api.models.consumer.objects.member.PNUUIDDetailsLevel +import com.pubnub.api.utils.PatchValue import com.pubnub.api.v2.callbacks.Consumer import com.pubnub.api.v2.callbacks.Result import com.pubnub.api.v2.createPNConfiguration @@ -35,6 +37,7 @@ import com.pubnub.chat.types.MessageMentionedUser import com.pubnub.chat.types.MessageReferencedChannel import com.pubnub.kmp.utils.BaseTest import com.pubnub.test.await +import com.pubnub.test.randomString import dev.mokkery.MockMode import dev.mokkery.answering.calls import dev.mokkery.answering.returns @@ -765,6 +768,16 @@ class ChannelTest : BaseTest() { verify { chat.updateChannel(channelId, name, custom, description, status, type) } } + + @Test + fun plus() { + val channel = createChannel(ChannelType.PUBLIC) + val expectedChannel = channel.copy(name = randomString(), description = randomString()) + + val newChannel = channel + PNChannelMetadata(expectedChannel.id, name = PatchValue.of(expectedChannel.name), description = PatchValue.of(expectedChannel.description)) + + assertEquals(expectedChannel, newChannel) + } } private operator fun Any?.get(s: String): Any? { diff --git a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/UserTest.kt b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/UserTest.kt index ee5283da..15fe4b7a 100644 --- a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/UserTest.kt +++ b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/UserTest.kt @@ -9,6 +9,8 @@ import com.pubnub.api.models.consumer.objects.channel.PNChannelMetadata import com.pubnub.api.models.consumer.objects.membership.PNChannelDetailsLevel import com.pubnub.api.models.consumer.objects.membership.PNChannelMembership import com.pubnub.api.models.consumer.objects.membership.PNChannelMembershipArrayResult +import com.pubnub.api.models.consumer.objects.uuid.PNUUIDMetadata +import com.pubnub.api.utils.PatchValue import com.pubnub.api.v2.PNConfiguration import com.pubnub.api.v2.callbacks.Consumer import com.pubnub.api.v2.callbacks.Result @@ -18,6 +20,7 @@ import com.pubnub.chat.internal.ChatInternal import com.pubnub.chat.internal.UserImpl import com.pubnub.chat.internal.channel.ChannelImpl import com.pubnub.kmp.utils.FakeChat +import com.pubnub.test.randomString import dev.mokkery.MockMode import dev.mokkery.answering.calls import dev.mokkery.answering.returns @@ -354,6 +357,16 @@ class UserTest { } } + @Test + fun plus() { + val user = createUser(chat) + val expectedUser = user.copy(name = randomString(), email = randomString()) + + val newUser = user + PNUUIDMetadata(expectedUser.id, name = PatchValue.of(expectedUser.name), email = PatchValue.of(expectedUser.email)) + + assertEquals(expectedUser, newUser) + } + private fun getPNChannelMembershipArrayResult(): PNChannelMembershipArrayResult { val channelMetadata = PNChannelMetadata( id = channelId,