Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoriiSolnyshkin committed Jul 17, 2024
1 parent 6655d34 commit 8ebf479
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
// REPLACE_STDLIB_EXTENSIONS
// ALWAYS_VALIDATE

import kotlin.contracts.contract
import kotlin.contracts.ExperimentalContracts
import org.jetbrains.kotlin.formver.plugin.verify

@OptIn(ExperimentalContracts::class)
fun <!VIPER_TEXT!>useRepeat<!>(): Unit {
contract {
returns()
}
fun <!VIPER_TEXT!>useChecks<!>(): Unit {
check(true)
check(true) { "Lazy message" }
}

// TODO: add test for `repeat` (we actually have a bug there because we require unsatisfied precondition in loops)

// TODO: add tests for `let`, `apply`, `with`, `run`, `also`
fun <!VIPER_TEXT!>useRuns<!>(x: Int): Unit {
verify(run { x + 1 } == 1 + x)
verify(x.run { plus(1) } == 1 + x)
}

fun <!VIPER_TEXT!>useAlso<!>(x: Int): Unit {
(x + 1).also { verify(it == 1 + x) }
}

fun <!VIPER_TEXT!>useLet<!>(x: Int): Unit {
verify(x.let { it + 1 } == 1 + x)
}

fun <!VIPER_TEXT!>useWith<!>(x: Int): Unit {
with(x + 1) { verify(this == 1 + x) }
}

fun <!VIPER_TEXT!>useApply<!>(x: Int): Unit {
(x + 1).apply { verify(this == 1 + x) }
}

3 changes: 1 addition & 2 deletions plugins/formal-verification/testData/stdlibReplacements.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.jetbrains.kotlin.formver.plugin.NeverConvert
import org.jetbrains.kotlin.formver.plugin.verify

@NeverConvert
inline fun <T> run(block: () -> T): T = block()
Expand All @@ -17,7 +16,7 @@ inline fun <T> T.also(block: (T) -> Unit): T {
inline fun <T, R> T.let(block: (T) -> R): R = block(this)

@NeverConvert
inline fun <T> T.apply(block: T.() -> Unit): T = {
inline fun <T> T.apply(block: T.() -> Unit): T {
this.block()
return this
}
Expand Down

0 comments on commit 8ebf479

Please sign in to comment.