test_state.py 5.5KB

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