diff --git a/usvm-ts/src/test/kotlin/org/usvm/samples/lang/SourceCallbackScenarioTest.kt b/usvm-ts/src/test/kotlin/org/usvm/samples/lang/SourceCallbackScenarioTest.kt new file mode 100644 index 0000000000..b092577f65 --- /dev/null +++ b/usvm-ts/src/test/kotlin/org/usvm/samples/lang/SourceCallbackScenarioTest.kt @@ -0,0 +1,47 @@ +package org.usvm.samples.lang + +import org.jacodb.ets.utils.DEFAULT_ARK_CLASS_NAME +import org.jacodb.ets.utils.EtsIrProvider +import org.jacodb.ets.utils.loadEtsProjectAutoConvert +import org.junit.jupiter.api.Test +import org.usvm.api.TsTestValue +import org.usvm.machine.TsMachine +import org.usvm.util.TsMethodTestRunner +import org.usvm.util.eq +import org.usvm.util.getResourcePath +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class SourceCallbackScenarioTest : TsMethodTestRunner() { + override val scene = loadEtsProjectAutoConvert( + projectPath = getResourcePath(res = "/samples/lang/source-callback-scenario"), + provider = EtsIrProvider.TS_FRONTEND, + ) + + @Test + fun `an imported registry invokes the same supplied closure on each emission`() { + val entry = scene.projectClasses.single { clazz -> + clazz.name == DEFAULT_ARK_CLASS_NAME && clazz.signature.file.fileName.endsWith("Scenario.ts") + }.methods.single { it.name == "withSourceCallback" } + + discoverProperties( + method = entry, + { repeat, total -> !repeat.value && (total eq 2) }, + { repeat, total -> repeat.value && (total eq 5) }, + invariants = arrayOf({ repeat, total -> total eq if (repeat.value) 5 else 2 }), + ) + } + + @Test + fun `an unbound callable parameter is still reported as unsupported`() { + val entry = getMethod(methodName = "addListener", className = "CallbackRegistry") + + val result = TsMachine(scene = scene, options = options, tsOptions = tsOptions).use { machine -> + machine.analyzeWithMetadata(methods = listOf(entry)) + } + + assertFalse(result.engineFailed) + assertTrue(result.states.isEmpty()) + assertTrue(result.unsupportedPaths.any { "Callable input parameter 'listener'" in it }) + } +} diff --git a/usvm-ts/src/test/resources/samples/lang/source-callback-scenario/CallbackRegistry.ts b/usvm-ts/src/test/resources/samples/lang/source-callback-scenario/CallbackRegistry.ts new file mode 100644 index 0000000000..f3e600445d --- /dev/null +++ b/usvm-ts/src/test/resources/samples/lang/source-callback-scenario/CallbackRegistry.ts @@ -0,0 +1,12 @@ +export class CallbackRegistry { + private listener: (value: number) => void = () => {}; + + addListener(listener: (value: number) => void): void { + this.listener = listener; + } + + emit(value: number): void { + const listener = this.listener; + listener(value); + } +} diff --git a/usvm-ts/src/test/resources/samples/lang/source-callback-scenario/Scenario.ts b/usvm-ts/src/test/resources/samples/lang/source-callback-scenario/Scenario.ts new file mode 100644 index 0000000000..7f9cf9f62b --- /dev/null +++ b/usvm-ts/src/test/resources/samples/lang/source-callback-scenario/Scenario.ts @@ -0,0 +1,21 @@ +import { CallbackRegistry } from "./CallbackRegistry"; + +class Counter { + total = 0; +} + +export function withSourceCallback(repeat: boolean): number { + const registry = new CallbackRegistry(); + const counter = new Counter(); + const listener = (value: number): void => { + counter.total += value; + }; + + registry.addListener(listener); + registry.emit(2); + if (repeat) { + registry.emit(3); + } + + return counter.total; +}