test_state.py 4.8KB

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