Пример #1
0
    def test_init(self):
        rule = Seq(Equal('X'), type=list)
        
        assert hasattr(rule, 'data_validator')
        assert isinstance(rule.data_validator, ValidatorBaseInterface)
        assert rule.data_validator([]) == None
        self.assertRaises(ValidationError, rule.data_validator, {})
        
        assert hasattr(rule, 'rules')
        assert isinstance(rule.rules, list)

        assert hasattr(rule, '__len__')
        assert len(rule) == 1
Пример #2
0
 def test_pattern_repeat(self):
     rule = Seq(
         Equal('A'),
         Any(Equal('A'), Equal('B')),
         Any(Equal('B'), Equal('C'))
     )
     r = rule.get(0)
     assert isinstance(r, Equal)
     assert r('A') == None
     self.assertRaises(ValidationError, r, 'B')
     self.assertRaises(ValidationError, r, 'C')
     
     r = rule.get(1)
     assert isinstance(r, Any)
     assert r('A') == None
     assert r('B') == None
     self.assertRaises(ValidationError, r, 'C')
     
     r = rule.get(2)
     assert isinstance(r, Any)
     assert r('B') == None
     assert r('C') == None
     self.assertRaises(ValidationError, r, 'A')
Пример #3
0
 def test_rule_accessor_set(self):
     rule = Seq(Equal('xxx'), Equal('Y'))
     
     rule[0] = String()
     assert rule[0] == String()
     assert rule[1] == Equal('Y')
     try:
         rule[2] = String()
     except IndexError:
         pass
     else:
         raise AssertionError()
     
     length = len(rule)
     rule.insert(Equal('aaa'))
     assert len(rule) == length + 1
     assert rule[len(rule)-1] == Equal('aaa')
     
     length = len(rule)
     rule.insert(Equal('bbb'), 0)
     assert len(rule) == length + 1
     assert rule[0] == Equal('bbb')
     rule.insert(String(), len(rule) + 100)
     assert rule[len(rule)-1] == String()