Skip to content

Commit

Permalink
begin moving tests to commonTest
Browse files Browse the repository at this point in the history
  • Loading branch information
luca992 committed Aug 16, 2023
1 parent e559393 commit 1e01007
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 34 deletions.
1 change: 1 addition & 0 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ kotlin {
// need to make a separate module to import the resources because moko resources doesn't commonTest
// resources yet: https://github.com/icerockdev/moko-resources/issues/193
implementation(project(":library-test-resources"))
implementation(kotlin("test"))
}
}
val jvmCommonMain by creating {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@
*/
package io.michaelrocks.libphonenumber.kotlin

import io.michaelrocks.libphonenumber.kotlin.PhoneNumberMatch
import io.michaelrocks.libphonenumber.kotlin.Phonenumber.PhoneNumber
import junit.framework.TestCase
import kotlin.test.*

/**
* Tests for [PhoneNumberMatch].
*/
class PhoneNumberMatchTest : TestCase() {
class PhoneNumberMatchTest {
/**
* Tests the value type semantics. Equality and hash code must be based on the covered range and
* corresponding phone number. Range and number correctness are tested by
* [PhoneNumberMatcherTest].
*/
@Throws(Exception::class)
@Test
fun testValueTypeSemantics() {
val number = PhoneNumber()
val match1 = PhoneNumberMatch(10, "1 800 234 45 67", number)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ package io.michaelrocks.libphonenumber.kotlin

import io.michaelrocks.libphonenumber.kotlin.Phonenumber.PhoneNumber
import io.michaelrocks.libphonenumber.kotlin.Phonenumber.PhoneNumber.CountryCodeSource
import junit.framework.TestCase
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse

/**
* Tests for the Phonenumber.PhoneNumber object itself.
*
* @author Lara Rennie
*/
class PhonenumberTest : TestCase() {
@Throws(Exception::class)
class PhonenumberTest {
@Test
fun testEqualSimpleNumber() {
val numberA = PhoneNumber()
numberA.setCountryCode(1).setNationalNumber(6502530000L)
Expand All @@ -36,7 +38,7 @@ class PhonenumberTest : TestCase() {
assertEquals(numberA.hashCode(), numberB.hashCode())
}

@Throws(Exception::class)
@Test
fun testEqualWithItalianLeadingZeroSetToDefault() {
val numberA = PhoneNumber()
numberA.setCountryCode(1).setNationalNumber(6502530000L).setItalianLeadingZero(false)
Expand All @@ -48,7 +50,7 @@ class PhonenumberTest : TestCase() {
assertEquals(numberA.hashCode(), numberB.hashCode())
}

@Throws(Exception::class)
@Test
fun testEqualWithCountryCodeSourceSet() {
val numberA = PhoneNumber()
numberA.setRawInput("+1 650 253 00 00").setCountryCodeSource(CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN)
Expand All @@ -58,7 +60,7 @@ class PhonenumberTest : TestCase() {
assertEquals(numberA.hashCode(), numberB.hashCode())
}

@Throws(Exception::class)
@Test
fun testNonEqualWithItalianLeadingZeroSetToTrue() {
val numberA = PhoneNumber()
numberA.setCountryCode(1).setNationalNumber(6502530000L).setItalianLeadingZero(true)
Expand All @@ -82,7 +84,7 @@ class PhonenumberTest : TestCase() {
assertFalse(numberA.hashCode() == numberB.hashCode())
}

@Throws(Exception::class)
@Test
fun testNonEqualWithPreferredDomesticCarrierCodeSetToDefault() {
val numberA = PhoneNumber()
numberA.setCountryCode(1).setNationalNumber(6502530000L).setPreferredDomesticCarrierCode("")
Expand All @@ -92,7 +94,7 @@ class PhonenumberTest : TestCase() {
assertFalse(numberA.hashCode() == numberB.hashCode())
}

@Throws(Exception::class)
@Test
fun testEqualWithPreferredDomesticCarrierCodeSetToDefault() {
val numberA = PhoneNumber()
numberA.setCountryCode(1).setNationalNumber(6502530000L).setPreferredDomesticCarrierCode("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,32 @@
package io.michaelrocks.libphonenumber.kotlin.internal

import io.michaelrocks.libphonenumber.kotlin.internal.GeoEntityUtility.isGeoEntity
import junit.framework.TestCase
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class GeoEntityUtilityTest : TestCase() {
class GeoEntityUtilityTest {
@Test
fun test_isGeoEntity_shouldReturnTrueForCountryRegionCode() {
assertTrue(isGeoEntity("DE"))
}

@Test
fun test_isGeoEntity_shouldReturnFalseForWorldRegionCode() {
assertFalse(isGeoEntity("001"))
}

@Test
fun test_isGeoEntity_shouldReturnTrueForCountryCallingCode() {
assertTrue(isGeoEntity(41))
}

@Test
fun test_isGeoEntity_shouldReturnFalseForInternationalSharedCostServiceCallingCode() {
assertFalse(isGeoEntity(808))
}

@Test
fun test_isGeoEntity_shouldReturnFalseForNonExistingCountryCallingCode() {
assertFalse(isGeoEntity(111111111))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ package io.michaelrocks.libphonenumber.kotlin.internal

import io.michaelrocks.libphonenumber.kotlin.Phonemetadata.PhoneNumberDesc
import io.michaelrocks.libphonenumber.kotlin.Phonemetadata.PhoneNumberDesc.Companion.newBuilder
import io.michaelrocks.libphonenumber.kotlin.internal.MatcherApi
import io.michaelrocks.libphonenumber.kotlin.internal.RegexBasedMatcher.Companion.create
import junit.framework.TestCase
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

/**
* Tests that all implementations of `MatcherApi` are consistent.
*/
class MatcherTest : TestCase() {
class MatcherTest {

@Test
fun testRegexBasedMatcher() {
checkMatcherBehavesAsExpected(create())
}
Expand Down Expand Up @@ -65,34 +68,34 @@ class MatcherTest : TestCase() {

private fun assertMatched(matcher: MatcherApi, number: String, desc: PhoneNumberDesc) {
assertTrue(
String.format("%s should have matched %s.", number, toString(desc)),
matcher.matchNationalNumber(number, desc, false)
matcher.matchNationalNumber(number, desc, false),
"$number should have matched ${toString(desc)}."
)
assertTrue(
String.format("%s should have matched %s.", number, toString(desc)),
matcher.matchNationalNumber(number, desc, true)
matcher.matchNationalNumber(number, desc, true),
"$number should have matched ${toString(desc)}."
)
}

private fun assertInvalid(matcher: MatcherApi, number: String, desc: PhoneNumberDesc) {
assertFalse(
String.format("%s should not have matched %s.", number, toString(desc)),
matcher.matchNationalNumber(number, desc, false)
matcher.matchNationalNumber(number, desc, false),
"$number should not have matched ${toString(desc)}."
)
assertFalse(
String.format("%s should not have matched %s.", number, toString(desc)),
matcher.matchNationalNumber(number, desc, true)
matcher.matchNationalNumber(number, desc, true),
"$number should not have matched ${toString(desc)}."
)
}

private fun assertTooLong(matcher: MatcherApi, number: String, desc: PhoneNumberDesc) {
assertFalse(
String.format("%s should have been too long for %s.", number, toString(desc)),
matcher.matchNationalNumber(number, desc, false)
matcher.matchNationalNumber(number, desc, false),
"$number should have been too long for ${toString(desc)}."
)
assertTrue(
String.format("%s should have been too long for %s.", number, toString(desc)),
matcher.matchNationalNumber(number, desc, true)
matcher.matchNationalNumber(number, desc, true),
"$number should have been too long for ${toString(desc)}."
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
*/
package io.michaelrocks.libphonenumber.kotlin.internal

import io.michaelrocks.libphonenumber.kotlin.internal.RegexCache
import junit.framework.TestCase
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

/**
* Unittests for LRU Cache for compiled regular expressions used by the libphonenumbers libary.
*
* @author Shaopeng Jia
*/
class RegexCacheTest : TestCase() {
class RegexCacheTest {
private val regexCache: RegexCache = RegexCache(2)

@Test
fun testRegexInsertion() {
val regex1 = "[1-5]"
val regex2 = "(?:12|34)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package io.michaelrocks.libphonenumber.kotlin

import io.michaelrocks.libphonenumber.kotlin.CountryCodeToRegionCodeMapForTesting.countryCodeToRegionCodeMap
import io.michaelrocks.libphonenumber.kotlin.RegionCode
import io.michaelrocks.libphonenumber.kotlin.TestMetadataTestCase
import io.michaelrocks.libphonenumber.kotlin.PhoneNumberUtil.*
import io.michaelrocks.libphonenumber.kotlin.PhoneNumberUtil.Companion.extractPossibleNumber
import io.michaelrocks.libphonenumber.kotlin.PhoneNumberUtil.Companion.getCountryMobileToken
Expand Down

0 comments on commit 1e01007

Please sign in to comment.