Skip to content

Commit

Permalink
tests: add null and empty checks
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Mar 11, 2024
1 parent 78e0195 commit a486da0
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/test/java/io/github/brenoepics/MentionExtractorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,66 @@ class MentionExtractorTest {

assertEquals(setOf("user1", "user2"), mentions)
}

@Test
fun `extract mentions from null text`() {
val pattern = MentionPattern().defaultPattern()
val extractor = MentionExtractor.Builder().pattern(pattern).build()

val text = null
val mentions = extractor.fromString(text)

assertTrue(mentions.isEmpty())
}

@Test
fun `extract mentions from empty text`() {
val pattern = MentionPattern().defaultPattern()
val extractor = MentionExtractor.Builder().pattern(pattern).build()

val text = ""
val mentions = extractor.fromString(text)

assertTrue(mentions.isEmpty())
}

@Test
fun `check if null text contains mention`() {
val pattern = MentionPattern().defaultPattern()
val extractor = MentionExtractor.Builder().pattern(pattern).build()

val text = null

assertFalse(extractor.containsMention(text))
}

@Test
fun `check if empty text contains mention`() {
val pattern = MentionPattern().defaultPattern()
val extractor = MentionExtractor.Builder().pattern(pattern).build()

val text = ""

assertFalse(extractor.containsMention(text))
}

@Test
fun `count mentions in null text`() {
val pattern = MentionPattern().defaultPattern()
val extractor = MentionExtractor.Builder().pattern(pattern).build()

val text = null

assertEquals(0, extractor.countMentions(text))
}

@Test
fun `count mentions in empty text`() {
val pattern = MentionPattern().defaultPattern()
val extractor = MentionExtractor.Builder().pattern(pattern).build()

val text = ""

assertEquals(0, extractor.countMentions(text))
}
}
41 changes: 41 additions & 0 deletions src/test/java/io/github/brenoepics/MentionPatternTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.brenoepics

import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import kotlin.test.assertFalse

class MentionPatternTest {

Expand Down Expand Up @@ -30,6 +31,16 @@ class MentionPatternTest {
assertTrue(pattern.matcher("@user@example.net").matches())
}

@Test
fun `withUnderscores should match mentions with underscores`() {
val pattern = MentionPattern().withUnderscores().build()
assertTrue(pattern.matcher("@user_name").matches())
assertTrue(pattern.matcher("@username").matches())
assertTrue(!pattern.matcher("@user-name").matches())
assertTrue(pattern.matcher("@username123").matches())
assertTrue(pattern.matcher("@user_name_123").matches())
}

@Test
fun `defaultPattern should match any word character after '@'`() {
val pattern = MentionPattern().defaultPattern().build()
Expand All @@ -49,4 +60,34 @@ class MentionPatternTest {
assertTrue(pattern.matcher("@username123").matches())
assertTrue(pattern.matcher("@user_name_123").matches())
}

@Test
fun `defaultPattern should not match null or empty strings`() {
val pattern = MentionPattern().defaultPattern().build()
assertFalse(pattern.matcher("").matches())
}

@Test
fun `withSpecialChars should not match null or empty strings`() {
val pattern = MentionPattern().withSpecialChars().build()
assertFalse(pattern.matcher("").matches())
}

@Test
fun `withEmailLike should not match null or empty strings`() {
val pattern = MentionPattern().withEmailLike().build()
assertFalse(pattern.matcher("").matches())
}

@Test
fun `withUnderscores should not match null or empty strings`() {
val pattern = MentionPattern().withUnderscores().build()
assertFalse(pattern.matcher("").matches())
}

@Test
fun `customPattern should not match null or empty strings`() {
val pattern = MentionPattern().customPattern("@([A-Za-z0-9_]+)").build()
assertFalse(pattern.matcher("").matches())
}
}

0 comments on commit a486da0

Please sign in to comment.