~leonardr/beautifulsoup/bs4

« back to all changes in this revision

Viewing changes to bs4/tests/test_formatter.py

  • Committer: Leonard Richardson
  • Date: 2021-09-13 00:59:43 UTC
  • Revision ID: leonardr@segfault.org-20210913005943-uysm9cnv249h78d9
Ported unit tests to use pytest.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
        # Attributes come out sorted by name. In Python 3, attributes
19
19
        # normally come out of a dictionary in the order they were
20
20
        # added.
21
 
        self.assertEqual([('a', 2), ('b', 1)], formatter.attributes(tag))
 
21
        assert [('a', 2), ('b', 1)] == formatter.attributes(tag)
22
22
 
23
23
        # This works even if Tag.attrs is None, though this shouldn't
24
24
        # normally happen.
25
25
        tag.attrs = None
26
 
        self.assertEqual([], formatter.attributes(tag))
 
26
        assert [] == formatter.attributes(tag)
27
27
        
28
28
    def test_sort_attributes(self):
29
29
        # Test the ability to override Formatter.attributes() to,
42
42
 
43
43
        # attributes() was called on the <p> tag. It filtered out one
44
44
        # attribute and sorted the other two.
45
 
        self.assertEqual(formatter.called_with, soup.p)
46
 
        self.assertEqual('<p aval="2" cval="1"></p>', decoded)
 
45
        assert formatter.called_with == soup.p
 
46
        assert '<p aval="2" cval="1"></p>' == decoded
47
47
 
48
48
    def test_empty_attributes_are_booleans(self):
49
49
        # Test the behavior of empty_attributes_are_booleans as well
51
51
        
52
52
        for name in ('html', 'minimal', None):
53
53
            formatter = HTMLFormatter.REGISTRY[name]
54
 
            self.assertEqual(False, formatter.empty_attributes_are_booleans)
 
54
            assert False == formatter.empty_attributes_are_booleans
55
55
 
56
56
        formatter = XMLFormatter.REGISTRY[None]
57
 
        self.assertEqual(False, formatter.empty_attributes_are_booleans)
 
57
        assert False == formatter.empty_attributes_are_booleans
58
58
 
59
59
        formatter = HTMLFormatter.REGISTRY['html5']
60
 
        self.assertEqual(True, formatter.empty_attributes_are_booleans)
 
60
        assert True == formatter.empty_attributes_are_booleans
61
61
 
62
62
        # Verify that the constructor sets the value.
63
63
        formatter = Formatter(empty_attributes_are_booleans=True)
64
 
        self.assertEqual(True, formatter.empty_attributes_are_booleans)
 
64
        assert True == formatter.empty_attributes_are_booleans
65
65
 
66
66
        # Now demonstrate what it does to markup.
67
67
        for markup in (
70
70
        ):
71
71
            soup = self.soup(markup)
72
72
            for formatter in ('html', 'minimal', 'xml', None):
73
 
                self.assertEqual(
74
 
                    b'<option selected=""></option>',
75
 
                    soup.option.encode(formatter='html')
76
 
                )
77
 
                self.assertEqual(
78
 
                    b'<option selected></option>',
79
 
                    soup.option.encode(formatter='html5')
80
 
                )
 
73
                assert b'<option selected=""></option>' == soup.option.encode(formatter='html')
 
74
                assert b'<option selected></option>' == soup.option.encode(formatter='html5')
81
75