def test_save(tmpdir): st = SymbolsTable() st.add("a", 1) st.add("b", 2) st_file = tmpdir / "syms" st.save(str(st_file)) assert st_file.read_text("utf-8") == "a 1\nb 2\n"
def test_contains(): st = SymbolsTable() st.add("a", 1) assert "a" in st assert 1 in st assert 2 not in st with pytest.raises(ValueError, match="SymbolsTable contains pairs"): assert None in st # noqa: expected type
def test_iterator(self): st = SymbolsTable() st.add("a", 1) st.add("b", 2) it = iter(st) self.assertEqual(next(it), ("a", 1)) self.assertEqual(next(it), ("b", 2)) with self.assertRaises(StopIteration): next(it)
def test_add_repeated_value(self): st = SymbolsTable() st.add("a", 0) with self.assertRaises( KeyError, msg= 'Value "0" was already present in the table (assigned to symbol "a")', ): st.add("b", 0)
def test_iterator(): st = SymbolsTable() st.add("a", 1) st.add("b", 2) it = iter(st) assert next(it) == ("a", 1) assert next(it) == ("b", 2) with pytest.raises(StopIteration): next(it)
def test_save(self): st = SymbolsTable() st.add("a", 1) st.add("b", 2) table_file = NamedTemporaryFile(delete=False) st.save(table_file) with open(table_file.name, "r") as f: table_content = f.read() self.assertEqual(table_content, "a 1\nb 2\n") os.remove(table_file.name)
def test_getitem(self): st = SymbolsTable() st.add("a", 1) st.add("b", 2) self.assertEqual(st["a"], 1) self.assertEqual(st["b"], 2) self.assertEqual(st[1], "a") self.assertEqual(st[2], "b") self.assertEqual(st[-9], None) self.assertEqual(st["c"], None)
def test_getitem(): st = SymbolsTable() st.add("a", 1) st.add("b", 2) assert st["a"] == 1 assert st["b"] == 2 assert st[1] == "a" assert st[2] == "b" assert st[-9] is None assert st["c"] is None
def test_add_repeated_value(): st = SymbolsTable() st.add("a", 0) with pytest.raises( KeyError, match=re.escape( 'Value "0" was already present in the table (assigned to symbol "a")' ), ): st.add("b", 0)
def test_add_valid_repeated(self): st = SymbolsTable() st.add("<eps>", 0) st.add("<eps>", 0) self.assertEqual(len(st), 1)
def test_add(self): st = SymbolsTable() st.add("<eps>", 0) self.assertEqual(len(st), 1) st.add("a", 1) self.assertEqual(len(st), 2)
def test_add_valid_repeated(): st = SymbolsTable() st.add("<eps>", 0) st.add("<eps>", 0) assert len(st) == 1
def test_add(): st = SymbolsTable() st.add("<eps>", 0) assert len(st) == 1 st.add("a", 1) assert len(st) == 2