|
| 1 | +from inspect import getmembers, ismethod |
| 2 | +from itertools import chain |
| 3 | +from six.moves import zip |
| 4 | + |
| 5 | +from moretools import camelize, isidentifier |
| 6 | + |
| 7 | +from robot.utils import normalize |
| 8 | +from robot.errors import DataError |
| 9 | + |
| 10 | +import robottools.library.inspector |
| 11 | +from robottools import TestLibraryInspector, TestLibraryImportError |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | + |
| 16 | +class TestInspector(object): |
| 17 | + """Tests for :class:`robottools.TestLibraryInspector`. |
| 18 | + """ |
| 19 | + def test_class(self): |
| 20 | + assert TestLibraryInspector \ |
| 21 | + is robottools.library.inspector.TestLibraryInspector |
| 22 | + assert type(TestLibraryInspector) \ |
| 23 | + is robottools.library.inspector.TestLibraryInspectorMeta |
| 24 | + |
| 25 | + def test__init__(self, stdlibname, stdlib): |
| 26 | + inspector = TestLibraryInspector(stdlibname) |
| 27 | + assert type(inspector._library.get_instance()) is type(stdlib) |
| 28 | + |
| 29 | + def test_name(self, inspector, stdlibname): |
| 30 | + assert inspector.name == stdlibname |
| 31 | + |
| 32 | + def test__iter__(self, inspector, stdlib, stdlib_kwfuncnames): |
| 33 | + kwnames = [camelize(n, joiner=' ') for n in stdlib_kwfuncnames] |
| 34 | + for keyword in iter(inspector): |
| 35 | + assert isinstance( |
| 36 | + keyword, robottools.library.inspector.KeywordInspector) |
| 37 | + kwnames.remove(keyword.name) |
| 38 | + assert not kwnames |
| 39 | + |
| 40 | + def test__dir__(self, inspector, stdlib): |
| 41 | + # filter keyword methods from directly instantiated stdlib |
| 42 | + kwfuncnames = [name for name, obj in getmembers(stdlib) |
| 43 | + if name[0] != '_' and ismethod(obj)] |
| 44 | + # and check if __dir__ contains their camelized names |
| 45 | + assert set(dir(inspector)) \ |
| 46 | + == set(chain(map(camelize, kwfuncnames), |
| 47 | + # as well as all other members |
| 48 | + super(TestLibraryInspector, inspector).__dir__())) |
| 49 | + |
| 50 | + def test__getattr__(self, inspector, stdlib): |
| 51 | + # filter keyword methods from directly instantiated stdlib |
| 52 | + kwfuncnames = [name for name, obj in getmembers(stdlib) |
| 53 | + if name[0] != '_' and ismethod(obj)] |
| 54 | + # and check that __getattr__ works with with keyword method names |
| 55 | + # as well as with normalized and camelized keyword names |
| 56 | + for name, normalized, camelized in zip( |
| 57 | + kwfuncnames, map(normalize, kwfuncnames), |
| 58 | + map(camelize, kwfuncnames) |
| 59 | + ): |
| 60 | + keyword, from_normalized, from_camelized = ( |
| 61 | + getattr(inspector, n) |
| 62 | + for n in [name, normalized, camelized]) |
| 63 | + assert keyword == from_normalized == from_camelized |
| 64 | + for keyword in [keyword, from_normalized, from_camelized]: |
| 65 | + assert isinstance( |
| 66 | + keyword, robottools.library.inspector.KeywordInspector) |
| 67 | + assert keyword.name == camelize(name, joiner=' ') |
| 68 | + |
| 69 | + def test__getattr__error(self, inspector): |
| 70 | + with pytest.raises(AttributeError) as exc: |
| 71 | + getattr(inspector, 'Invalid') |
| 72 | + # and check that exception message contains |
| 73 | + # RFW's original exception message |
| 74 | + with pytest.raises(DataError) as robot_exc: |
| 75 | + if hasattr(inspector._library, 'get_handler'): |
| 76 | + # robot < 2.9 |
| 77 | + inspector._library.get_handler('Invalid') |
| 78 | + else: |
| 79 | + inspector._library.handlers['Invalid'] |
| 80 | + assert str(robot_exc.value) in str(exc.value) |
| 81 | + |
| 82 | + def test__repr__(self, inspector, stdlibname): |
| 83 | + assert repr(inspector) == "[Library] %s" % stdlibname |
0 commit comments