Exemplo n.º 1
0
 def setUp(self):
     self.start = "NP"
     self.productions = [
         Production("NP", ["N"]),
         Production("NP", ["D", "N"])
     ]
     self.grammar = Grammar(self.start, self.productions)
Exemplo n.º 2
0
 def test_parse(self):
     """ Should work properly on valid data """
     res = [
         Production("Adj", ["'fall'"]),
         Production("Adj", ["'spring'"]),
         Production("Adj", ["'purple'"]),
         Production("Adj", ["'left'"])
     ]
     self.assertEqual(res, Production.parse_production(self.line))
Exemplo n.º 3
0
 def setUp(self):
     self.productions = [
         Production("NP", ["N"]),
         Production("NP", ["D", "N"]),
         Production("N", ["'fall'"]),
         Production("N", ["'spring'"]),
         Production("N", ["'leaves'"]),
         Production("N", ["'dog'"]),
         Production("N", ["'cat'"]),
         Production("D", ["'the'"])
     ]
     self.grammar = Grammar("NP", self.productions)
Exemplo n.º 4
0
class TestProduction(unittest.TestCase):
    def setUp(self):
        # TODO: create an array of productions for testing
        self.lhs = "S"
        self.rhs = ["NP", "VP"]
        self.production = Production(self.lhs, self.rhs)

    def test_lhs(self):
        res = self.lhs
        self.assertEqual(res, self.production.lhs())

    def test_rhs(self):
        res = ("NP", "VP")
        self.assertEqual(res, self.production.rhs())

    def test_rhs_single_element(self):
        """ Assert it still returns a tuple """
        lhs = "S"
        rhs = ["NP"]
        production = Production(lhs, rhs)
        self.assertIsInstance(production.rhs(), tuple)

    def test_str(self):
        # TODO: handle terminals
        res = "S -> NP VP"
        self.assertEqual(res, str(self.production))

    def test_eq(self):
        """ Test equality """
        other = copy.copy(self.production)
        self.assertEqual(self.production, other)

    def test_ne(self):
        """ Test inequality """
        other = Production("NP", ["N"])
        self.assertNotEqual(self, other)
Exemplo n.º 5
0
 def test_parse_grammar(self):
     # TODO: define __eq__ for Grammar object so we can test equality
     """ Test that the string representation of a grammar obtained using
     parse_grammar is equal to one created with constructor.
     """
     start = "NP"
     productions = [
         Production("NP", ["N"]),
         Production("NP", ["D", "N"]),
         Production("N", ["'fall'"]),
         Production("N", ["'spring'"]),
         Production("N", ["'leaves'"]),
         Production("N", ["'dog'"]),
         Production("N", ["'cat'"]),
         Production("D", ["'the'"])
     ]
     res = Grammar(start, productions)
     self.assertEqual(str(res),
                      str(Grammar.parse_grammar(self.grammar_as_string)))
Exemplo n.º 6
0
 def test_calculate_lhs_indexes(self):
     # TODO: We could remove this
     """ We'll just check that this ``grammar``._lhs_index is properly set
     after init
     """
     res = {}
     res["NP"] = [Production("NP", ["N"]), Production("NP", ["D", "N"])]
     res["N"] = [
         Production("N", ["'fall'"]),
         Production("N", ["'spring'"]),
         Production("N", ["'leaves'"])
     ]
     res["D"] = [Production("D", ["'the'"])]
     self.assertEqual(res, self.grammar._calculate_lhs_index())
Exemplo n.º 7
0
 def test_tree_from_production_with_terminal(self):
     self.production = Production("N", ["'fall'"])
     res = Tree("N", ["'fall'"])
     self.assertEqual(res, tree_from_production(self.production))
Exemplo n.º 8
0
 def setUp(self):
     self.production = Production("S", ["NP", "VP"])
Exemplo n.º 9
0
 def test_return_array(self):
     """ Make sure it returns an array """
     self.assertIsInstance(Production.parse_production(self.line), list)
Exemplo n.º 10
0
 def test_ne(self):
     """ Test inequality """
     other = Production("NP", ["N"])
     self.assertNotEqual(self, other)
Exemplo n.º 11
0
 def test_rhs_single_element(self):
     """ Assert it still returns a tuple """
     lhs = "S"
     rhs = ["NP"]
     production = Production(lhs, rhs)
     self.assertIsInstance(production.rhs(), tuple)
Exemplo n.º 12
0
 def setUp(self):
     # TODO: create an array of productions for testing
     self.lhs = "S"
     self.rhs = ["NP", "VP"]
     self.production = Production(self.lhs, self.rhs)