2026-07-03 14:40:47 +02:00
|
|
|
from jw.util.cast import cast_str, guess_type
|
|
|
|
|
from jw.util.log import ERR, INFO, set_flags, set_level, slog
|
2025-01-16 12:39:20 +01:00
|
|
|
|
2026-07-03 14:40:47 +02:00
|
|
|
set_level('debug')
|
2025-01-16 12:39:20 +01:00
|
|
|
set_flags('console,color,position,prio')
|
|
|
|
|
|
2026-07-03 14:40:47 +02:00
|
|
|
# fmt: disable # don't conflate
|
|
|
|
|
# yapf: disable # don't conflate
|
2025-01-16 12:39:20 +01:00
|
|
|
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')
|
|
|
|
|
]
|
2026-07-03 14:40:47 +02:00
|
|
|
# fmt: ensable # don't conflate
|
|
|
|
|
# yapf: ensable # don't conflate
|
2025-01-16 12:39:20 +01:00
|
|
|
|
|
|
|
|
status = 0
|
|
|
|
|
for tp, s, guessed_val, cast_val in test_data:
|
|
|
|
|
try:
|
2026-09-11 17:00:03 +02:00
|
|
|
slog(INFO, f'guessed type of "{s}" = "{guess_type(s)}", should be "{tp}"') # type: ignore[arg-type]
|
|
|
|
|
slog(INFO, f'cast_str("{s}") = "{cast_str(s)}"') # type: ignore[arg-type]
|
|
|
|
|
slog(INFO, f'cast_str("{s}, {tp}") = "{cast_str(s, target_type=tp)}"') # type: ignore[arg-type]
|
2025-01-16 12:39:20 +01:00
|
|
|
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)
|