import unittest

from distance.level import Level
from distance.bytes import DstBytes, UnexpectedEOFError
from distance.printing import PrintContext
from .common import check_exceptions


class LevelTest(unittest.TestCase):

    def test_level(self):
        with open("tests/in/level/test-straightroad.bytes", 'rb') as f:
            level = Level(DstBytes(f))
            self.assertEqual(level.level_name, "Test-straightroad")

    def test_print_data(self):
        p = PrintContext.for_test()
        with open("tests/in/level/test-straightroad.bytes", 'rb') as f:
            p.print_data_of(Level(DstBytes(f)))

    def test_truncated(self):
        with open("tests/in/level/test-straightroad_truncated.bytes", 'rb') as f:
            dbytes = DstBytes(f)
            level = Level(dbytes)
            self.assertEqual(level.level_name, "Test-straightroad")
            results = [level.settings] + list(level.iter_objects())
            self.assertEqual(len(results), 3)

    def test_truncated2(self):
        with open("tests/in/level/test-straightroad_truncated_2.bytes", 'rb') as f:
            dbytes = DstBytes(f)
            level = Level(dbytes)
            self.assertEqual(level.level_name, "Test-straightroad")
            gen = level.iter_objects()
            next(gen)
            obj = next(gen)
            self.assertEqual(UnexpectedEOFError, type(obj.exception))

    def test_invalid_str(self):
        with open("tests/in/level/invalid-groupname.bytes", 'rb') as f:
            level = Level(DstBytes(f))
            self.assertEqual(level.level_name, "Test Group")
            results = list(level.iter_objects())
            self.assertEqual(len(results), 5)
            for i, obj in enumerate(results):
                self.assertIsNotNone(obj, f"i == {i}")
                self.assertTrue(obj.sane_end_pos, f"i == {i}")
                if i == 2:
                    self.assertRaises(UnicodeError, check_exceptions, obj)
                else:
                    check_exceptions(obj)


if __name__ == '__main__':
    unittest.main()


# vim:set sw=4 ts=8 sts=4 et sr ft=python fdm=marker tw=0:

@@ -73,18 +73,8 @@
 port
- DstBytes,
  Une
@@ -238,33 +238,37 @@
 f):%0A        
-with open
+level = Level
 (%22tests/in/l
@@ -300,65 +300,10 @@
 tes%22
-, 'rb') as f:%0A            level = Level(DstBytes(f))%0A    
+)%0A
     
@@ -434,33 +434,45 @@
 t()%0A        
-with open
+p.print_data_of(Level
 (%22tests/in/l
@@ -504,67 +504,8 @@
 tes%22
-, 'rb') as f:%0A            p.print_data_of(Level(DstBytes(f)
 ))%0A%0A
@@ -538,33 +538,37 @@
 f):%0A        
-with open
+level = Level
 (%22tests/in/l
@@ -610,93 +610,10 @@
 tes%22
-, 'rb') as f:%0A            dbytes = DstBytes(f)%0A            level = Level(dbytes)%0A    
+)%0A
     
@@ -664,36 +664,32 @@
 -straightroad%22)%0A
-    
         results 
@@ -736,36 +736,32 @@
 ects())%0A        
-  
-  
 self.assertEqual
@@ -814,33 +814,37 @@
 f):%0A        
-with open
+level = Level
 (%22tests/in/l
@@ -888,93 +888,10 @@
 tes%22
-, 'rb') as f:%0A            dbytes = DstBytes(f)%0A            level = Level(dbytes)%0A    
+)%0A
     
@@ -958,20 +958,16 @@
         
-   
- 
 gen = le
@@ -993,20 +993,16 @@
         
-    
 next(gen
@@ -1011,20 +1011,16 @@
         
-  
-  
 obj = ne
@@ -1023,28 +1023,24 @@
 = next(gen)%0A
-    
         self
@@ -1138,17 +1138,21 @@
     
-with open
+level = Level
 (%22te
@@ -1192,65 +1192,10 @@
 tes%22
-, 'rb') as f:%0A            level = Level(DstBytes(f))%0A    
+)%0A
     
@@ -1251,28 +1251,24 @@
 p%22)%0A        
-    
 results = li
@@ -1292,36 +1292,32 @@
 ects())%0A        
-  
-  
 self.assertEqual
@@ -1334,20 +1334,16 @@
 ts), 5)%0A
-    
         
@@ -1380,36 +1380,32 @@
 s):%0A            
-  
-  
 self.assertIsNot
@@ -1419,36 +1419,32 @@
 j, f%22i == %7Bi%7D%22)%0A
-    
             self
@@ -1498,20 +1498,16 @@
         
- 
-   
 if i == 
@@ -1509,20 +1509,16 @@
 i == 2:%0A
-    
         
@@ -1596,22 +1596,14 @@
     
-    
 else:%0A
-    
     

use more implicit DstBytes constructor