Ejemplo n.º 1
0
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"
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
 def test_add_valid_repeated(self):
     st = SymbolsTable()
     st.add("<eps>", 0)
     st.add("<eps>", 0)
     self.assertEqual(len(st), 1)
Ejemplo n.º 11
0
 def test_add(self):
     st = SymbolsTable()
     st.add("<eps>", 0)
     self.assertEqual(len(st), 1)
     st.add("a", 1)
     self.assertEqual(len(st), 2)
Ejemplo n.º 12
0
def test_add_valid_repeated():
    st = SymbolsTable()
    st.add("<eps>", 0)
    st.add("<eps>", 0)
    assert len(st) == 1
Ejemplo n.º 13
0
def test_add():
    st = SymbolsTable()
    st.add("<eps>", 0)
    assert len(st) == 1
    st.add("a", 1)
    assert len(st) == 2