test_state.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # coding: utf-8
  2. from collections import OrderedDict
  3. import time
  4. import pytest
  5. from synergine2.config import Config
  6. from synergine2_cocos2d.const import SELECTION_COLOR_RGB
  7. from opencombat.exception import StateLoadError
  8. from opencombat.simulation.base import TileStrategySimulation
  9. from opencombat.simulation.base import TileStrategySubjects
  10. from opencombat.simulation.subject import ManSubject
  11. from opencombat.state import StateConstructorBuilder, StateDumper
  12. from opencombat.state import StateLoader
  13. from opencombat.const import FLAG
  14. from opencombat.const import SIDE
  15. from opencombat.const import FLAG_DE
  16. from opencombat.const import DE_COLOR
  17. from opencombat.const import USSR_COLOR
  18. from opencombat.const import FLAG_USSR
  19. from opencombat.const import SIDE_ALLIES
  20. from opencombat.const import SIDE_AXIS
  21. class MyStateLoader(StateLoader):
  22. pass
  23. @pytest.fixture
  24. def state_loader(config, simulation):
  25. return StateLoader(config, simulation)
  26. @pytest.fixture
  27. def simulation_for_dump(config) -> TileStrategySimulation:
  28. simulation = TileStrategySimulation(
  29. config,
  30. 'tests/fixtures/map_a/map_a.tmx',
  31. )
  32. subjects = TileStrategySubjects(simulation=simulation)
  33. simulation.subjects = subjects
  34. man1 = ManSubject(config, simulation)
  35. man1.position = (10, 11)
  36. man1.direction = 42
  37. man1.properties = OrderedDict([
  38. (SELECTION_COLOR_RGB, DE_COLOR),
  39. (FLAG, FLAG_DE),
  40. (SIDE, SIDE_AXIS),
  41. ])
  42. man2 = ManSubject(config, simulation)
  43. man2.position = (16, 8)
  44. man2.direction = 197
  45. man2.properties = OrderedDict([
  46. (SELECTION_COLOR_RGB, USSR_COLOR),
  47. (FLAG, FLAG_USSR),
  48. (SIDE, SIDE_ALLIES),
  49. ])
  50. subjects.append(man1)
  51. subjects.append(man2)
  52. return simulation
  53. def test_state_loader_builder__ok__nominal_case(
  54. simulation,
  55. ):
  56. config = Config({
  57. 'global': {
  58. 'state_loader': 'tests.test_state.MyStateLoader',
  59. }
  60. })
  61. builder = StateConstructorBuilder(config, simulation)
  62. state_loader = builder.get_state_loader()
  63. assert type(state_loader) == MyStateLoader
  64. def test_state_loader__ok__load_state(
  65. state_loader,
  66. ):
  67. assert state_loader.get_state('tests/fixtures/state_ok.xml')
  68. def test_state_loader__error__state_empty(
  69. state_loader,
  70. ):
  71. with pytest.raises(StateLoadError):
  72. assert state_loader.get_state('tests/fixtures/state_empty.xml')
  73. def test_state_loader__error__state_invalid(
  74. state_loader,
  75. ):
  76. with pytest.raises(StateLoadError):
  77. assert state_loader.get_state(
  78. 'tests/fixtures/state_error_schema.xml',
  79. )
  80. def test_state__ok__subjects(
  81. state_loader,
  82. ):
  83. state = state_loader.get_state('tests/fixtures/state_ok.xml')
  84. assert 2 == len(state.subjects)
  85. assert isinstance(state.subjects[0], ManSubject)
  86. assert isinstance(state.subjects[1], ManSubject)
  87. assert (1, 1) == state.subjects[0].position
  88. assert (10, 10) == state.subjects[1].position
  89. assert 90.0 == state.subjects[0].direction
  90. assert 270.0 == state.subjects[1].direction
  91. assert 'COMBAT_MODE_DEFEND' == state.subjects[0].combat_mode
  92. assert 'COMBAT_MODE_HIDE' == state.subjects[1].combat_mode
  93. assert {
  94. 'SELECTION_COLOR_RGB': (204, 0, 0),
  95. 'FLAG': 'FLAG_USSR',
  96. 'SIDE': 'ALLIES',
  97. } == state.subjects[0].properties
  98. assert {
  99. 'SELECTION_COLOR_RGB': (0, 81, 211),
  100. 'FLAG': 'FLAG_DE',
  101. 'SIDE': 'AXIS',
  102. } == state.subjects[1].properties
  103. def test_state__ok__dump(
  104. config: Config,
  105. simulation_for_dump: TileStrategySimulation,
  106. ):
  107. state_dumper = StateDumper(config, simulation_for_dump)
  108. state_xml_str = state_dumper.get_state_dump()
  109. assert """<?xml version="1.0" ?>
  110. <state type="before_battle">
  111. <map>
  112. <name>tests/fixtures/map_a/map_a.tmx</name>
  113. </map>
  114. <subjects>
  115. <subject>
  116. <type>opencombat.simulation.subject.ManSubject</type>
  117. <position>10,11</position>
  118. <direction>42</direction>
  119. <combat_mode>COMBAT_MODE_DEFEND</combat_mode>
  120. <properties>
  121. <item>
  122. <key>SELECTION_COLOR_RGB</key>
  123. <value>0,81,211</value>
  124. </item>
  125. <item>
  126. <key>FLAG</key>
  127. <value>FLAG_DE</value>
  128. </item>
  129. <item>
  130. <key>SIDE</key>
  131. <value>SIDE_AXIS</value>
  132. </item>
  133. </properties>
  134. </subject>
  135. <subject>
  136. <type>opencombat.simulation.subject.ManSubject</type>
  137. <position>16,8</position>
  138. <direction>197</direction>
  139. <combat_mode>COMBAT_MODE_DEFEND</combat_mode>
  140. <properties>
  141. <item>
  142. <key>SELECTION_COLOR_RGB</key>
  143. <value>204,0,0</value>
  144. </item>
  145. <item>
  146. <key>FLAG</key>
  147. <value>FLAG_USSR</value>
  148. </item>
  149. <item>
  150. <key>SIDE</key>
  151. <value>SIDE_ALLIES</value>
  152. </item>
  153. </properties>
  154. </subject>
  155. </subjects>
  156. </state>""" == state_xml_str
  157. def test_state__ok__dump_and_load(
  158. config: Config,
  159. simulation_for_dump: TileStrategySimulation,
  160. state_loader,
  161. ):
  162. state_dumper = StateDumper(config, simulation_for_dump)
  163. state_xml_str = state_dumper.get_state_dump()
  164. tmp_file_name = '/tmp/{}.xml'.format(time.time())
  165. with open(tmp_file_name, 'w+') as file:
  166. file.write(state_xml_str)
  167. state_loader.get_state(tmp_file_name)