Skip to content
Snippets Groups Projects
Commit d11a719f authored by Eva Bardou's avatar Eva Bardou :frog:
Browse files

Improve symbols table loading to preserve tabulations

parent 84285241
No related branches found
No related tags found
1 merge request!101Improve symbols table loading to preserve tabulations
Pipeline #195246 passed
......@@ -29,8 +29,13 @@ class SymbolsTable:
f = open(f)
self.clear()
try:
lines = [line.split() for line in f if len(line.split())]
for s, v in lines:
lines = f.read().splitlines()
mapping = [
list(filter(bool, line.split(" ")))
for line in lines
if len(list(filter(bool, line.split(" "))))
]
for s, v in mapping:
self.add(s, int(v))
except Exception:
raise
......
......@@ -86,19 +86,32 @@ def test_contains():
@pytest.mark.parametrize("as_type", [str, Path])
def test_load(tmpdir, as_type):
file = tmpdir / "f"
file.write_text("\n\na 1\nb 2\n", "utf-8")
file.write_text("\n\na 1\nb 2\n\t 3\n\n", "utf-8")
st = SymbolsTable(as_type(file))
assert len(st) == 2
assert len(st) == 3
assert st["a"] == 1
assert st["b"] == 2
assert st["\t"] == 3
assert st[1] == "a"
assert st[2] == "b"
assert st[3] == "\t"
def test_load_type_error(tmpdir):
file = tmpdir / "f"
file.write_text("\n 1", "utf-8")
with pytest.raises(
ValueError, match=re.escape("not enough values to unpack (expected 2, got 1)")
):
SymbolsTable(file)
def test_load_value_error(tmpdir):
file = tmpdir / "f"
file.write_text("\n\na 1\nb c\n", "utf-8")
with pytest.raises(ValueError):
file.write_text("a X", "utf-8")
with pytest.raises(
ValueError, match=re.escape("invalid literal for int() with base 10: 'X'")
):
SymbolsTable(file)
......
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