test_state.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # coding: utf-8
  2. import pytest
  3. from synergine2.config import Config
  4. from synergine2_cocos2d.const import SELECTION_COLOR_RGB
  5. from opencombat.exception import StateLoadError
  6. from opencombat.simulation.base import TileStrategySimulation
  7. from opencombat.simulation.base import TileStrategySubjects
  8. from opencombat.simulation.subject import ManSubject
  9. from opencombat.state import StateLoaderBuilder, StateDumper
  10. from opencombat.state import StateLoader
  11. from opencombat.const import FLAG
  12. from opencombat.const import SIDE
  13. from opencombat.const import FLAG_DE
  14. from opencombat.const import DE_COLOR
  15. from opencombat.const import URSS_COLOR
  16. from opencombat.const import FLAG_URSS
  17. class MyStateLoader(StateLoader):
  18. pass
  19. @pytest.fixture
  20. def state_loader(config, simulation):
  21. return StateLoader(config, simulation)
  22. @pytest.fixture
  23. def simulation_for_dump(config) -> TileStrategySimulation:
  24. simulation = TileStrategySimulation(
  25. config,
  26. 'tests/fixtures/map_a/map_a.tmx',
  27. )
  28. subjects = TileStrategySubjects(simulation=simulation)
  29. man1 = ManSubject(config, simulation)
  30. man1.position = (10, 11)
  31. man1.direction = 42
  32. man1.properties = {
  33. SELECTION_COLOR_RGB: DE_COLOR,
  34. FLAG: FLAG_DE,
  35. SIDE: 'AXIS',
  36. }
  37. man2 = ManSubject(config, simulation)
  38. man2.position = (16, 8)
  39. man2.direction = 197
  40. man2.properties = {
  41. SELECTION_COLOR_RGB: URSS_COLOR,
  42. FLAG: FLAG_URSS,
  43. SIDE: 'ALLIES',
  44. }
  45. subjects.append(man1)
  46. subjects.append(man2)
  47. return simulation
  48. def test_state_loader_builder__ok__nominal_case(
  49. simulation,
  50. ):
  51. config = Config({
  52. 'global': {
  53. 'state_loader': 'tests.test_state.MyStateLoader',
  54. }
  55. })
  56. builder = StateLoaderBuilder(config, simulation)
  57. state_loader = builder.get_state_loader()
  58. assert type(state_loader) == MyStateLoader
  59. def test_state_loader__ok__load_state(
  60. state_loader,
  61. ):
  62. assert state_loader.get_state('tests/fixtures/state_ok.xml')
  63. def test_state_loader__error__state_empty(
  64. state_loader,
  65. ):
  66. with pytest.raises(StateLoadError):
  67. assert state_loader.get_state('tests/fixtures/state_empty.xml')
  68. def test_state_loader__error__state_invalid(
  69. state_loader,
  70. ):
  71. with pytest.raises(StateLoadError):
  72. assert state_loader.get_state(
  73. 'tests/fixtures/state_error_schema.xml',
  74. )
  75. def test_state__ok__subjects(
  76. state_loader,
  77. ):
  78. state = state_loader.get_state('tests/fixtures/state_ok.xml')
  79. assert 2 == len(state.subjects)
  80. assert isinstance(state.subjects[0], ManSubject)
  81. assert isinstance(state.subjects[1], ManSubject)
  82. assert (1, 1) == state.subjects[0].position
  83. assert (10, 10) == state.subjects[1].position
  84. assert 90.0 == state.subjects[0].direction
  85. assert 270.0 == state.subjects[1].direction
  86. def test_state__ok__dump(
  87. config: Config,
  88. simulation_for_dump: TileStrategySimulation,
  89. ):
  90. state_dumper = StateDumper(config, simulation_for_dump)
  91. state_xml = state_dumper.get_state_dump()
  92. assert False