v2 / vlib / v / tests / comptime / comptime_else_map_init_markused_test.v
45 lines · 39 sloc · 1.16 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1// Regression test for https://github.com/vlang/v/issues/XXXXX
2// Comptime $else branch with map literal init should not cause
3// a panic in the markused walker when the branch is skipped.
4import x.json2
5
6pub enum AppError {
7 invalid_method
8}
9
10pub fn new_error_detail_with_details(code AppError, message string, details map[string]string) !string {
11 return '${message}: ${details}'
12}
13
14struct App {}
15
16fn (mut app App) test_handler(message map[string]json2.Any) string {
17 return 'test result'
18}
19
20pub fn fire_call[T](mut app T, method_name string, message map[string]json2.Any) !string {
21 $for method in T.methods {
22 if method.name == method_name {
23 $if method.return_type is string {
24 return app.$method(message)
25 } $else {
26 return new_error_detail_with_details(.invalid_method,
27 'Method should return string', {
28 'method': method_name
29 'return_type': method.return_type
30 })
31 }
32 }
33 }
34 return error('not found')
35}
36
37fn test_comptime_else_map_init() {
38 mut app := App{}
39 message := map[string]json2.Any{}
40 result := fire_call(mut app, 'test_handler', message) or {
41 assert false, 'unexpected error: ${err}'
42 return
43 }
44 assert result == 'test result'
45}
46