From 04097e99df51d4edb08d61db9e3ceeff6943a9e9 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 26 Apr 2026 00:22:08 +0300 Subject: [PATCH] tests: interface_closure_test.v --- .../tests/interfaces/interface_closure_test.v | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/vlib/v/tests/interfaces/interface_closure_test.v b/vlib/v/tests/interfaces/interface_closure_test.v index 0784f584d..9a979853f 100644 --- a/vlib/v/tests/interfaces/interface_closure_test.v +++ b/vlib/v/tests/interfaces/interface_closure_test.v @@ -28,3 +28,61 @@ fn test_main() { b.with_reader(a.caller)! assert true } + +interface ClosureCommand { + value() int +mut: + increment() +} + +struct MutableClosureCommand { +mut: + value_ int +} + +fn (cmd MutableClosureCommand) value() int { + return cmd.value_ +} + +fn (mut cmd MutableClosureCommand) increment() { + cmd.value_++ +} + +fn with_closure_connection(func fn () !) ! { + func()! +} + +fn with_closure_writer(func fn () !) ! { + func()! +} + +fn with_closure_reader(func fn () !) ! { + func()! +} + +fn write_closure_command(cmd ClosureCommand) ! { + assert cmd.value() == 41 +} + +fn read_closure_command(mut cmd ClosureCommand) ! { + cmd.increment() +} + +fn process_closure_command(mut cmd ClosureCommand) ! { + with_closure_connection(fn [mut cmd] () ! { + with_closure_writer(fn [cmd] () ! { + write_closure_command(cmd)! + })! + with_closure_reader(fn [mut cmd] () ! { + read_closure_command(mut cmd)! + })! + })! +} + +fn test_nested_closure_captures_mut_interface_by_value() { + mut cmd := ClosureCommand(MutableClosureCommand{ + value_: 41 + }) + process_closure_command(mut cmd)! + assert cmd.value() == 42 +} -- 2.39.5