def _t(inputs, outputs): from mtots import test import contextlib contents = dict(inputs) @contextlib.contextmanager def open(name, mode='r'): if mode == 'r': yield FakeFile('r', contents[name]) elif mode == 'w': fake_file = FakeFile('w', '') yield fake_file contents[name] = fake_file.read() else: assert False, mode main(open) for filename in outputs: test.that(filename in contents, f'File {filename} missing') test.equal( f'FILE{filename}:{contents[filename]}', f'FILE{filename}:{outputs[filename]}', )
def test_inheritance(): @dataclass class A: a: int @dataclass class B(A): b: str b = B(10, 'a') test.equal(b.a, 10) test.equal(b.b, 'a') a = A(76) test.equal(a.a, 76) test.that(not hasattr(a, 'b'))
def test_struct(): class Foo(typing.NamedTuple): abc: float xyz: str foo_parser = Struct(Foo, [ ['abc', 'NUMBER'], '+', ['xyz', 'NAME'], ]) def parse(parser, text): return parser.parse(test_lexer.lex_string(text)) test.equal( parse(foo_parser, "924 + hi"), Success(None, Foo(924, 'hi')), ) class Bar(typing.NamedTuple): mark: base.Mark abc: float xyz: str bar_parser = Struct(Bar, [ ['abc', 'NUMBER'], '+', ['xyz', 'NAME'], ], include_mark=True) m = parse(bar_parser, "924 + hi") test.that(isinstance(m.mark, base.Mark)) test.that(isinstance(m, Success)) test.that(hasattr(m.value, 'mark')) test.equal( m, Success(None, Bar(m.value.mark, 924, 'hi')), )
def test_optional(): test.that(isinstance(51.1, Optional)) test.that(isinstance(None, Optional)) test.that(not isinstance(10, Optional[str])) test.that(isinstance('hi', Optional[str])) test.that(isinstance(None, Optional[str]))
def test_list(): test.that(isinstance([], List)) test.that(isinstance([], List[int])) test.that(not isinstance(['hi'], List[int])) test.that(isinstance([51], List[int]))
def test_tuple(): test.that(isinstance((), Tuple)) test.that(isinstance((), Tuple[()])) test.that(isinstance((5, ), Tuple[int])) test.that(not isinstance((5, ), Tuple[int, int])) test.that(isinstance((5, 10), Tuple[int, int])) test.that(not isinstance((5, '10'), Tuple[int, int]))
def test_union(): test.that(isinstance('hi', Union[int, str])) test.that(isinstance(15, Union[int, str])) test.that(not isinstance(15.0, Union[int, str]))