Browse Source

Merge pull request #22 from tomlowshang/trailing-slash-2

Bastien Sevajol 6 years ago
parent
commit
5a50a523a9
No account linked to committer's email
2 changed files with 27 additions and 12 deletions
  1. 17 12
      synergine2_cocos2d/middleware.py
  2. 10 0
      tests/test_middleware.py

+ 17 - 12
synergine2_cocos2d/middleware.py View File

1
 # coding: utf-8
1
 # coding: utf-8
2
 import os
2
 import os
3
+import typing
3
 
4
 
4
-import cocos
5
 from synergine2.config import Config
5
 from synergine2.config import Config
6
 from synergine2.log import get_logger
6
 from synergine2.log import get_logger
7
+from synergine2_cocos2d.util import get_map_file_path_from_dir
8
+
9
+if typing.TYPE_CHECKING:
10
+    import cocos
7
 
11
 
8
 
12
 
9
 class MapMiddleware(object):
13
 class MapMiddleware(object):
18
         self.tmx = None
22
         self.tmx = None
19
 
23
 
20
     def get_map_file_path(self) -> str:
24
     def get_map_file_path(self) -> str:
21
-        return os.path.join(
22
-            self.map_dir_path,
23
-            '{}.tmx'.format(os.path.basename(self.map_dir_path)),
24
-        )
25
+        return get_map_file_path_from_dir(self.map_dir_path)
25
 
26
 
26
     def init(self) -> None:
27
     def init(self) -> None:
28
+        # import cocos here for prevent test crash when no X server is
29
+        # present
30
+        import cocos
31
+
27
         map_file_path = self.get_map_file_path()
32
         map_file_path = self.get_map_file_path()
28
         self.tmx = cocos.tiles.load(map_file_path)
33
         self.tmx = cocos.tiles.load(map_file_path)
29
 
34
 
30
-    def get_background_sprite(self) -> cocos.sprite.Sprite:
35
+    def get_background_sprite(self) -> 'cocos.sprite.Sprite':
31
         raise NotImplementedError()
36
         raise NotImplementedError()
32
 
37
 
33
-    def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
38
+    def get_ground_layer(self) -> 'cocos.tiles.RectMapLayer':
34
         raise NotImplementedError()
39
         raise NotImplementedError()
35
 
40
 
36
-    def get_top_layer(self) -> cocos.tiles.RectMapLayer:
41
+    def get_top_layer(self) -> 'cocos.tiles.RectMapLayer':
37
         raise NotImplementedError()
42
         raise NotImplementedError()
38
 
43
 
39
     def get_world_height(self) -> int:
44
     def get_world_height(self) -> int:
50
 
55
 
51
 
56
 
52
 class TMXMiddleware(MapMiddleware):
57
 class TMXMiddleware(MapMiddleware):
53
-    def get_background_sprite(self) -> cocos.sprite.Sprite:
58
+    def get_background_sprite(self) -> 'cocos.sprite.Sprite':
54
         # TODO: Extract it from tmx
59
         # TODO: Extract it from tmx
55
         return cocos.sprite.Sprite(os.path.join(
60
         return cocos.sprite.Sprite(os.path.join(
56
             self.map_dir_path,
61
             self.map_dir_path,
57
             'background.png',
62
             'background.png',
58
         ))
63
         ))
59
 
64
 
60
-    def get_interior_sprite(self) -> cocos.sprite.Sprite:
65
+    def get_interior_sprite(self) -> 'cocos.sprite.Sprite':
61
         # TODO: Extract it from tmx
66
         # TODO: Extract it from tmx
62
         return cocos.sprite.Sprite(os.path.join(
67
         return cocos.sprite.Sprite(os.path.join(
63
             self.map_dir_path,
68
             self.map_dir_path,
64
             'background_interiors.png',
69
             'background_interiors.png',
65
         ))
70
         ))
66
 
71
 
67
-    def get_ground_layer(self) -> cocos.tiles.RectMapLayer:
72
+    def get_ground_layer(self) -> 'cocos.tiles.RectMapLayer':
68
         assert self.tmx
73
         assert self.tmx
69
         return self.tmx['ground']
74
         return self.tmx['ground']
70
 
75
 
71
-    def get_top_layer(self) -> cocos.tiles.RectMapLayer:
76
+    def get_top_layer(self) -> 'cocos.tiles.RectMapLayer':
72
         assert self.tmx
77
         assert self.tmx
73
         return self.tmx['top']
78
         return self.tmx['top']
74
 
79
 

+ 10 - 0
tests/test_middleware.py View File

1
+from synergine2.config import Config
2
+from synergine2_cocos2d.middleware import MapMiddleware
3
+
4
+class TestMapMiddleware:
5
+    def test_get_map_file_path(self):
6
+        test = MapMiddleware(Config(), 'map/003')
7
+        assert test.get_map_file_path() == 'map/003/003.tmx'
8
+        test = MapMiddleware(Config(), 'map/003/')
9
+        assert test.get_map_file_path() == 'map/003/003.tmx'
10
+