from unittest import TestCase from easybits import Bits from easybits.util import is_bit_string from easybits.errors import NotEnoughBits, IntegersRequireLength class TestBits(TestCase): def test_is_bit_string(self): self.assertTrue(is_bit_string('10 10')) self.assertFalse(is_bit_string('T')) def test_init_with_bitstring(self): Bits('10101010') def test_init_with_bitstring_and_length(self): self.assertEqual(len(Bits('10101010', length=16)), 16) with self.assertRaises(NotEnoughBits): Bits('10101010', length=4) def test_init_with_bytes(self): b = Bits(b'a') self.assertEqual(len(b), 8) def test_init_with_ascii(self): b = Bits('Chris') def test_init_with_utf8(self): b = Bits('😍', encoding='utf8') def test_init_with_positive_int(self): b = Bits(12, length=16) def test_init_with_negative_int(self): b = Bits(-12, length=16) with self.assertRaises(IntegersRequireLength): Bits(-12) def test_int(self): b = Bits(-12, length=16) self.assertEqual(-12, b.int) def test_bytes(self): _bytes = '😍'.encode('utf8') b = Bits(_bytes) self.assertEqual(b.bytes, _bytes) def test_ascii(self): b = Bits('Chris') self.assertEqual('Chris', b.ascii) def test_force_encoded_text(self): b = Bits('1', encoding='ascii') self.assertEqual(len(b), 8)