def testElementFactory(self): template = ('<{type} label="{label}"{extra}>' ' <title>' ' {title}' ' </title>' ' <comment>{comment}</comment>' ' {content}' '</{type}>' '<suspend/>') def format_real(*args): label, title = args[0][0].split(' ', 1) # Q1. Spamalot content = '\n'.join(args[0][1:]) if args[0][1:] else '' # any extra lines in vbuffer? if args[3]: # make xml attrs if they exist attrs_str = ' ' + ' '.join('%s="%s"' % (k, v) for k, v in args[3].items()) else: attrs_str = '' return template.format(type=args[1], label=label, extra=attrs_str, title=title, comment=args[2], content=content) testElements = ((['Q1 SPAM'], 'radio', 'comment', {}), (['Q2 SPAM'], 'checkbox', '', {}), (['Q3 SPAM'], 'select', '', dict(optional='0')), (['Q1 HAM'], 'checkbox', 'SPAM', {'EGGS': 'MORNING!'}), (['Q2 EGG'], 'BACON', 'SPAM', dict(a=1, b=2, c=3)), (['Q3 Anything without spam?', '<row label="r1">Spam</row>'], 'spam', '', dict(a=1, b=2, c=3)), ) for e in testElements: elTest = clean_xml(format_real(*e)) xmlTest = etree.fromstring('<root>{0}</root>'.format(elTest)) elReal = clean_xml(''.join(decipher.element_factory(*e))) xmlReal = etree.fromstring('<root>{0}</root>'.format(elReal)) self.assertEqual(*map(etree.tostring, (xmlTest, xmlReal))) badElements = ((['SPAM'], 'radio', 'EGGS', {}), # No label ) for e in badElements: self.assertRaises(Exception, decipher.element_factory, e)
def testCellFactoryLabelRgx(self): testTitles = ('Q1. SPAM', 'Q2: EGGS', '(Q3) HAM', 'Q3.1 BACON', 'Q4. Q4. Q4.') resultXMLs = [] for title in testTitles: resultXMLs.append(decipher.element_factory([title], 'radio', '', {})) cleanedTitles = (('Q1', 'SPAM'), ('Q2', 'EGGS'), ('Q3', 'HAM'), ('Q3_1', 'BACON'), ('Q4', 'Q4. Q4.')) expectedXMLs = [] for label, title in cleanedTitles: formatDict = dict(type='radio', label=label, extra='', title=title, comment='', content='') expectedXMLs.append(self.elementTemplate.format(**formatDict).split('\n')) for madeXML, expectedXML in zip(resultXMLs, expectedXMLs): self.assertEqual(madeXML, expectedXML)