test_sane.py (8255B)
1 # standard imports 2 import unittest 3 import logging 4 5 # external imports 6 import hexathon 7 8 #logging.basicConfig(level=logging.DEBUG) 9 logg = logging.getLogger() 10 11 12 class TestSane(unittest.TestCase): 13 14 def setUp(self): 15 self.v_invalid = 'foo' 16 self.v_empty = '' 17 self.v_zero_pad = '00' 18 self.v_zero = '0' 19 self.v_value_pad = '0BA4' 20 self.v_value = 'BA4' 21 self.v_value_zerox_pad = '0x0BA4' 22 self.v_value_zerox = '0xBA4' 23 self.v_value_pad_long = '00BA4' 24 self.v_value_pad_long_even = '000BA4' 25 self.expect = { 26 'invalid': ValueError, 27 'empty': ValueError, 28 'zero_pad': None, 29 'zero': None, 30 'value_pad': None, 31 'value': None, 32 'value_zerox_pad': None, 33 'value_zerox': None, 34 'value_pad_long': None, 35 'value_pad_long_even': None, 36 } 37 38 39 def __check(self, m, **kwargs): 40 for k in self.expect.keys(): 41 v = getattr(self, 'v_' + k) 42 logg.debug('trying k {} v {} kwargs {} expect {}'.format(k, v, kwargs, self.expect[k])) 43 if self.expect[k] == None: 44 m(v, **kwargs) 45 else: 46 try: 47 if issubclass(self.expect[k], BaseException): 48 with self.assertRaises(self.expect[k]): 49 m(v, **kwargs) 50 except TypeError: 51 r = m(v, **kwargs) 52 self.assertEqual(r, self.expect[k]) 53 54 55 def test_valid(self): 56 self.expect['zero'] = ValueError 57 self.expect['value'] = ValueError 58 self.expect['value_zerox_pad'] = ValueError 59 self.expect['value_zerox'] = ValueError 60 self.expect['value_pad_long'] = ValueError 61 self.__check(hexathon.valid) 62 63 64 def test_valid_compact(self): 65 self.expect['value_zerox_pad'] = ValueError 66 self.expect['value_zerox'] = ValueError 67 self.__check(hexathon.valid, allow_compact=True) 68 69 70 71 def test_even(self): 72 self.expect['value_zerox_pad'] = ValueError 73 self.expect['value_zerox'] = ValueError 74 self.__check(hexathon.even) 75 76 77 def test_even_empty(self): 78 self.expect['value_zerox_pad'] = ValueError 79 self.expect['value_zerox'] = ValueError 80 self.expect['empty'] = None 81 self.__check(hexathon.even, allow_empty=True) 82 83 84 def test_even_compact(self): 85 self.expect['value_zerox_pad'] = ValueError 86 self.expect['value_zerox'] = ValueError 87 self.__check(hexathon.even, allow_compact=True) 88 89 90 def test_even_compact_empty(self): 91 self.expect['empty'] = '' 92 self.expect['value_zerox_pad'] = ValueError 93 self.expect['value_zerox'] = ValueError 94 self.__check(hexathon.even, allow_empty=True, allow_compact=True) 95 96 97 def test_uniform(self): 98 self.expect['value'] = '0ba4' 99 self.expect['value_pad'] = '0ba4' 100 self.expect['value_zerox_pad'] = ValueError 101 self.expect['value_zerox'] = ValueError 102 self.__check(hexathon.uniform) 103 104 105 def test_strip_0x(self): 106 self.expect['zero'] = '00' 107 self.expect['zero_pad'] = '00' 108 self.expect['value'] = '0BA4' 109 self.expect['value_pad'] = '0BA4' 110 self.expect['value_zerox'] = '0BA4' 111 self.__check(hexathon.strip_0x) 112 113 114 def test_strip_0x_compact(self): 115 self.expect['zero'] = '0' 116 self.expect['zero_pad'] = '0' 117 self.expect['value'] = 'BA4' 118 self.expect['value_pad'] = 'BA4' 119 self.expect['value_zerox'] = 'BA4' 120 self.__check(hexathon.strip_0x, compact_value=True) 121 122 123 def test_strip_0x_empty(self): 124 self.expect['empty'] = '' 125 self.expect['zero'] = '00' 126 self.expect['zero_pad'] = '00' 127 self.expect['value'] = '0BA4' 128 self.expect['value_pad'] = '0BA4' 129 self.expect['value_zerox'] = '0BA4' 130 self.__check(hexathon.strip_0x, allow_empty=True) 131 132 133 def test_strip_0x_compact_empty(self): 134 self.expect['empty'] = '' 135 self.expect['zero'] = '0' 136 self.expect['zero_pad'] = '0' 137 self.expect['value'] = 'BA4' 138 self.expect['value_pad'] = 'BA4' 139 self.expect['value_zerox'] = 'BA4' 140 self.expect['value_pad_long'] = 'BA4' 141 self.expect['value_pad_long_even'] = 'BA4' 142 self.__check(hexathon.strip_0x, allow_empty=True, compact_value=True) 143 144 145 def test_add_0x(self): 146 self.expect['value'] = '0x0BA4' 147 self.expect['zero'] = '0x00' 148 self.expect['zero_pad'] = '0x00' 149 self.expect['value_pad'] = '0x0BA4' 150 self.expect['value_zerox'] = '0x0BA4' 151 self.expect['value_pad_long'] = '0x000BA4' 152 self.expect['value_pad_long_even'] = '0x000BA4' 153 self.__check(hexathon.add_0x) 154 155 156 def test_add_0x_compact(self): 157 self.expect['value'] = '0xBA4' 158 self.expect['zero'] = '0x0' 159 self.expect['zero_pad'] = '0x0' 160 self.expect['value_pad'] = '0xBA4' 161 self.expect['value_zerox'] = '0xBA4' 162 self.expect['value_pad_long'] = '0xBA4' 163 self.expect['value_pad_long_even'] = '0xBA4' 164 self.__check(hexathon.add_0x, compact_value=True) 165 166 167 def test_add_0x_empty(self): 168 self.expect['empty'] = '0x' 169 self.expect['value'] = '0x0BA4' 170 self.expect['value_pad'] = '0x0BA4' 171 self.expect['value_zerox'] = '0x0BA4' 172 self.expect['value_pad_long'] = '0x000BA4' 173 self.expect['value_pad_long_even'] = '0x000BA4' 174 self.__check(hexathon.add_0x, allow_empty=True) 175 176 177 def test_add_0x_compact_empty(self): 178 self.expect['empty'] = '0x' 179 self.expect['value'] = '0xBA4' 180 self.expect['zero'] = '0x0' 181 self.expect['zero_pad'] = '0x0' 182 self.expect['value_pad'] = '0xBA4' 183 self.expect['value_zerox'] = '0xBA4' 184 self.expect['value_pad_long'] = '0xBA4' 185 self.expect['value_pad_long_even'] = '0xBA4' 186 self.__check(hexathon.add_0x, allow_empty=True, compact_value=True) 187 188 189 def test_compact(self): 190 self.expect['value_zerox_pad'] = ValueError 191 self.expect['value_zerox'] = ValueError 192 self.expect['value_pad'] = 'BA4' 193 self.expect['zero_pad'] = '0' 194 self.__check(hexathon.compact) 195 196 197 def test_unpad(self): 198 self.expect['value_zerox_pad'] = ValueError 199 self.expect['value_zerox'] = ValueError 200 self.expect['value_pad'] = '0BA4' 201 self.expect['value_pad_long'] = '0BA4' 202 self.__check(hexathon.unpad) 203 204 205 def test_pad_noop(self): 206 self.expect['zero'] = ValueError 207 self.expect['value'] = ValueError 208 self.expect['value_zerox_pad'] = ValueError 209 self.expect['value_zerox'] = ValueError 210 self.expect['value_pad'] = '0BA4' 211 self.expect['value_pad_long'] = ValueError 212 self.expect['value_pad_long_even'] = '000BA4' 213 self.__check(hexathon.pad) 214 215 216 def test_pad_noop_compact(self): 217 self.expect['zero'] = '00' 218 self.expect['value'] = '0BA4' 219 self.expect['value_zerox_pad'] = ValueError 220 self.expect['value_zerox'] = ValueError 221 self.expect['value_pad_long_even'] = '000BA4' 222 self.__check(hexathon.pad, allow_compact=True) 223 224 225 def test_pad_to_four_bytes(self): 226 #self.expect['zero'] = '00000000' 227 self.expect['zero'] = ValueError 228 self.expect['value'] = ValueError 229 self.expect['value_pad_long'] = ValueError 230 self.expect['value_zerox_pad'] = ValueError 231 self.expect['value_zerox'] = ValueError 232 self.expect['value_pad'] = '00000BA4' 233 self.expect['value_pad_long_even'] = '00000BA4' 234 self.__check(hexathon.pad, byte_length=4) 235 236 237 def test_pad_to_four_bytes_compact(self): 238 self.expect['zero'] = '00000000' 239 self.expect['value'] = '00000BA4' 240 self.expect['value_pad_long'] = '00000BA4' 241 self.expect['value_zerox_pad'] = ValueError 242 self.expect['value_zerox'] = ValueError 243 self.expect['value_pad'] = '00000BA4' 244 self.expect['value_pad_long_even'] = '00000BA4' 245 self.__check(hexathon.pad, byte_length=4, allow_compact=True) 246 247 248 if __name__ == '__main__': 249 unittest.main()