| 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. |
| 4 | import x.json2 |
| 5 | |
| 6 | pub enum AppError { |
| 7 | invalid_method |
| 8 | } |
| 9 | |
| 10 | pub fn new_error_detail_with_details(code AppError, message string, details map[string]string) !string { |
| 11 | return '${message}: ${details}' |
| 12 | } |
| 13 | |
| 14 | struct App {} |
| 15 | |
| 16 | fn (mut app App) test_handler(message map[string]json2.Any) string { |
| 17 | return 'test result' |
| 18 | } |
| 19 | |
| 20 | pub 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 | |
| 37 | fn 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 | |