示例#1
0
    def grammar_elem_init(self, session_data):
        scope = model.Rule.SCOPE_PRIVATE

        if self[0] is not None:
            scope = model.Rule.SCOPE_PUBLIC

        self.model = model.Rule(name=self[2].value, value=self[5].model, scope=scope)
    def test_add_rule_should_succeed(self):
        rule_b = model.Rule(name="b")

        self.assertEqual(1, len(self.gram.rules))

        self.gram.add_rule(rule_b)

        self.assertEqual(2, len(self.gram.rules))
    def test_create_grammar_element(self):
        token1 = model.Token(value='token content 1')
        rule1 = model.Rule(name='rule name 1', value=token1)
        token2 = model.Token(value='token content 2')
        rule2 = model.Rule(name='rule name 2', value=token2)
        grammar = model.Grammar(name='grammar', language='en-US')
        grammar.root_rule = rule1
        grammar.add_rule(rule1)
        grammar.add_rule(rule2)

        grammar_element = self.serializer.create_grammar_element(grammar)

        self.assertEqual(2, len(grammar_element))
        self.assertEqual('http://www.w3.org/2001/06/grammar',
                         grammar_element.get('xmlns'))
        self.assertEqual('1.0', grammar_element.get('version'))
        self.assertEqual('en-US', grammar_element.get('xml:lang'))
        self.assertEqual('rule name 1', grammar_element.get('root'))
    def test_create_rule_element(self):
        token = model.Token(value='token content')
        rule = model.Rule(name='rule name')
        rule.value = token
        rule_element = self.serializer.create_rule_element(rule)

        self.assertEqual('rule', rule_element.tag)
        self.assertEqual('rule name', rule_element.get('id'))
        self.assertEqual('token content', rule_element.text)
        self.assertIsNone(rule_element.get('scope'))
 def setUp(self):
     self.gram = model.Grammar()
     self.rule_a = model.Rule(name="id")
     self.gram.add_rule(self.rule_a)
    def test_add_rule_with_existing_name_should_raise_value_error(self):
        rule_b = model.Rule(name="id")

        with self.assertRaises(ValueError):
            self.gram.add_rule(rule_b)