1 """Unit tests for jazzparser.grammar module
2
3 The most important thing here is to make sure that the default grammar
4 can be loaded and gets the right set of attributes that we'll need
5 to use elsewhere.
6
7 It's quite difficult to test some of the functionality thoroughly, so
8 I'm not going into a lot of depth on testing other things.
9
10 """
11 """
12 ============================== License ========================================
13 Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding
14
15 This file is part of The Jazz Parser.
16
17 The Jazz Parser is free software: you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation, either version 3 of the License, or
20 (at your option) any later version.
21
22 The Jazz Parser is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with The Jazz Parser. If not, see <http://www.gnu.org/licenses/>.
29
30 ============================ End license ======================================
31
32 """
33 __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"
34
35 import unittest, warnings
36 from jazzparser.grammar import Grammar, get_grammar, MorphItem
37 from jptests import prepare_db_input
38
40 SET_ATTRIBUTES = [
41 'formalism',
42 'grammar_file',
43 'families',
44 'inactive_families',
45 'morphs',
46 'morph_items',
47 'modality_tree',
48 'rules',
49 'unary_rules',
50 'binary_rules',
51 'rules_by_name',
52 'lexical_rules',
53 'pos_tags',
54 ]
55
57 """
58 Load some chord sequences as db mirrors that we can use as
59 example input.
60
61 There's quite a bit in here that could fail, but we can't get
62 round that. We need some data to test with.
63
64 """
65 inputs = prepare_db_input()
66 self.dbinput = inputs[2]
67
69 """
70 Just loads the default grammar to see if there are any errors
71 raised in the process.
72
73 """
74
75 Grammar()
76
78 """
79 Loads a grammar using L{jazzparser.grammar.get_grammar} and then checks
80 that if we load another we get the same instance.
81
82 """
83 g1 = get_grammar()
84 g2 = get_grammar()
85 self.assertIs(g1, g2)
86
88 """
89 Checks that the public attributes of Grammar get set when the
90 default grammar is loaded.
91
92 """
93 g = Grammar()
94 for attr in self.SET_ATTRIBUTES:
95 self.assertIsNotNone(getattr(g, attr))
96
98 """
99 Tries getting a sign from the grammar from an example of
100 chord input.
101
102 @see: L{jazzparser.grammar.Grammar.get_signs_for_word}
103
104 """
105 g = Grammar()
106
107 for word in self.dbinput.chords[:10]:
108
109 signs = g.get_signs_for_word(word)
110
125
127 """
128 Try getting a function for every tag and check it's in the
129 set of allowed functions.
130
131 """
132 g = Grammar()
133 for tag in g.pos_tags:
134 fun = g.tag_to_function(tag)
135 if fun is None:
136 warnings.warn("Tag %s has no function given by the "\
137 "grammar" % tag)
138 else:
139 self.assertIn(fun, ['T','D','S','Pass'])
140
142 """
143 Try reading something out of the equivalence map and check the map
144 works as expected.
145
146 We expect the default grammar to have an equivalence map, so test
147 on this basis. It could happen in the future that it has no equivalence
148 map, which is perfectly legal. In this case, this test will need to
149 be updated so that it gets a grammar with a map, or just removed.
150
151 """
152 g = Grammar()
153 if len(g.equiv_map) == 0:
154 raise ValueError, "cannot test equivalence map because it's empty "\
155 "in the default grammar"
156
157 key = g.equiv_map.keys()[0]
158
159 equiv = g.equiv_map[key]
160 self.assertIsInstance(equiv.root, int)
161 self.assertIsInstance(equiv.target, MorphItem)
162