Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ Android Commons is a curated set of libraries designed to accelerate Android dev
| `commons-android-compose` | Jetpack Compose utilities | - | [![Maven Central](https://img.shields.io/maven-central/v/com.raxdenstudios/commons-android-compose.svg?label=version)](https://central.sonatype.com/artifact/com.raxdenstudios/commons-android-compose) |
| `commons-android-test` | Android testing utilities | - | [![Maven Central](https://img.shields.io/maven-central/v/com.raxdenstudios/commons-android-test.svg?label=version)](https://central.sonatype.com/artifact/com.raxdenstudios/commons-android-test) |
| **Async & Concurrency** |
| `commons-coroutines` | Kotlin Coroutines extensions | - | [![Maven Central](https://img.shields.io/maven-central/v/com.raxdenstudios/commons-coroutines.svg?label=version)](https://central.sonatype.com/artifact/com.raxdenstudios/commons-coroutines) |
| `commons-coroutines-test` | Coroutines testing utilities | - | [![Maven Central](https://img.shields.io/maven-central/v/com.raxdenstudios/commons-coroutines-test.svg?label=version)](https://central.sonatype.com/artifact/com.raxdenstudios/commons-coroutines-test) |
| `commons-coroutines` | Kotlin Coroutines extensions | [📖 Docs](libraries/coroutines/README.md) | [![Maven Central](https://img.shields.io/maven-central/v/com.raxdenstudios/commons-coroutines.svg?label=version)](https://central.sonatype.com/artifact/com.raxdenstudios/commons-coroutines) |
| `commons-coroutines-test` | Coroutines testing utilities | [📖 Docs](libraries/coroutines/README.md) | [![Maven Central](https://img.shields.io/maven-central/v/com.raxdenstudios/commons-coroutines-test.svg?label=version)](https://central.sonatype.com/artifact/com.raxdenstudios/commons-coroutines-test) |
| **Networking** |
| `commons-network` | Network utilities and interceptors | [📖 Docs](libraries/network/README.md) | [![Maven Central](https://img.shields.io/maven-central/v/com.raxdenstudios/commons-network.svg?label=version)](https://central.sonatype.com/artifact/com.raxdenstudios/commons-network) |
| **Pagination** |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package com.raxdenstudios.commons.core

import com.google.common.truth.Truth.assertThat
import org.junit.Test

internal class NetworkErrorTest {

@Test
fun `Client error should have correct properties`() {
val error = NetworkError.Client<String>(
code = 400,
body = "Bad Request",
message = "Invalid input"
)

assertThat(error.code).isEqualTo(400)
assertThat(error.body).isEqualTo("Bad Request")
assertThat(error.message).isEqualTo("Invalid input")
}

@Test
fun `Client error with null body should work`() {
val error = NetworkError.Client<String>(
code = 401,
body = null,
message = "Unauthorized"
)

assertThat(error.code).isEqualTo(401)
assertThat(error.body).isNull()
assertThat(error.message).isEqualTo("Unauthorized")
}

@Test
fun `Server error should have correct properties`() {
val error = NetworkError.Server<String>(
code = 500,
body = "Internal Server Error",
message = "Server crashed"
)

assertThat(error.code).isEqualTo(500)
assertThat(error.body).isEqualTo("Internal Server Error")
assertThat(error.message).isEqualTo("Server crashed")
}

@Test
fun `Server error with null body should work`() {
val error = NetworkError.Server<String>(
code = 503,
body = null,
message = "Service Unavailable"
)

assertThat(error.code).isEqualTo(503)
assertThat(error.body).isNull()
assertThat(error.message).isEqualTo("Service Unavailable")
}

@Test
fun `Network error should have correct properties`() {
val error = NetworkError.Network<String>(
body = "Connection timeout",
message = "Network timeout"
)

assertThat(error.body).isEqualTo("Connection timeout")
assertThat(error.message).isEqualTo("Network timeout")
}

@Test
fun `Network error with null body should work`() {
val error = NetworkError.Network<String>(
body = null,
message = "No internet connection"
)

assertThat(error.body).isNull()
assertThat(error.message).isEqualTo("No internet connection")
}

@Test
fun `Unknown error should have correct properties`() {
val error = NetworkError.Unknown<String>(
code = 999,
body = "Unknown error body",
message = "Unknown error occurred"
)

assertThat(error.code).isEqualTo(999)
assertThat(error.body).isEqualTo("Unknown error body")
assertThat(error.message).isEqualTo("Unknown error occurred")
}

@Test
fun `Unknown error with null code and body should work`() {
val error = NetworkError.Unknown<String>(
code = null,
body = null,
message = "Unknown error"
)

assertThat(error.code).isNull()
assertThat(error.body).isNull()
assertThat(error.message).isEqualTo("Unknown error")
}

@Test
fun `NetworkError should work with different body types`() {
data class ErrorBody(val errorCode: String, val details: String)

val errorBody = ErrorBody("ERR_001", "Validation failed")
val error = NetworkError.Client(
code = 400,
body = errorBody,
message = "Validation error"
)

assertThat(error.body).isEqualTo(errorBody)
assertThat(error.body?.errorCode).isEqualTo("ERR_001")
assertThat(error.body?.details).isEqualTo("Validation failed")
}

@Test
fun `NetworkError sealed interface should allow polymorphic usage`() {
val errors: List<NetworkError<String>> = listOf(
NetworkError.Client(code = 400, message = "Bad Request"),
NetworkError.Server(code = 500, message = "Server Error"),
NetworkError.Network(message = "Network Error"),
NetworkError.Unknown(message = "Unknown Error")
)

assertThat(errors).hasSize(4)
assertThat(errors[0]).isInstanceOf(NetworkError.Client::class.java)
assertThat(errors[1]).isInstanceOf(NetworkError.Server::class.java)
assertThat(errors[2]).isInstanceOf(NetworkError.Network::class.java)
assertThat(errors[3]).isInstanceOf(NetworkError.Unknown::class.java)
}

@Test
fun `NetworkError can be used in when expression`() {
val error: NetworkError<String> = NetworkError.Client(
code = 404,
message = "Not Found"
)

val result = when (error) {
is NetworkError.Client -> "Client error: ${error.code}"
is NetworkError.Server -> "Server error: ${error.code}"
is NetworkError.Network -> "Network error"
is NetworkError.Unknown -> "Unknown error"
}

assertThat(result).isEqualTo("Client error: 404")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.raxdenstudios.commons.core.ext

import com.google.common.truth.Truth.assertThat
import org.junit.Test

internal class KotlinExtensionTest {

@Test
fun `exhaustive should return the same value when not null`() {
val value: String? = "test"

val result = value.exhaustive

assertThat(result).isEqualTo("test")
}

@Test
fun `exhaustive should return null when value is null`() {
val value: String? = null

val result = value.exhaustive

assertThat(result).isNull()
}

@Test
fun `exhaustive should work with when expressions on boolean`() {
val value: Boolean = true

val output = when (value) {
true -> "success"
false -> "failure"
}.exhaustive

assertThat(output).isEqualTo("success")
}

@Test
fun `exhaustive should work with nullable when expressions`() {
val value: String? = "test"

val result = when (value) {
null -> null
else -> value.uppercase()
}.exhaustive

assertThat(result).isEqualTo("TEST")
}

@Test
fun `exhaustive should preserve type information`() {
val number: Int? = 42

val result: Int? = number.exhaustive

assertThat(result).isEqualTo(42)
}

@Test
fun `exhaustive should work with different types`() {
val stringValue: String? = "hello".exhaustive
val intValue: Int? = 123.exhaustive
val booleanValue: Boolean? = true.exhaustive
val listValue: List<String>? = listOf("a", "b").exhaustive

assertThat(stringValue).isEqualTo("hello")
assertThat(intValue).isEqualTo(123)
assertThat(booleanValue).isTrue()
assertThat(listValue).containsExactly("a", "b")
}

@Test
fun `exhaustive should help ensure when expressions are exhaustive`() {
val value: Boolean = true

// The exhaustive property ensures the when expression is exhaustive
val result = when (value) {
true -> "yes"
false -> "no"
}.exhaustive

assertThat(result).isEqualTo("yes")
}
}
Loading
Loading