Share a common function to convert objects to display strings for consistency. While at it, also add support for formatting user options.pull/11833/head
parent
465ad6d261
commit
cec3edc08a
10 changed files with 119 additions and 32 deletions
@ -0,0 +1,10 @@ |
||||
## Unified message(), str.format() and f-string formatting |
||||
|
||||
They now all support the same set of values: strings, integers, bools, options, |
||||
dictionaries and lists thereof. |
||||
|
||||
- Feature options (i.e. enabled, disabled, auto) were not previously supported |
||||
by any of those functions. |
||||
- Lists and dictionaries were not previously supported by f-string. |
||||
- str.format() allowed any type and often resulted in printing the internal |
||||
representation which is now deprecated. |
@ -0,0 +1,20 @@ |
||||
project('test format string') |
||||
|
||||
# Test all supported types in message(), format(), f-string. |
||||
foreach t : [get_option('opt'), 42, true, false, 'str', ['list'], {'dict': 'value'}] |
||||
message(t, '@0@'.format(t), f'@t@', [t], {'key': t}) |
||||
endforeach |
||||
|
||||
# Deprecated but should work with str.format(). |
||||
env = environment() |
||||
message('@0@'.format(env)) |
||||
|
||||
# Should fail with f-string and message() |
||||
error_msg = 'Value other than strings, integers, bools, options, dictionaries and lists thereof.' |
||||
testcase expect_error('message(): ' + error_msg) |
||||
message(env) |
||||
endtestcase |
||||
|
||||
testcase expect_error('f-string: ' + error_msg) |
||||
message(f'@env@') |
||||
endtestcase |
@ -0,0 +1 @@ |
||||
option('opt', type: 'feature') |
@ -0,0 +1,28 @@ |
||||
{ |
||||
"stdout": [ |
||||
{ |
||||
"line": "Message: auto auto auto [auto] {'key' : auto}" |
||||
}, |
||||
{ |
||||
"line": "Message: 42 42 42 [42] {'key' : 42}" |
||||
}, |
||||
{ |
||||
"line": "Message: true true true [true] {'key' : true}" |
||||
}, |
||||
{ |
||||
"line": "Message: false false false [false] {'key' : false}" |
||||
}, |
||||
{ |
||||
"line": "Message: str str str ['str'] {'key' : 'str'}" |
||||
}, |
||||
{ |
||||
"line": "Message: ['list'] ['list'] ['list'] [['list']] {'key' : ['list']}" |
||||
}, |
||||
{ |
||||
"line": "Message: {'dict' : 'value'} {'dict' : 'value'} {'dict' : 'value'} [{'dict' : 'value'}] {'key' : {'dict' : 'value'}}" |
||||
}, |
||||
{ |
||||
"line": "Message: <EnvironmentVariables: []>" |
||||
} |
||||
] |
||||
} |
Loading…
Reference in new issue