Skip to content

Commit

Permalink
Add test for regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
wkal-pubnub committed Oct 3, 2024
1 parent 6be257f commit 52449c5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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://"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 52449c5

Please sign in to comment.