Mentions légales du service

Skip to content
Snippets Groups Projects
Commit f7b106c2 authored by Florent Bouchez Tichadou's avatar Florent Bouchez Tichadou
Browse files

Unit test where a map is loaded from a string and updated.

parent e3c7f687
No related branches found
No related tags found
1 merge request!73Unit test where a map is loaded from a string and updated.
Subproject commit 7af0044bc75803f1d95cd4f25501f032f39ff2d9
Subproject commit 7c0321d589a49db2e562641395b8ea409830345c
......@@ -224,7 +224,8 @@ class AbstractLevel(ABC, TrackerHelp):
try:
memory = self.tracker.get_program_memory(as_raw_python_objects=True)
if memory is None:
raise ValueError
lvl.error(f"Cannot update state of level, memory is None")
return
except ValueError as e:
lvl.error(f"Cannot update state of level: {e}")
return
......
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char str_map[] = "\
+------------------------+\n\
| |\n\
| |\n\
| > @ |\n\
| |\n\
| |\n\
| |\n\
+------------------------+\n\
";
int player_x;
int player_y;
char *the_map = NULL;
int width;
int height;
void load_map(const char *the_str_map)
{
int len = strlen(the_str_map);
free(the_map);
the_map = malloc(len * sizeof(char));
width =
strchr(str_map, '\n') - the_str_map + 1; // keeping the '\n' in the row
height = len / width;
assert(len % width == 0);
memcpy(the_map, the_str_map, len); // No '\0' at the end of this one
int row;
int col;
for (row = 0; row < height; row++) {
for (col = 0; col < width; col++) {
if (the_map[row * width + col] == '>') {
player_y = row;
player_x = col;
}
}
}
}
void move_forward(void)
{
the_map[player_y * width + player_x] = ' ';
player_x++;
the_map[player_y * width + player_x] = '>';
}
int main()
{
load_map(str_map);
move_forward();
exit(EXIT_SUCCESS);
}
......@@ -33,16 +33,23 @@ def assert_no_errors(caplog, levelnolimit):
assert False, f"Log level too high {record.levelname} {record.message}"
class LevelUnitTest(LevelTest):
def __init__(self, srcfile):
def __init__(self, srcfile, debug=False):
level_in_queue = queue.Queue()
level_out_queue = queue.Queue()
self.debug_mode = debug
exec_name, ext = splitext(srcfile)
assert ext == ".c"
self.temp_dir = tempfile.TemporaryDirectory(prefix="agdb_unittest_")
if debug: # not deleted on LevelUnitTest deletion
self.temp_dir_name = tempfile.mkdtemp(
prefix="agdb_unittest_"
)
else:
self.temp_dir = tempfile.TemporaryDirectory(
prefix="agdb_unittest_"
)
self.temp_dir_name = self.temp_dir.name
self.metadata = defaultdict(
lambda: None,
......@@ -54,7 +61,7 @@ class LevelUnitTest(LevelTest):
'wops': {},
# hacking the level_abc source_level_dir so it looks
# there for the executable
'source_level_dir': self.temp_dir.name,
'source_level_dir': self.temp_dir_name,
# reduced player mode, only variable player_x
'player_mode': 'x_only',
'player_y': 3,
......@@ -72,7 +79,18 @@ class LevelUnitTest(LevelTest):
)
def __del__(self):
self.temp_dir.cleanup()
if not self.debug_mode:
# TemporaryDirectory will be automatically deleted
# no need to to cleanup() manually
return
print(f"TemporaryDirectory kept: {self.temp_dir_name}")
logfile = join(self.temp_dir_name, "logs.txt")
print("Showing contents of logs.txt file")
with open(logfile, 'r') as file:
contents = file.read()
print("Contents of logfile:")
print(contents)
def compile(self):
self.level_path = self.metadata['level_path']
......@@ -111,6 +129,16 @@ class LevelUnitTest(LevelTest):
# tst.error("test error")
# tst.critical("FATAL ERROR")
def do_start(self):
"""
Redefine the do_start method to only do what we want in tests
"""
tst.debug("Performing do_start at the LevelUnitTest")
self.load_program()
self.tracker.send_direct_command("start")
self.inferior_started = True
self.start_number += 1
def test(self):
pass
......@@ -194,3 +222,46 @@ def test_missing_var(caplog):
logging.ERROR,
"Cannot find variable player_x in local or global variables",
)
def test_char_map(caplog):
caplog.set_level(logging.DEBUG)
level = LevelUnitTest("char_map.c") #, debug=True)
level.do_start()
level._gdb_next()
memory = level.tracker.get_program_memory(as_raw_python_objects=True)
r = level.tracker.get_variable_value_as_str('player_y', 'int')
c = level.tracker.get_variable_value_as_str('player_x', 'int')
w = level.tracker.get_variable_value_as_str('width', 'int')
assert r == 3
assert c == 6
assert w == 27
map = memory['global_variables']['the_map'].value
print("Current map:", map)
assert map[r*w + c] == '>', "starting player position"
level._gdb_next()
memory = level.tracker.get_program_memory(as_raw_python_objects=True)
new_r = level.tracker.get_variable_value_as_str('player_y', 'int')
new_c = level.tracker.get_variable_value_as_str('player_x', 'int')
assert new_r == 3
assert new_c == 7
new_map = memory['global_variables']['the_map'].value
assert len(map) == len(new_map), "map have same size"
differences = []
for i in range(len(new_map)):
if map[i] != new_map[i]:
differences.append((i, map[i], new_map[i]))
assert len(differences) == 2
assert differences[0] == (r*w+c, '>', ' ')
assert differences[1] == (new_r*w+new_c, ' ', '>')
assert_no_errors(caplog, logging.WARNING)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment