-
Notifications
You must be signed in to change notification settings - Fork 0
test: Add various unit tests #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
helperlib/src/test/java/com/itachi1706/helperlib/exceptions/ApiExceptionTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.itachi1706.helperlib.exceptions | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNull | ||
|
|
||
| class ApiExceptionTest { | ||
|
|
||
| @Test | ||
| fun apiExceptionPreservesMessage() { | ||
| val exception = ApiException("Callback is not set") | ||
|
|
||
| assertEquals("Callback is not set", exception.message) | ||
| } | ||
|
|
||
| @Test | ||
| fun apiExceptionExtendsExceptionType() { | ||
| val exception = ApiException("Any message") | ||
| val asException: Exception = exception | ||
|
|
||
| assertEquals("Any message", asException.message) | ||
| } | ||
|
|
||
| @Test | ||
| fun apiExceptionHasNullCauseByDefault() { | ||
| val exception = ApiException("Any message") | ||
|
|
||
| assertNull(exception.cause) | ||
| } | ||
| } | ||
|
|
||
|
|
54 changes: 54 additions & 0 deletions
54
helperlib/src/test/java/com/itachi1706/helperlib/helpers/LogHelperTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package com.itachi1706.helperlib.helpers | ||
|
|
||
| import com.itachi1706.helperlib.interfaces.LogHandler | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.verify | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class LogHelperTest { | ||
|
|
||
| @Test | ||
| fun getLogLevelCharReturnsErrorForError() { | ||
| assertEquals('E', LogHelper.getLogLevelChar(6)) | ||
| } | ||
|
|
||
| @Test | ||
| fun getLogLevelCharReturnsWarnForWarn() { | ||
| assertEquals('W', LogHelper.getLogLevelChar(5)) | ||
| } | ||
|
|
||
| @Test | ||
| fun getLogLevelCharReturnsDebugForDebug() { | ||
| assertEquals('D', LogHelper.getLogLevelChar(3)) | ||
| } | ||
|
|
||
| @Test | ||
| fun getLogLevelCharReturnsInfoForInfo() { | ||
| assertEquals('I', LogHelper.getLogLevelChar(4)) | ||
| } | ||
|
|
||
| @Test | ||
| fun getLogLevelCharReturnsVerboseForUnknownLevel() { | ||
| assertEquals('V', LogHelper.getLogLevelChar(Int.MAX_VALUE)) | ||
| } | ||
|
|
||
| @Test | ||
| fun getGenericLogStringFormatsLevelTagAndMessage() { | ||
| val log = LogHelper.getGenericLogString(5, "Network", "Timeout") | ||
|
|
||
| assertEquals("W/Network: Timeout", log) | ||
| } | ||
|
|
||
| @Test | ||
| fun addExternalLogForwardsDebugMessagesToExternalLogger() { | ||
| val logger = mock<LogHandler>() | ||
| LogHelper.addExternalLog(logger) | ||
|
|
||
| LogHelper.d("UnitTest", "hello") | ||
|
|
||
| verify(logger).handleExtraLogging(3, "UnitTest", "hello") | ||
| } | ||
| } | ||
|
|
||
|
|
34 changes: 34 additions & 0 deletions
34
helperlib/src/test/java/com/itachi1706/helperlib/helpers/ValidationHelperTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.itachi1706.helperlib.helpers | ||
|
|
||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class ValidationHelperTest { | ||
|
|
||
| @Test | ||
| fun bytesToHexFormatsSingleByteAsUppercaseHex() { | ||
| val value = byteArrayOf(0x0A) | ||
|
|
||
| assertEquals("0A", ValidationHelper.bytesToHex(value)) | ||
| } | ||
|
|
||
| @Test | ||
| fun bytesToHexFormatsMultipleBytesWithColonSeparator() { | ||
| val value = byteArrayOf(0x12, 0x34, 0x56) | ||
|
|
||
| assertEquals("12:34:56", ValidationHelper.bytesToHex(value)) | ||
| } | ||
|
|
||
| @Test | ||
| fun bytesToHexHandlesNegativeByteValuesCorrectly() { | ||
| val value = byteArrayOf(0xFF.toByte(), 0x80.toByte(), 0x00) | ||
|
|
||
| assertEquals("FF:80:00", ValidationHelper.bytesToHex(value)) | ||
| } | ||
|
|
||
| @Test | ||
| fun bytesToHexReturnsEmptyStringForEmptyArray() { | ||
| assertEquals("", ValidationHelper.bytesToHex(byteArrayOf())) | ||
| } | ||
| } | ||
|
|
136 changes: 136 additions & 0 deletions
136
helperlib/src/test/java/com/itachi1706/helperlib/objects/ApiResponseTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| package com.itachi1706.helperlib.objects | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
| import kotlinx.serialization.json.Json | ||
| import kotlinx.serialization.json.JsonPrimitive | ||
| import kotlinx.serialization.json.buildJsonObject | ||
| import kotlinx.serialization.json.put | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFailsWith | ||
| import kotlin.test.assertNotNull | ||
| import kotlin.test.assertNull | ||
|
|
||
| class ApiResponseTest { | ||
|
|
||
| @Test | ||
| fun getTypedDataReturnsDecodedObjectWhenDataMatchesType() { | ||
| val response = ApiResponse( | ||
| date = "2025-10-01T10:29:38.201645045Z", | ||
| status = 200, | ||
| message = "OK", | ||
| success = true, | ||
| data = buildJsonObject { | ||
| put("id", 7) | ||
| put("name", "Kenneth") | ||
| } | ||
| ) | ||
|
|
||
| val typedData = response.getTypedData<TestPayload>() | ||
|
|
||
| assertEquals(TestPayload(id = 7, name = "Kenneth"), typedData) | ||
| } | ||
|
|
||
| @Test | ||
| fun getTypedDataReturnsNullWhenDataIsMissing() { | ||
| val response = ApiResponse( | ||
| date = "2025-10-01T10:29:38.201645045Z", | ||
| status = 204, | ||
| message = "No Content", | ||
| success = true, | ||
| data = null | ||
| ) | ||
|
|
||
| assertNull(response.getTypedData<TestPayload>()) | ||
| } | ||
|
|
||
| @Test | ||
| fun getTypedDataUsesProvidedJsonConfigurationForUnknownKeys() { | ||
| val response = ApiResponse( | ||
| date = "2025-10-01T10:29:38.201645045Z", | ||
| status = 200, | ||
| message = "OK", | ||
| success = true, | ||
| data = buildJsonObject { | ||
| put("id", 7) | ||
| put("name", "Kenneth") | ||
| put("ignored", "value") | ||
| } | ||
| ) | ||
|
|
||
| val typedData = response.getTypedData<TestPayload>( | ||
| Json { | ||
| ignoreUnknownKeys = true | ||
| } | ||
| ) | ||
|
|
||
| assertEquals(TestPayload(id = 7, name = "Kenneth"), typedData) | ||
| } | ||
|
|
||
| @Test | ||
| fun getTypedDataUsesProvidedJsonConfigurationForLenientPayloads() { | ||
| val json = Json { | ||
| isLenient = true | ||
| ignoreUnknownKeys = true | ||
| } | ||
|
|
||
| val response = json.decodeFromString<ApiResponse>( | ||
| "{\"date\":\"2025-10-01T10:29:38.201645045Z\",\"status\":200,\"message\":\"OK\",\"success\":true,\"data\":{\"id\":7,\"name\":\"Kenneth\"},\"ignored\":\"value\"}" | ||
| ) | ||
|
|
||
| val typedData = response.getTypedData<TestPayload>(json) | ||
|
|
||
| assertEquals(TestPayload(id = 7, name = "Kenneth"), typedData) | ||
| } | ||
|
|
||
| @Test | ||
| fun getTypedDataThrowsWhenPayloadDoesNotMatchRequestedType() { | ||
| val response = ApiResponse( | ||
| date = "2025-10-01T10:29:38.201645045Z", | ||
| status = 200, | ||
| message = "OK", | ||
| success = true, | ||
| data = JsonPrimitive("not a number") | ||
| ) | ||
|
|
||
| assertFailsWith<IllegalArgumentException> { | ||
| response.getTypedData<Int>() | ||
| } | ||
|
itachi1706 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Test | ||
| fun getDateObjectParsesIsoDateWithNanosecondsByTruncatingToMilliseconds() { | ||
| val response = ApiResponse( | ||
| date = "2025-10-01T10:29:38.201645045Z", | ||
| status = 200, | ||
| message = "OK", | ||
| success = true | ||
| ) | ||
|
|
||
| val parsedDate = response.getDateObject() | ||
|
|
||
| assertNotNull(parsedDate) | ||
| assertEquals(1759314578201L, parsedDate.time) | ||
| } | ||
|
|
||
| @Test | ||
| fun getDateObjectReturnsNullForInvalidDateString() { | ||
| val response = ApiResponse( | ||
| date = "not-a-date", | ||
| status = 500, | ||
| message = "Error", | ||
| success = false | ||
| ) | ||
|
|
||
| assertNull(response.getDateObject()) | ||
| } | ||
|
|
||
| @Serializable | ||
| private data class TestPayload( | ||
| val id: Int, | ||
| val name: String | ||
| ) | ||
| } | ||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.