test_state.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 {
  90. 'SELECTION_COLOR_RGB': (204, 0, 0),
  91. 'FLAG': 'FLAG_URSS',
  92. 'SIDE': 'ALLIES',
  93. } == state.subjects[0].properties
  94. assert {
  95. 'SELECTION_COLOR_RGB': (0, 81, 211),
  96. 'FLAG': 'FLAG_DE',
  97. 'SIDE': 'AXIS',
  98. } == state.subjects[1].properties
  99. def test_state__ok__dump(
  100. config: Config,
  101. simulation_for_dump: TileStrategySimulation,
  102. ):
  103. state_dumper = StateDumper(config, simulation_for_dump)
  104. state_xml_str = state_dumper.get_state_dump()
  105. assert """<?xml version="1.0" ?>
  106. <state type="before_battle">
  107. <map>
  108. <name>tests/fixtures/map_a/map_a.tmx</name>
  109. </map>
  110. <subjects>
  111. <subject>
  112. <type>opencombat.simulation.subject.ManSubject</type>
  113. <position>10,11</position>
  114. <direction>42</direction>
  115. <properties>
  116. <item>
  117. <key>SELECTION_COLOR_RGB</key>
  118. <value>0,81,211</value>
  119. </item>
  120. <item>
  121. <key>FLAG</key>
  122. <value>FLAG_DE</value>
  123. </item>
  124. <item>
  125. <key>SIDE</key>
  126. <value>AXIS</value>
  127. </item>
  128. </properties>
  129. </subject>
  130. <subject>
  131. <type>opencombat.simulation.subject.ManSubject</type>
  132. <position>16,8</position>
  133. <direction>197</direction>
  134. <properties>
  135. <item>
  136. <key>SELECTION_COLOR_RGB</key>
  137. <value>204,0,0</value>
  138. </item>
  139. <item>
  140. <key>FLAG</key>
  141. <value>FLAG_URSS</value>
  142. </item>
  143. <item>
  144. <key>SIDE</key>
  145. <value>ALLIES</value>
  146. </item>
  147. </properties>
  148. </subject>
  149. </subjects>
  150. </state>""" == state_xml_str
  151. def test_state__ok__dump_and_load(
  152. config: Config,
  153. simulation_for_dump: TileStrategySimulation,
  154. state_loader,
  155. ):
  156. state_dumper = StateDumper(config, simulation_for_dump)
  157. state_xml_str = state_dumper.get_state_dump()
  158. tmp_file_name = '/tmp/{}.xml'.format(time.time())
  159. with open(tmp_file_name, 'w+') as file:
  160. file.write(state_xml_str)
  161. state_loader.get_state(tmp_file_name)