101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
from unittest import TestCase
|
|
|
|
from .context import Bit
|
|
|
|
class TestBit(TestCase):
|
|
def setUp(self):
|
|
self.true = ["1", 1, True]
|
|
self.false = ["0", 0, False]
|
|
pass
|
|
|
|
def test_init(self):
|
|
"""
|
|
Test creation of bit object
|
|
"""
|
|
for t in self.true:
|
|
self.assertTrue(Bit(t), f"Bit({t}) == True")
|
|
for f in self.false:
|
|
self.assertFalse(Bit(f), f"Bit({f}) == False")
|
|
|
|
def test_conversion(self):
|
|
"""
|
|
Test the conversion methods
|
|
"""
|
|
self.assertEqual(str(Bit(True)), "1", f'str(Bit(True)) == "1"')
|
|
self.assertEqual(str(Bit(False)), "0", f'str(Bit(False)) == "0"')
|
|
self.assertEqual(int(Bit(True)), 1, f'int(Bit(True)) == 1')
|
|
self.assertEqual(int(Bit(False)), 0, f'int(Bit(False)) == 0')
|
|
self.assertEqual(bool(Bit(True)), True, f'bool(Bit(True)) == True')
|
|
self.assertEqual(bool(Bit(False)), False, f'bool(Bit(False)) == False')
|
|
|
|
def test_comparison(self):
|
|
"""
|
|
Test the comparison operators
|
|
"""
|
|
self.assertNotEqual(Bit(False), Bit(True), 'Bit(False) != Bit(True)')
|
|
self.assertNotEqual(Bit(True), Bit(False), 'Bit(True) != Bit(False)')
|
|
self.assertLess(Bit(False), Bit(True), 'Bit(False) < Bit(True)')
|
|
self.assertGreater(Bit(True), Bit(False), 'Bit(True) > Bit(False)')
|
|
self.assertLessEqual(Bit(False), Bit(True), 'Bit(False) <= Bit(True)')
|
|
self.assertLessEqual(Bit(True), Bit(True), 'Bit(True) <= Bit(True)')
|
|
self.assertLessEqual(Bit(False), Bit(False),
|
|
'Bit(False) <= Bit(False)')
|
|
self.assertGreaterEqual(Bit(True), Bit(False),
|
|
'Bit(True) >= Bit(False)')
|
|
self.assertGreaterEqual(Bit(True), Bit(True),
|
|
'Bit(True) >= Bit(True)')
|
|
self.assertGreaterEqual(Bit(False), Bit(False),
|
|
'Bit(False) >= Bit(False)')
|
|
|
|
def test_lie(self):
|
|
"""
|
|
Test the lie function/property
|
|
"""
|
|
self.assertFalse(Bit(True).lie, "Bit(True).lie == False")
|
|
self.assertTrue(Bit(False).lie, "Bit(False).lie == True")
|
|
|
|
def test_toggle(self):
|
|
"""
|
|
Test the toggle function
|
|
"""
|
|
var = Bit(True)
|
|
var.toggle()
|
|
self.assertFalse(var, "Bit(True).toggle() == False")
|
|
var.toggle()
|
|
self.assertTrue(var, "Bit(False).toggle() == True")
|
|
|
|
def test_set(self):
|
|
"""Test the set() method"""
|
|
var = Bit(False)
|
|
var.set()
|
|
self.assertTrue(var, "Bit(False).set() == True")
|
|
var = Bit(True)
|
|
var.set()
|
|
self.assertTrue(var, "Bit(True).set() == True")
|
|
|
|
def test_unset(self):
|
|
"""Test the unset() method"""
|
|
var = Bit(True)
|
|
var.unset()
|
|
self.assertFalse(var, "Bit(True).unset() == False")
|
|
var = Bit(False)
|
|
var.unset()
|
|
self.assertFalse(var, "Bit(False).unset() == False")
|
|
|
|
def test_errors(self):
|
|
"""
|
|
Test that errors are raised for invalid values
|
|
"""
|
|
with self.subTest("Raise TypeError"):
|
|
self.assertRaises(TypeError, Bit, 12.7)
|
|
self.assertRaises(TypeError, Bit, b'xyz')
|
|
self.assertRaises(TypeError, Bit, [True, False])
|
|
self.assertRaises(TypeError, Bit, [True])
|
|
|
|
with self.subTest("Raise ValueError"):
|
|
self.assertRaises(ValueError, Bit, "2")
|
|
self.assertRaises(ValueError, Bit, 2)
|
|
self.assertRaises(ValueError, Bit, -1)
|
|
self.assertRaises(ValueError, Bit, "-2")
|
|
|