Skip to content

Commit cad1299

Browse files
committed
test bot.paint_background
1 parent 42f143f commit cad1299

1 file changed

Lines changed: 147 additions & 1 deletion

File tree

test/test_team.py

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pelita.gamestate_filters import manhattan_dist
77
from pelita.layout import initial_positions, parse_layout
88
from pelita.maze_generator import generate_maze
9-
9+
from pelita.exceptions import PelitaBotError
1010

1111
def stopping(bot, state):
1212
return bot.position
@@ -767,3 +767,149 @@ def speaking_team(bot, state):
767767
# check that we finished without problem
768768
assert state['round'] == 1
769769
assert state['turn'] == 3
770+
771+
772+
def test_valid_paint_background():
773+
test_layout = """
774+
##################
775+
#.#... .##. y#
776+
# # # . .### #x#
777+
# ####. . #
778+
# . .#### #
779+
#a# ###. . # # #
780+
#b .##. ...#.#
781+
##################
782+
"""
783+
overlay = [
784+
((0,0) , "#AABBCC"),
785+
((1,1) , "#BBCCDD"),
786+
((100, 200) , "#DDCCAA"), # silently ignore coords out of the maze
787+
]
788+
789+
parsed = parse_layout(test_layout)
790+
791+
def overlay_team(bot, state):
792+
for pos, color in overlay:
793+
bot.paint_background(pos, color=color)
794+
return bot.position
795+
796+
state = setup_game([overlay_team, overlay_team], max_rounds=2, layout_dict=parsed, raise_bot_exceptions=True)
797+
798+
overlay_in_maze = [{'pos':(0,0), 'color':"#AABBCC"}, {'pos':(1,1), 'color':"#BBCCDD"}]
799+
for idx in range(4):
800+
state = play_turn(state)
801+
gs_overlay = state['overlays'][idx]
802+
assert gs_overlay == overlay_in_maze
803+
804+
805+
@pytest.mark.parametrize('pos, color, match', [
806+
((0,1,2), '#AABBCC', 'Position.+(0, 1, 2).+not a valid'), # position has too many values
807+
('#AABBCC', (0, 1), 'Position.+#AABBCC.+not a valid'), # swapped arguments
808+
((1, 2), 'white', 'Background color "white".+'), # color is not HTML encoded
809+
((1, 2),'#AABBCCDDEE', 'Background color.+AABBCCDDEE.+'), # color uses alpha channel
810+
((1, 2),'#GGGGGG', 'Background color.+GGGGGG.+'), # color out of range
811+
])
812+
def test_paint_background_exceptions(pos, color, match):
813+
test_layout = """
814+
##################
815+
#.#... .##. y#
816+
# # # . .### #x#
817+
# ####. . #
818+
# . .#### #
819+
#a# ###. . # # #
820+
#b .##. ...#.#
821+
##################
822+
"""
823+
parsed = parse_layout(test_layout)
824+
825+
def overlay_team(bot, state):
826+
bot.paint_background(pos, color)
827+
return bot.position
828+
829+
state = setup_game([overlay_team, overlay_team], max_rounds=2, layout_dict=parsed, raise_bot_exceptions=True)
830+
with pytest.raises(PelitaBotError, match=match):
831+
state = play_turn(state, raise_bot_exceptions=True)
832+
833+
834+
def test_paint_background_doesnt_overwrite():
835+
# check that we do override color for a coordinate if already set and
836+
# that we don't touch other properties that may be defined on that coordinate
837+
test_layout = """
838+
##################
839+
#.#... .##. y#
840+
# # # . .### #x#
841+
# ####. . #
842+
# . .#### #
843+
#a# ###. . # # #
844+
#b .##. ...#.#
845+
##################
846+
"""
847+
parsed = parse_layout(test_layout)
848+
849+
pos = (0, 1)
850+
color = '#FFFFFF'
851+
def overlay_team(bot, state):
852+
bot.paint_background(pos, '#AABBCC')
853+
bot._overlay[pos].update({'text' : 'something'})
854+
bot.paint_background(pos, color)
855+
return bot.position
856+
857+
state = setup_game([overlay_team, overlay_team], max_rounds=2, layout_dict=parsed, raise_bot_exceptions=True)
858+
859+
for idx in range(4):
860+
state = play_turn(state)
861+
gs_overlay = state['overlays'][idx]
862+
assert gs_overlay == [{'pos':(0,1), 'color':'#FFFFFF', 'text':'something'}]
863+
864+
865+
def test_paint_background_different_bots():
866+
test_layout = """
867+
##################
868+
#.#... .##. y#
869+
# # # . .### #x#
870+
# ####. . #
871+
# . .#### #
872+
#a# ###. . # # #
873+
#b .##. ...#.#
874+
##################
875+
"""
876+
parsed = parse_layout(test_layout)
877+
878+
# blue team
879+
pos0 = (0, 0)
880+
color0 = '#FFFFF0'
881+
pos2 = (0, 2)
882+
color2 = '#FFFFF2'
883+
def overlay_team_blue(bot, state):
884+
if bot.turn == 0:
885+
bot.paint_background(pos0, color0)
886+
else:
887+
bot.paint_background(pos2, color2)
888+
return bot.position
889+
890+
# red team
891+
pos1 = (0, 1)
892+
color1 = '#FFFFF1'
893+
pos3 = (0, 3)
894+
color3 = '#FFFFF3'
895+
def overlay_team_red(bot, state):
896+
if bot.turn == 0:
897+
bot.paint_background(pos1, color1)
898+
else:
899+
bot.paint_background(pos3, color3)
900+
return bot.position
901+
902+
state = setup_game([overlay_team_blue, overlay_team_red], max_rounds=2, layout_dict=parsed, raise_bot_exceptions=True)
903+
904+
pos_color = [(pos0, color0), (pos1, color1), (pos2, color2), (pos3, color3)]
905+
for idx in range(4):
906+
pos, color = pos_color[idx]
907+
state = play_turn(state)
908+
gs_overlay = state['overlays'][idx]
909+
rest_left = state['overlays'][:idx]
910+
rest_right = state['overlays'][idx+1:]
911+
assert gs_overlay == [{'pos' : pos, 'color': color}]
912+
# verify that the overlays get overriden at every turn
913+
for overlay in rest_left+rest_right:
914+
assert overlay == []
915+

0 commit comments

Comments
 (0)