Skip to content

Commit

Permalink
✨ Add EmailAddress.Companion.qualifiedName property (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Apr 20, 2024
1 parent 4b37b0b commit 61b775c
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 1 deletion.
2 changes: 2 additions & 0 deletions subprojects/library/src/api/types.api
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ public final class org/kotools/types/EmailAddress {
public static final fun fromStringOrNull (Ljava/lang/Object;)Lorg/kotools/types/EmailAddress;
public static final fun fromStringOrNull (Ljava/lang/Object;Ljava/lang/Object;)Lorg/kotools/types/EmailAddress;
public final fun hashCode ()I
public static final fun qualifiedName ()Ljava/lang/String;
public final fun toString ()Ljava/lang/String;
}

Expand All @@ -457,6 +458,7 @@ public final class org/kotools/types/EmailAddress$Companion {
public final fun fromString (Ljava/lang/Object;Ljava/lang/Object;)Lorg/kotools/types/EmailAddress;
public final fun fromStringOrNull (Ljava/lang/Object;)Lorg/kotools/types/EmailAddress;
public final fun fromStringOrNull (Ljava/lang/Object;Ljava/lang/Object;)Lorg/kotools/types/EmailAddress;
public final fun qualifiedName ()Ljava/lang/String;
}

public final class org/kotools/types/Zero {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package org.kotools.types

import kotools.types.experimental.ExperimentalKotoolsTypesApi
import kotools.types.internal.hashCodeOf
import kotools.types.internal.simpleNameOf
import org.kotools.types.internal.ExperimentalSince
import org.kotools.types.internal.InvalidEmailAddress
import org.kotools.types.internal.InvalidEmailAddressPattern
import org.kotools.types.internal.KotoolsTypesVersion
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic
import kotlin.reflect.KClass

private const val FINAL_WARNING: String = "RedundantModalityModifier"

Expand Down Expand Up @@ -156,6 +159,42 @@ public class EmailAddress private constructor(private val value: String) {
*/
public const val PATTERN: String = "^\\S+@\\S+\\.\\S+\$"

/**
* Returns the qualified name of the [EmailAddress] type.
* Matches the [KClass.qualifiedName] property on the Kotlin/JVM and
* the Kotlin Native platforms.
*
* <br>
* <details open>
* <summary>
* <b>Calling from Kotlin</b>
* </summary>
*
* Here's an example of calling this property from Kotlin code:
*
* SAMPLE: EmailAddressCompanionKotlinSample.qualifiedName.md
* </details>
*
* <br>
* <details>
* <summary>
* <b>Calling from Java</b>
* </summary>
*
* Here's an example of calling this property from Java code:
*
* SAMPLE: EmailAddressCompanionJavaSample.qualifiedName.md
* </details>
*/
@get:JvmName("qualifiedName")
@get:JvmStatic
public val qualifiedName: String
get() {
val emailAddressSimpleName: String =
simpleNameOf<EmailAddress>()
return "org.kotools.types.$emailAddressSimpleName"
}

/**
* Creates an instance of [EmailAddress] from the string representation
* of the specified [value], or throws an [IllegalArgumentException] if
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kotools.types

import kotools.types.experimental.ExperimentalKotoolsTypesApi
import kotools.types.internal.simpleNameOf
import org.kotools.types.internal.InvalidEmailAddress
import org.kotools.types.internal.InvalidEmailAddressPattern
import kotlin.test.Test
Expand Down Expand Up @@ -92,14 +93,22 @@ class EmailAddressTest {
}

@OptIn(ExperimentalKotoolsTypesApi::class)
class EmailAddressCompanionTest {
class EmailAddressCompanionCommonTest {
@Test
fun pattern_should_pass() {
val actual: String = EmailAddress.PATTERN
val expected = "^\\S+@\\S+\\.\\S+\$"
assertEquals(expected, actual)
}

@Test
fun qualifiedName_should_pass() {
val actual: String = EmailAddress.qualifiedName
val emailAddressSimpleName: String = simpleNameOf<EmailAddress>()
val expected = "org.kotools.types.$emailAddressSimpleName"
assertEquals(expected, actual)
}

@Test
fun fromString_Any_should_pass_with_a_valid_value() {
val value: Any = Values.VALID
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.kotools.types

import kotools.types.experimental.ExperimentalKotoolsTypesApi
import kotools.types.internal.simpleNameOf
import kotlin.test.Test
import kotlin.test.assertEquals

@OptIn(ExperimentalKotoolsTypesApi::class)
class EmailAddressCompanionJvmAndNativeTest {
@Test
fun `qualifiedName should return the qualified name of EmailAddress`() {
val actual: String = EmailAddress.qualifiedName
val expected: String = checkNotNull(EmailAddress::class.qualifiedName) {
val emailAddressSimpleName: String = simpleNameOf<EmailAddress>()
"Getting qualified name of '$emailAddressSimpleName' shouldn't " +
"return null."
}
assertEquals(expected, actual)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ void patternSample() {
System.out.println(pattern); // ^\S+@\S+\.\S+$
} // END

void qualifiedName() {
final String qualifiedName = EmailAddress.qualifiedName();
System.out.println(qualifiedName); // org.kotools.types.EmailAddress
} // END

void fromString_Any() {
final Object value = "[email protected]";
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ internal object EmailAddressCompanionKotlinSample {
println(pattern) // ^\S+@\S+\.\S+$
} // END

fun qualifiedName() {
val qualifiedName: String = EmailAddress.qualifiedName
println(qualifiedName) // org.kotools.types.EmailAddress
} // END

@Suppress("FunctionName")
fun fromString_Any() {
val value: Any = "[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ void patternSample_should_pass() {
Assert.prints(expected, sample::patternSample);
}

@Test
void qualifiedName_should_pass() {
final EmailAddressCompanionJavaSample sample =
new EmailAddressCompanionJavaSample();
final String expected = EmailAddress.class.getCanonicalName();
Assert.prints(expected, sample::qualifiedName);
}

@Test
void fromString_Any_should_pass() {
final EmailAddressCompanionJavaSample sample =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.kotools.types

import kotools.types.experimental.ExperimentalKotoolsTypesApi
import kotools.types.internal.simpleNameOf
import kotlin.test.Test

class EmailAddressCompanionKotlinSampleTest {
Expand All @@ -9,6 +11,16 @@ class EmailAddressCompanionKotlinSampleTest {
assertPrints(expected, EmailAddressCompanionKotlinSample::patternSample)
}

@OptIn(ExperimentalKotoolsTypesApi::class)
@Test
fun `qualifiedName should pass`() {
val expected: String = checkNotNull(EmailAddress::class.qualifiedName) {
val type: String = simpleNameOf<EmailAddress>()
"Getting qualified name of '$type' shouldn't return null."
}
assertPrints(expected, EmailAddressCompanionKotlinSample::qualifiedName)
}

@Test
fun `fromString(Any) should pass`(): Unit =
assertPrintsTrue(EmailAddressCompanionKotlinSample::fromString_Any)
Expand Down

0 comments on commit 61b775c

Please sign in to comment.