Skip to content

Commit

Permalink
✨ Add Zero(Any) constructor (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Jul 14, 2024
1 parent 2bddd45 commit 65cc4af
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions subprojects/library/src/api/types.api
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ public final class org/kotools/types/Zero {
public static final field Companion Lorg/kotools/types/Zero$Companion;
public static final field PATTERN Ljava/lang/String;
public fun <init> ()V
public fun <init> (Ljava/lang/Object;)V
public final fun compareTo (B)I
public final fun compareTo (D)I
public final fun compareTo (F)I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,44 @@ public class Zero {
* SAMPLE: [org.kotools.types.ZeroJavaSample.secondaryConstructor]
* </details>
*/
@Suppress("ConvertSecondaryConstructorToPrimary")
public constructor()

/**
* Creates an instance of [Zero] from the string representation of the
* specified [number], or throws an [IllegalArgumentException] if the string
* representation of [number] doesn't match [Zero.PATTERN].
*
* <br>
* <details>
* <summary>
* <b>Calling from Kotlin</b>
* </summary>
*
* Here's an example of calling this constructor from Kotlin code:
*
* SAMPLE: [org.kotools.types.ZeroCommonSample.constructorAny]
* </details>
*
* <br>
* <details>
* <summary>
* <b>Calling from Java</b>
* </summary>
*
* Here's an example of calling this constructor from Java code:
*
* SAMPLE: [org.kotools.types.ZeroJavaSample.constructorAny]
* </details>
*/
@ExperimentalSince(KotoolsTypesVersion.Unreleased)
public constructor(number: Any) {
val regex = Regex(PATTERN)
val numberMatchesRegex: Boolean = "$number".matches(regex)
require(numberMatchesRegex) {
"'$number' is not a valid representation of zero."
}
}

// -------------------- Structural equality operations ---------------------

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ class ZeroCommonSample {
Zero()
}

@Test
fun constructorAny() {
val number: Any = "0.000"
val isSuccess: Boolean = try {
Zero(number)
true
} catch (exception: IllegalArgumentException) {
false
}
assertTrue(isSuccess)
}

// -------------------- Structural equality operations ---------------------

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@ import kotlin.test.assertTrue

@OptIn(ExperimentalKotoolsTypesApi::class)
class ZeroTest {
private val validNumbers: List<Any>
get() = listOf(
0, 0.0,
"+0", "+000", "+0.000", "+000.000", // with unary plus
"-0", "-000", "-0.000", "-000.000" // with unary minus
)

private val invalidNumbers: List<Any>
get() = listOf<Any>(
".0", "+.0", "-.0", // integer part missing
"0,0", "+0,0", "-0,0", // comma as decimal point
"0.", "+0.", "-0.", // decimal part missing
"hello world", "123456789" // not zero number
)

@Test
fun constructorAnyShouldPassWithValidNumber(): Unit =
this.validNumbers.forEach(::Zero)

@Test
fun constructorAnyShouldFailWithInvalidNumber(): Unit =
this.invalidNumbers.forEach {
val exception: IllegalArgumentException =
assertFailsWith { Zero(it) }
val actual: String? = exception.message
val expected = "'$it' is not a valid representation of zero."
assertEquals(expected, actual)
}

// -------------------- Structural equality operations ---------------------

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ void secondaryConstructor() {
new Zero();
}

@Test
void constructorAny() {
final Object number = "0.000";
boolean isSuccess;
try {
new Zero(number);
isSuccess = true;
} catch (final IllegalArgumentException exception) {
isSuccess = false;
}
Assertions.assertTrue(isSuccess);
}

// -------------------- Structural equality operations ---------------------

@Test
Expand Down

0 comments on commit 65cc4af

Please sign in to comment.