From 52449c5344351eefb3c4c90421f2deb8aea6f3d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Kalici=C5=84ski?= Date: Thu, 3 Oct 2024 19:04:40 +0200 Subject: [PATCH] Add test for regexes --- .../pubnub/chat/internal/MessageDraftImpl.kt | 4 +- .../kotlin/com/pubnub/kmp/MessageDraftTest.kt | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MessageDraftImpl.kt b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MessageDraftImpl.kt index 07179def..64ebba81 100644 --- a/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MessageDraftImpl.kt +++ b/pubnub-chat-impl/src/commonMain/kotlin/com/pubnub/chat/internal/MessageDraftImpl.kt @@ -20,8 +20,8 @@ import kotlinx.atomicfu.update import name.fraser.neil.plaintext.DiffMatchPatch import kotlin.math.min -private val userMentionRegex = Regex("""(?U)(?<=^|\p{Space})(@[\p{L}-]+)""") -private val channelReferenceRegex = Regex("""(?U)(?<=^|\p{Space})(#[\p{LD}-]+)""") +internal val userMentionRegex = Regex("""(?U)(?<=^|\p{Space})(@[\p{Alpha}\-]+)""") +internal val channelReferenceRegex = Regex("""(?U)(?<=^|\p{Space})(#[\p{Alnum}\-]+)""") private const val SCHEMA_USER = "pn-user://" private const val SCHEMA_CHANNEL = "pn-channel://" diff --git a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MessageDraftTest.kt b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MessageDraftTest.kt index a82727f4..546e27b3 100644 --- a/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MessageDraftTest.kt +++ b/pubnub-chat-impl/src/commonTest/kotlin/com/pubnub/kmp/MessageDraftTest.kt @@ -14,6 +14,8 @@ import com.pubnub.chat.internal.Mention import com.pubnub.chat.internal.MessageDraftImpl import com.pubnub.chat.internal.UserImpl import com.pubnub.chat.internal.channel.ChannelImpl +import com.pubnub.chat.internal.channelReferenceRegex +import com.pubnub.chat.internal.userMentionRegex import com.pubnub.test.await import dev.mokkery.MockMode import dev.mokkery.answering.returns @@ -390,4 +392,50 @@ class MessageDraftTest { draft.getMessageElements() ) } + + // TODO iOS fails on diacritics (ęąść..) + // when JS is also enabled, we'll fix all platform's regexes at the same time + @Test + fun test_user_mention_regexes() { + val stringsToExpectedMatches = listOf( + "@user" to "@user", + "@us()er" to "@us", + "@user @fsjdoif" to "@user", + "@123aaa @user" to "@user", + "@user aaa" to "@user", + "aaa @user aaa" to "@user", + "aaa @user," to "@user", + "aaa @user-user aaa" to "@user-user", + "@user-user" to "@user-user", + "@brzęczy" to "@brzęczy", + "@ąśćżć" to "@ąśćżć", + ) + + stringsToExpectedMatches.forEach { + assertEquals(it.second, userMentionRegex.find(it.first)!!.value) + } + } + + // TODO iOS fails on diacritics (ęąść..) + // when JS is also enabled, we'll fix all platform's regexes at the same time + @Test + fun test_channel_mention_regexes() { + val stringsToExpectedMatches = listOf( + "#user" to "#user", + "#us()er" to "#us", + "#user #fsjdoif" to "#user", + "#123aaa #user" to "#123aaa", + "#user aaa" to "#user", + "aaa #user aaa" to "#user", + "aaa #user," to "#user", + "aaa #user-user aaa" to "#user-user", + "#user-user" to "#user-user", + "#brzęczy" to "#brzęczy", + "#ąśćżć" to "#ąśćżć", + ) + + stringsToExpectedMatches.forEach { + assertEquals(it.second, channelReferenceRegex.find(it.first)!!.value) + } + } }