def test__create_new_class(self): dtd_dict = { 'tag': {'elts': 'tag1', 'attrs': [('idtag', 'ID', '#IMPLIED')]}, } class_dic = dtd_parser._create_class_dict(dtd_dict) cls = dtd_parser._create_new_class( class_dic, 'tag', required=False, islist=False, conditionals=[]) self.assertTrue(issubclass(cls, Element)) self.assertEqual(cls.__name__, 'tag') self.assertEqual(cls._required, False) cls = dtd_parser._create_new_class( class_dic, 'tag', required=True, islist=False, conditionals=[]) self.assertTrue(issubclass(cls, Element)) self.assertEqual(cls.__name__, 'tag') self.assertEqual(cls._required, True) cls = dtd_parser._create_new_class( class_dic, 'tag', required=True, islist=True, conditionals=[]) self.assertTrue(issubclass(cls, ListElement)) self.assertEqual(cls.__name__, 'tagList') self.assertEqual(cls._required, True) self.assertEqual(cls._children_class._parent_cls, cls) self.assertEqual(cls._children_class._required, True) self.assertTrue(issubclass(cls._children_class, InListMixin)) dtd_dict = { 'tag': {'elts': '(tag1|tag2)', 'attrs': []}, 'tag1': {'elts': 'sub1', 'attrs': []}, 'tag2': {'elts': 'sub2', 'attrs': []}, } conditionals = [ ('tag1', True, False, []), ('tag2', True, False, []), ] class_dic = dtd_parser._create_class_dict(dtd_dict) try: cls = dtd_parser._create_new_class( class_dic, 'tag1_tag2', required=True, islist=False, conditionals=[]) assert(False) except Exception, e: self.assertEqual( str(e), 'You should provide a base_cls or conditionals for tag1_tag2')
def test__create_new_class(self): dtd_dict = { 'tag': { 'elts': 'tag1', 'attrs': [('idtag', 'ID', '#IMPLIED')] }, } class_dic = dtd_parser._create_class_dict(dtd_dict) cls = dtd_parser._create_new_class(class_dic, 'tag', required=False, islist=False, conditionals=[]) self.assertTrue(issubclass(cls, Element)) self.assertEqual(cls.__name__, 'tag') self.assertEqual(cls._required, False) cls = dtd_parser._create_new_class(class_dic, 'tag', required=True, islist=False, conditionals=[]) self.assertTrue(issubclass(cls, Element)) self.assertEqual(cls.__name__, 'tag') self.assertEqual(cls._required, True) cls = dtd_parser._create_new_class(class_dic, 'tag', required=True, islist=True, conditionals=[]) self.assertTrue(issubclass(cls, ListElement)) self.assertEqual(cls.__name__, 'tagList') self.assertEqual(cls._required, True) self.assertEqual(cls._children_class._parent_cls, cls) self.assertEqual(cls._children_class._required, True) self.assertTrue(issubclass(cls._children_class, InListMixin)) dtd_dict = { 'tag': { 'elts': '(tag1|tag2)', 'attrs': [] }, 'tag1': { 'elts': 'sub1', 'attrs': [] }, 'tag2': { 'elts': 'sub2', 'attrs': [] }, } conditionals = [ ('tag1', True, False, []), ('tag2', True, False, []), ] class_dic = dtd_parser._create_class_dict(dtd_dict) try: cls = dtd_parser._create_new_class(class_dic, 'tag1_tag2', required=True, islist=False, conditionals=[]) assert (False) except Exception as e: self.assertEqual( str(e), 'You should provide a base_cls or conditionals for tag1_tag2') cls = dtd_parser._create_new_class(class_dic, 'tag1_tag2', required=True, islist=False, conditionals=conditionals) self.assertTrue(issubclass(cls, ChoiceElement)) self.assertEqual(cls.__name__, 'tag1_tag2Choice') self.assertEqual(cls._required, True) self.assertEqual(len(cls._choice_classes), 2) elt1 = cls._choice_classes[0] elt2 = cls._choice_classes[1] self.assertEqual(elt1._parent_cls, cls) self.assertTrue(issubclass(elt1, InChoiceMixin)) self.assertEqual(elt1._required, True) self.assertEqual(elt2._parent_cls, cls) self.assertTrue(issubclass(elt2, InChoiceMixin)) self.assertEqual(elt2._required, True) cls = dtd_parser._create_new_class(class_dic, 'tag1_tag2', required=True, islist=True, conditionals=conditionals) self.assertTrue(issubclass(cls, ChoiceListElement)) self.assertEqual(cls.__name__, 'tag1_tag2ChoiceList') self.assertEqual(cls._required, True) self.assertEqual(len(cls._choice_classes), 2) elt1 = cls._choice_classes[0] elt2 = cls._choice_classes[1] self.assertEqual(elt1._parent_cls, cls) self.assertEqual(elt1._required, True) self.assertTrue(issubclass(elt1, InListMixin)) self.assertEqual(elt2._parent_cls, cls) self.assertEqual(elt2._required, True) self.assertTrue(issubclass(elt2, InListMixin))
def test__create_class_dict(self): dtd_dict = { 'tag': { 'elts': '#PCDATA', 'attrs': [('idtag', 'ID', '#IMPLIED')] }, } dic = dtd_parser._create_class_dict(dtd_dict) self.assertEqual(len(dic), 1) tag = dic['tag'] self.assertTrue(issubclass(tag, TextElement)) self.assertEqual(tag.tagname, 'tag') self.assertEqual(tag._is_empty, False) self.assertEqual(tag._attribute_names, ['idtag']) self.assertEqual(tag.children_classes, []) dtd_dict = { 'tag': { 'elts': '(#PCDATA|tag1|tag2)*', 'attrs': [] }, } dic = dtd_parser._create_class_dict(dtd_dict) self.assertEqual(len(dic), 1) tag = dic['tag'] self.assertTrue(issubclass(tag, TextElement)) self.assertEqual(tag.tagname, 'tag') self.assertEqual(tag._is_empty, False) self.assertEqual(tag._attribute_names, []) self.assertEqual(tag.children_classes, []) # dtd_dict has changed because of the mixed content self.assertEqual(dtd_dict, {'tag': { 'elts': 'tag1?,tag2?', 'attrs': [] }}) dtd_dict = { 'tag': { 'elts': '(tag1)*', 'attrs': [] }, } dic = dtd_parser._create_class_dict(dtd_dict) self.assertEqual(len(dic), 1) tag = dic['tag'] self.assertTrue(issubclass(tag, Element)) self.assertEqual(tag.tagname, 'tag') self.assertEqual(tag._is_empty, False) self.assertEqual(tag._attribute_names, []) self.assertEqual(tag.children_classes, []) dtd_dict = { 'tag': { 'elts': '(tag1|tag2)', 'attrs': [] }, } dic = dtd_parser._create_class_dict(dtd_dict) self.assertEqual(len(dic), 1) tag = dic['tag'] self.assertTrue(issubclass(tag, Element)) self.assertEqual(tag.tagname, 'tag') self.assertEqual(tag._is_empty, False) self.assertEqual(tag._attribute_names, []) self.assertEqual(tag.children_classes, []) dtd_dict = { 'tag': { 'elts': 'EMPTY', 'attrs': [] }, } dic = dtd_parser._create_class_dict(dtd_dict) self.assertEqual(len(dic), 1) tag = dic['tag'] self.assertTrue(issubclass(tag, TextElement)) self.assertEqual(tag.tagname, 'tag') self.assertEqual(tag._is_empty, True) self.assertEqual(tag.children_classes, [])
def test__create_new_class(self): dtd_dict = { 'tag': { 'elts': 'tag1', 'attrs': [('idtag', 'ID', '#IMPLIED')] }, } class_dic = dtd_parser._create_class_dict(dtd_dict) cls = dtd_parser._create_new_class(class_dic, 'tag', required=False, islist=False, conditionals=[]) self.assertTrue(issubclass(cls, Element)) self.assertEqual(cls.__name__, 'tag') self.assertEqual(cls._required, False) cls = dtd_parser._create_new_class(class_dic, 'tag', required=True, islist=False, conditionals=[]) self.assertTrue(issubclass(cls, Element)) self.assertEqual(cls.__name__, 'tag') self.assertEqual(cls._required, True) cls = dtd_parser._create_new_class(class_dic, 'tag', required=True, islist=True, conditionals=[]) self.assertTrue(issubclass(cls, ListElement)) self.assertEqual(cls.__name__, 'tagList') self.assertEqual(cls._required, True) self.assertEqual(len(cls._elts), 1) self.assertEqual(cls._elts[0]._parent, cls) self.assertEqual(cls._elts[0]._required, True) dtd_dict = { 'tag': { 'elts': '(tag1|tag2)', 'attrs': [] }, 'tag1': { 'elts': 'sub1', 'attrs': [] }, 'tag2': { 'elts': 'sub2', 'attrs': [] }, } conditionals = [ ('tag1', True, False, []), ('tag2', True, False, []), ] class_dic = dtd_parser._create_class_dict(dtd_dict) try: cls = dtd_parser._create_new_class(class_dic, 'tag1_tag2', required=True, islist=False, conditionals=[]) assert 0 except Exception, e: self.assertEqual( str(e), 'You should provide a base_cls or conditionals for tag1_tag2')
def test__create_class_dict(self): dtd_dict = { 'tag': {'elts': '#PCDATA', 'attrs': [('idtag', 'ID', '#IMPLIED')]}, } dic = dtd_parser._create_class_dict(dtd_dict) self.assertEqual(len(dic), 1) tag = dic['tag'] self.assertTrue(issubclass(tag, TextElement)) self.assertEqual(tag.tagname, 'tag') self.assertEqual(tag._is_empty, False) self.assertEqual(tag._attribute_names, ['idtag']) self.assertEqual(tag.children_classes, []) dtd_dict = { 'tag': {'elts': '(#PCDATA|tag1|tag2)*', 'attrs': []}, } dic = dtd_parser._create_class_dict(dtd_dict) self.assertEqual(len(dic), 1) tag = dic['tag'] self.assertTrue(issubclass(tag, TextElement)) self.assertEqual(tag.tagname, 'tag') self.assertEqual(tag._is_empty, False) self.assertEqual(tag._attribute_names, []) self.assertEqual(tag.children_classes, []) # dtd_dict has changed because of the mixed content self.assertEqual( dtd_dict, {'tag': {'elts': 'tag1?,tag2?', 'attrs': []}}) dtd_dict = { 'tag': {'elts': '(tag1)*', 'attrs': []}, } dic = dtd_parser._create_class_dict(dtd_dict) self.assertEqual(len(dic), 1) tag = dic['tag'] self.assertTrue(issubclass(tag, Element)) self.assertEqual(tag.tagname, 'tag') self.assertEqual(tag._is_empty, False) self.assertEqual(tag._attribute_names, []) self.assertEqual(tag.children_classes, []) dtd_dict = { 'tag': {'elts': '(tag1|tag2)', 'attrs': []}, } dic = dtd_parser._create_class_dict(dtd_dict) self.assertEqual(len(dic), 1) tag = dic['tag'] self.assertTrue(issubclass(tag, Element)) self.assertEqual(tag.tagname, 'tag') self.assertEqual(tag._is_empty, False) self.assertEqual(tag._attribute_names, []) self.assertEqual(tag.children_classes, []) dtd_dict = { 'tag': {'elts': 'EMPTY', 'attrs': []}, } dic = dtd_parser._create_class_dict(dtd_dict) self.assertEqual(len(dic), 1) tag = dic['tag'] self.assertTrue(issubclass(tag, TextElement)) self.assertEqual(tag.tagname, 'tag') self.assertEqual(tag._is_empty, True) self.assertEqual(tag.children_classes, [])