Skip to content

Commit

Permalink
Operator fun plus and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wkal-pubnub committed Aug 7, 2024
1 parent acd7c95 commit c1e55a5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ abstract class BaseChannel<C : Channel, M : Message>(
)
}

override fun plus(update: PNChannelMetadata): Channel {
override operator fun plus(update: PNChannelMetadata): Channel {
return channelFactory(chat, toPNChannelMetadata() + update)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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? {
Expand Down
13 changes: 13 additions & 0 deletions pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/UserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c1e55a5

Please sign in to comment.