-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,73 @@ | ||
/* | ||
* Copyright 2024 Kazimierz Pogoda / Xemantic | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.xemantic.ai.money.test | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.json.Json | ||
import kotlin.test.asserter | ||
|
||
/** | ||
* A pretty printing [Json] for tests. | ||
*/ | ||
val testJson = Json { | ||
prettyPrint = true | ||
@OptIn(ExperimentalSerializationApi::class) | ||
prettyPrintIndent = " " | ||
} | ||
|
||
/** | ||
* Provides an infix function for equality assertions in tests. | ||
* | ||
* This function is inspired by the Kotest library's assertions but is implemented | ||
* to support power-assert conventions. It allows for a more readable and expressive | ||
* way of writing assertions in Kotlin tests. | ||
* | ||
* Note: Together with the `shouldBe` variant receiving a message this function fulfills | ||
* the `power-assert` convention. | ||
* | ||
* @param expected The expected value to compare against. | ||
* @throws AssertionError if the actual value is not equal to the expected value. | ||
*/ | ||
infix fun <V> V.shouldBe( | ||
expected: V | ||
) { | ||
shouldBe( | ||
expected = expected, | ||
message = null | ||
) | ||
} | ||
|
||
/** | ||
* Asserts that the actual value is equal to the expected value. | ||
* | ||
* This function provides a more flexible way to perform equality assertions | ||
* by allowing an optional custom error message. | ||
* | ||
* @param expected The expected value to compare against. | ||
* @param message An optional custom message to be used in case of assertion failure. | ||
* @throws AssertionError if the actual value is not equal to the expected value. | ||
*/ | ||
fun <V> V.shouldBe( | ||
expected: V, | ||
message: String? = null | ||
) { | ||
asserter.assertEquals( | ||
message = message, | ||
actual = this, | ||
expected = expected | ||
) | ||
} |