diff python/testhexfile.py @ 244:58155c7c4a8e

Add hexutil
author Windel Bouwman
date Wed, 24 Jul 2013 19:47:13 +0200
parents d3dccf12ca88
children 66912720d712
line wrap: on
line diff
--- a/python/testhexfile.py	Tue Jul 23 16:50:02 2013 +0200
+++ b/python/testhexfile.py	Wed Jul 24 19:47:13 2013 +0200
@@ -1,39 +1,66 @@
 import unittest
 import io
-import hexfile
+from hexfile import HexFile, HexFileException
 
 
 class testHexFile(unittest.TestCase):
-    def setUp(self):
-        pass
-
+    def saveload(self, hf):
+        f = io.StringIO()
+        hf.save(f)
+        hf2 = HexFile()
+        hf2.load(io.StringIO(f.getvalue()))
+        self.assertEqual(hf, hf2)
+        
     def testSave(self):
-        hf = hexfile.HexFile()
-        f = io.StringIO()
-        region = hexfile.HexFileRegion(0x8000, bytes.fromhex('aabbcc'))
-        hf.regions.append(region)
-        hf.save(f)
+        hf = HexFile()
+        hf.addRegion(0x8000, bytes.fromhex('aabbcc'))
+        self.saveload(hf)
+
+    def testEqual(self):
+        hf1 = HexFile()
+        hf2 = HexFile()
+        hf1.addRegion(10, bytes.fromhex('aabbcc'))
+        hf2.addRegion(10, bytes.fromhex('aabbcc'))
+        self.assertEqual(hf1, hf2)
+
+    def testNotEqual(self):
+        hf1 = HexFile()
+        hf2 = HexFile()
+        hf1.addRegion(10, bytes.fromhex('aabbcc'))
+        hf2.addRegion(10, bytes.fromhex('aabbdc'))
+        self.assertNotEqual(hf1, hf2)
+        
+    def testNotEqual2(self):
+        hf1 = HexFile()
+        hf2 = HexFile()
+        hf1.addRegion(10, bytes.fromhex('aabbcc'))
+        hf2.addRegion(10, bytes.fromhex('aabbcc'))
+        hf2.addRegion(22, bytes.fromhex('aabbcc'))
+        self.assertNotEqual(hf1, hf2)
 
     def testLoad(self):
-        hf = hexfile.HexFile()
+        hf = HexFile()
         dummyhex = """
         :01400000aa15
         """
         f = io.StringIO(dummyhex)
         hf.load(f)
+        self.assertEqual(1, len(hf.regions))
+        self.assertEqual(0x4000, hf.regions[0].address)
+        self.assertSequenceEqual(bytes.fromhex('aa'), hf.regions[0].data)
 
     def testIncorrectCrc(self):
-        hf = hexfile.HexFile()
+        hf = HexFile()
         txt = ":01400000aabb"
         f = io.StringIO(txt)
-        with self.assertRaises(hexfile.HexFileException):
+        with self.assertRaisesRegex(HexFileException, 'crc'):
             hf.load(f)
 
     def testIncorrectLength(self):
-        hf = hexfile.HexFile()
+        hf = HexFile()
         txt = ":0140002200aabb"
         f = io.StringIO(txt)
-        with self.assertRaises(hexfile.HexFileException):
+        with self.assertRaisesRegex(HexFileException, 'count'):
             hf.load(f)
 
 if __name__ == '__main__':