public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob f97fc4010dc225eb78ccf46dda6cbfaa798a90f5 1105 bytes (raw)

 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)

debug log:

solving f97fc401 ...
found f97fc401 in https://archives.passt.top/passt-dev/20230627025429.2209702-9-david@gibson.dropbear.id.au/

applying [1/1] https://archives.passt.top/passt-dev/20230627025429.2209702-9-david@gibson.dropbear.id.au/
diff --git a/test/tasst/typecheck.py b/test/tasst/typecheck.py
new file mode 100644
index 00000000..f97fc401

Checking patch test/tasst/typecheck.py...
Applied patch test/tasst/typecheck.py cleanly.

index at:
100644 f97fc4010dc225eb78ccf46dda6cbfaa798a90f5	test/tasst/typecheck.py

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).