示例#1
0
def test_typed_field_convertible():
    class FieldTester:
        fieldA = fields.TypedField(int, str)
        fieldB = fields.TypedField(str, int)
        fieldC = fields.TypedField(int)

    tester = FieldTester()
    tester.fieldA = fields.make_convertible('10')
    tester.fieldB = fields.make_convertible('10')
    assert tester.fieldA == 10
    assert tester.fieldB == '10'

    with pytest.raises(TypeError):
        tester.fieldC = fields.make_convertible(None)
示例#2
0
    def _set_defaults(self, test_registry):
        if test_registry is None:
            return

        unset_vars = {}
        for test in test_registry:
            for name, val in self._external_vars.items():
                if '.' in name:
                    testname, varname = name.split('.', maxsplit=1)
                else:
                    testname, varname = test.__name__, name

                if testname == test.__name__:
                    # Treat special values
                    if val == '@none':
                        val = None
                    else:
                        val = fields.make_convertible(val)

                    if not test.setvar(varname, val):
                        unset_vars.setdefault(test.__name__, [])
                        unset_vars[test.__name__].append(varname)

        # Warn for all unset variables
        for testname, varlist in unset_vars.items():
            varlist = ', '.join(f'{v!r}' for v in varlist)
            getlogger().warning(
                f'test {testname!r}: '
                f'the following variables were not set: {varlist}')