def test_get_attr_value(self): t = wtp.Tag('<t n1=v1 n2=v2 n1=v3 n2=v4>c</t>') self.assertEqual(t.get('n1'), 'v3') self.assertEqual(t.get_attr('n2'), 'v4') t = wtp.Tag('<t a>c</t>') self.assertEqual(t.get_attr('a'), '') self.assertEqual(t.get_attr('z'), None)
def test_name(self): t = wtp.Tag('<t>c</t>') self.assertEqual(t.name, 't') t.name = 'a' self.assertEqual(repr(t), "Tag('<a>c</a>')") t = wtp.Tag('<t/>') self.assertEqual(t.name, 't') t.name = 'n' self.assertEqual(t.string, '<n/>')
def test_set_attr_value(self): t = wtp.Tag('<t n1=v1 n2=v2 n1=\'v3\'>c</t>') t.set('n1', 'v4') t.set_attr('n2', 'v5') self.assertEqual(t.string, '<t n1=v1 n2="v5" n1="v4">c</t>') t.set_attr('id', '1') self.assertEqual(t.string, '<t n1=v1 n2="v5" n1="v4" id="1">c</t>') t = wtp.Tag('<t>c</t>') t.set_attr('n', '') self.assertEqual(t.string, '<t n>c</t>')
def test_contents(self): t = wtp.Tag('<t>\nc\n</t>') self.assertEqual(t.contents, '\nc\n') t.contents = 'n' self.assertEqual(t.string, '<t>n</t>') t = wtp.Tag('<t></t>') self.assertEqual(t.contents, '') t.contents = 'n' self.assertEqual(t.string, '<t>n</t>') t = wtp.Tag('<t/>') self.assertEqual(t.contents, '') t.contents = 'n' self.assertEqual(t.string, '<t>n</t>')
def test_parsed_contents(self): t = wtp.Tag('<t>c [[w]]</t>') c1 = t.parsed_contents self.assertEqual(repr(c1), "SubWikiText('c [[w]]')") self.assertEqual(c1.wikilinks[0].target, 'w') # The new contents object won't create a new span c2 = t.parsed_contents self.assertEqual(len(c2._type_to_spans['WikiLink']), 1)
def test_contents_contains_tl(self): t = wtp.Tag('<b>{{text|t}}</b>') self.assertEqual(t.contents, '{{text|t}}')
def test_attrs(self): t = wtp.Tag('<t n1=v1 n2="v2" n3>c</t>') self.assertEqual(t.attrs, {'n1': 'v1', 'n2': 'v2', 'n3': ''})
def test_has_attr(self): t = wtp.Tag('<t n1=v1>c</t>') self.assertTrue(t.has('n1')) self.assertFalse(t.has_attr('n2'))
def test_attr_deletion(self): t = wtp.Tag('<t n1=v1 n1=v333 n2=v22>c</t>') t.delete('n1') self.assertEqual(t.string, '<t n2=v22>c</t>')