Skip to content

Add precondition functions for InvalidParameterException #3

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 1 commit into from
Feb 6, 2025
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
5 changes: 5 additions & 0 deletions library/error/api/error.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public final class org/kotlincrypto/error/PreconditionsKt {
public static final fun requireParam (Z)V
public static final fun requireParam (ZLkotlin/jvm/functions/Function0;)V
}

3 changes: 3 additions & 0 deletions library/error/api/error.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ open class org.kotlincrypto.error/ShortBufferException : org.kotlincrypto.error/
constructor <init>() // org.kotlincrypto.error/ShortBufferException.<init>|<init>(){}[0]
constructor <init>(kotlin/String?) // org.kotlincrypto.error/ShortBufferException.<init>|<init>(kotlin.String?){}[0]
}

final inline fun org.kotlincrypto.error/requireParam(kotlin/Boolean) // org.kotlincrypto.error/requireParam|requireParam(kotlin.Boolean){}[0]
final inline fun org.kotlincrypto.error/requireParam(kotlin/Boolean, kotlin/Function0<kotlin/Any>) // org.kotlincrypto.error/requireParam|requireParam(kotlin.Boolean;kotlin.Function0<kotlin.Any>){}[0]
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2025 Matthew Nelson
*
* 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
*
* https://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.
**/
@file:Suppress("KotlinRedundantDiagnosticSuppress", "NOTHING_TO_INLINE")

package org.kotlincrypto.error

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract

/**
* Throws [InvalidParameterException] when [condition] is false.
* */
@OptIn(ExperimentalContracts::class)
public inline fun requireParam(condition: Boolean) {
contract {
returns() implies condition
}
requireParam(condition) { "Parameter check failed." }
}

/**
* Throws [InvalidParameterException] with the result of calling [lazyMessage] when [condition] is false.
* */
@OptIn(ExperimentalContracts::class)
public inline fun requireParam(condition: Boolean, lazyMessage: () -> Any) {
contract {
returns() implies condition
}
if (condition) return
val message = lazyMessage()
throw InvalidParameterException(message.toString())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2025 Matthew Nelson
*
* 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
*
* https://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 org.kotlincrypto.error

import kotlin.test.Test
import kotlin.test.assertFailsWith

class PreconditionsUnitTest {

@Test
fun givenRequireParam_whenConditionTrue_thenDoesNotThrow() {
requireParam(true)
requireParam(true) { "fail" }
}

@Test
fun givenRequireParam_whenConditionFalse_thenThrows() {
assertFailsWith<InvalidParameterException> { requireParam(false) }
assertFailsWith<InvalidParameterException> { requireParam(false) { "fail" } }
}
}