mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
Add module jwutils.cast
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
57ff96608b
commit
a0234e7d54
2 changed files with 126 additions and 0 deletions
44
test/cast/main.py
Normal file
44
test/cast/main.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
from jwutils.cast import *
|
||||
from jwutils.log import *
|
||||
|
||||
set_level(DEBUG)
|
||||
set_flags('console,color,position,prio')
|
||||
|
||||
test_data = [
|
||||
(bool, 'true', True, True),
|
||||
(bool, 'False', True, False),
|
||||
(bool, 'yes', True, True),
|
||||
(bool, 'nah', None, None),
|
||||
(bool, 'duh', None, None),
|
||||
(bool, '11', 11, None),
|
||||
(bool, '22', 22, None),
|
||||
(bool, '0', False,False),
|
||||
(bool, '', None, None),
|
||||
(bool, None, Exception(), Exception()),
|
||||
(bool, '-1', -1, None),
|
||||
(bool, '1.6', None, None),
|
||||
(int, '1', 1, None),
|
||||
(int, '22', 22, 22),
|
||||
(int, '0', 0, 0),
|
||||
(int, '-1', -1, -1),
|
||||
(int, '-32', -32, -32),
|
||||
(int, 123, 132, 132),
|
||||
(str, '', '', ''),
|
||||
(str, None, Exception(), Exception()),
|
||||
(str, 'This is a string', 'This is a string', 'This is a string')
|
||||
]
|
||||
|
||||
status = 0
|
||||
for tp, s, guessed_val, cast_val in test_data:
|
||||
try:
|
||||
slog(INFO, f'guessed type of "{s}" = "{guess_type(s)}", should be "{tp}"')
|
||||
slog(INFO, f'cast_str("{s}") = "{cast_str(s)}"')
|
||||
slog(INFO, f'cast_str("{s}, {tp}") = "{cast_str(s, target_type=tp)}"')
|
||||
except Exception as e:
|
||||
if s is None:
|
||||
slog(INFO, f'guessed type of "{s}" = Exception, should be "Exception"')
|
||||
continue
|
||||
slog(ERR, f'Test failed for "{s}": ({e})')
|
||||
status = 1
|
||||
|
||||
exit(status)
|
||||
Loading…
Add table
Add a link
Reference in a new issue