1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| | #! /usr/bin/env avocado-runner-avocado-classless
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright Red Hat
# Author: David Gibson <david@gibson.dropbear.id.au>
"""
Test A Simple Socket Transport
Type checking and related helpers
"""
from avocado_classless.test import test, assert_eq, assert_raises
def typecheck(val, ty_):
"""typecheck(val, ty_)
Return val, raising TypeError if it does not have type ty_.
"""
if not isinstance(val, ty_):
raise TypeError(f'Expected {ty_} instead of {type(val)}')
return val
@test
def test_typecheck():
assert_eq(typecheck(17, int), 17)
assert_eq(typecheck("hello", str), "hello")
assert_raises(TypeError, typecheck, 17, str)
def typecheck_default(val, ty_, default):
"""typecheck_default(val, ty_, default)
If val is None, return default. Otherwise return typecheck(val, ty_).
"""
if val is None:
return default
return typecheck(val, ty_)
@test
def test_typecheck_default():
assert_eq(typecheck_default(17, int, None), 17)
assert_eq(typecheck_default(None, int, 17), 17)
|