Changeset View
Changeset View
Standalone View
Standalone View
contrib/buildbot/test/test_testutil.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# | # | ||||
# Copyright (c) 2020 The Bitcoin ABC developers | # Copyright (c) 2020 The Bitcoin ABC developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
import unittest | import unittest | ||||
from testutil import AnyWith | from testutil import AnyWith | ||||
class TestObject(): | class TestObject: | ||||
mystr = 'value' | mystr = "value" | ||||
mydict = { | mydict = { | ||||
'item': 'value', | "item": "value", | ||||
} | } | ||||
def TestAnyWith(expected): | def TestAnyWith(expected): | ||||
aw = AnyWith(TestObject, expected) | aw = AnyWith(TestObject, expected) | ||||
return aw.__eq__(TestObject()) | return aw.__eq__(TestObject()) | ||||
class TestUtilTests(unittest.TestCase): | class TestUtilTests(unittest.TestCase): | ||||
def test_compareWrongType(self): | def test_compareWrongType(self): | ||||
# dict is not a TestObject | # dict is not a TestObject | ||||
self.assertRaisesRegex( | self.assertRaisesRegex( | ||||
AssertionError, | AssertionError, | ||||
"Argument class type did not match", | "Argument class type did not match", | ||||
AnyWith( | AnyWith(TestObject, None).__eq__, | ||||
TestObject, | {}, | ||||
None).__eq__, | ) | ||||
{}) | |||||
def test_happyPaths(self): | def test_happyPaths(self): | ||||
self.assertRaisesRegex( | self.assertRaisesRegex( | ||||
AssertionError, "Argument missing expected attribute", TestAnyWith, { | AssertionError, | ||||
'does-not-exist': None}) | "Argument missing expected attribute", | ||||
TestAnyWith, | |||||
{"does-not-exist": None}, | |||||
) | |||||
self.assertRaisesRegex( | |||||
AssertionError, | |||||
"Argument missing expected attribute", | |||||
TestAnyWith, | |||||
{"does-not-exist": "value"}, | |||||
) | |||||
self.assertRaisesRegex( | self.assertRaisesRegex( | ||||
AssertionError, "Argument missing expected attribute", TestAnyWith, { | AssertionError, | ||||
'does-not-exist': 'value'}) | |||||
self.assertRaisesRegex(AssertionError, | |||||
"Argument missing expected attribute", | "Argument missing expected attribute", | ||||
TestAnyWith, | TestAnyWith, | ||||
{'does-not-exist': {'item': 'value'}}) | {"does-not-exist": {"item": "value"}}, | ||||
) | |||||
TestAnyWith({'mystr': 'value'}) | TestAnyWith({"mystr": "value"}) | ||||
self.assertRaisesRegex( | self.assertRaisesRegex( | ||||
AssertionError, "Argument attribute type did not match", TestAnyWith, { | AssertionError, | ||||
'mystr': None}) | "Argument attribute type did not match", | ||||
TestAnyWith, | |||||
{"mystr": None}, | |||||
) | |||||
self.assertRaisesRegex( | self.assertRaisesRegex( | ||||
AssertionError, "Argument attribute type did not match", TestAnyWith, { | AssertionError, | ||||
'mystr': {}}) | "Argument attribute type did not match", | ||||
TestAnyWith, | |||||
{"mystr": {}}, | |||||
) | |||||
self.assertRaisesRegex( | self.assertRaisesRegex( | ||||
AssertionError, "Argument attribute value did not match", TestAnyWith, { | AssertionError, | ||||
'mystr': 'wrong value'}) | "Argument attribute value did not match", | ||||
TestAnyWith, | |||||
{"mystr": "wrong value"}, | |||||
) | |||||
TestAnyWith({'mydict': { | TestAnyWith( | ||||
'item': 'value', | { | ||||
}}) | "mydict": { | ||||
"item": "value", | |||||
} | |||||
} | |||||
) | |||||
self.assertRaisesRegex( | self.assertRaisesRegex( | ||||
AssertionError, "Argument attribute type did not match", TestAnyWith, { | AssertionError, | ||||
'mydict': 'value'}) | "Argument attribute type did not match", | ||||
self.assertRaisesRegex(AssertionError, "Argument attribute value did not match", TestAnyWith, {'mydict': { | TestAnyWith, | ||||
'item-does-not-exist': 'value' | {"mydict": "value"}, | ||||
}}) | ) | ||||
self.assertRaisesRegex(AssertionError, "Argument attribute value did not match", TestAnyWith, {'mydict': { | self.assertRaisesRegex( | ||||
'item': None | AssertionError, | ||||
}}) | "Argument attribute value did not match", | ||||
self.assertRaisesRegex(AssertionError, "Argument attribute value did not match", TestAnyWith, {'mydict': { | TestAnyWith, | ||||
'item': 'wrong value' | {"mydict": {"item-does-not-exist": "value"}}, | ||||
}}) | ) | ||||
self.assertRaisesRegex( | |||||
AssertionError, | |||||
"Argument attribute value did not match", | |||||
TestAnyWith, | |||||
{"mydict": {"item": None}}, | |||||
) | |||||
self.assertRaisesRegex( | |||||
AssertionError, | |||||
"Argument attribute value did not match", | |||||
TestAnyWith, | |||||
{"mydict": {"item": "wrong value"}}, | |||||
) | |||||
if __name__ == '__main__': | if __name__ == "__main__": | ||||
unittest.main() | unittest.main() |