From 617adaf0d6d80cc250cce9b509880da481d70884 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 21 Apr 2026 14:46:49 +0300 Subject: [PATCH] tests: add test for saving a reference to a function in a map via metaprogramming (fixes #18397) --- .../comptime_call_method_closure_test.v | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/vlib/v/tests/comptime/comptime_call_method_closure_test.v b/vlib/v/tests/comptime/comptime_call_method_closure_test.v index e94fbdfff..f7388d564 100644 --- a/vlib/v/tests/comptime/comptime_call_method_closure_test.v +++ b/vlib/v/tests/comptime/comptime_call_method_closure_test.v @@ -43,3 +43,31 @@ fn test_comptime_method_value() { assert results[0] == 'foo rabbit' assert results[1] == 'bar rabbit' } + +@[heap] +struct MethodMapApp { + prefix string +} + +fn (app &MethodMapApp) alpha() string { + return '${app.prefix}:alpha' +} + +fn (app &MethodMapApp) beta() string { + return '${app.prefix}:beta' +} + +fn build_method_map[T](app &T) map[string]fn () string { + mut syms := map[string]fn () string{} + $for method in T.methods { + syms[method.name] = app.$(method.name) + } + return syms +} + +fn test_comptime_method_value_map() { + app := MethodMapApp{'ctx'} + syms := build_method_map(app) + assert syms['alpha']() == 'ctx:alpha' + assert syms['beta']() == 'ctx:beta' +} -- 2.39.5