def test_compiled_icomments(self): """Test comments iterator from compiled pattern.""" markup = """ <!-- before header --> <html> <head> </head> <body> <!-- comment --> <p id="1"><code id="2"></code><img id="3" src="./image.png"/></p> <pre id="4"></pre> <p><span id="5" class="some-class"></span><span id="some-id"></span></p> <pre id="6" class='ignore'> <!-- don't ignore --> </pre> </body> </html> """ soup = self.soup(markup, 'html5lib') pattern = sv.compile('div', None, 0) comments = [ sv_util.ustr(c).strip() for c in pattern.icomments(soup, limit=2) ] self.assertEqual(sorted(comments), sorted(['before header', 'comment']))
def test_compiled_comments(self): """Test comments from compiled pattern.""" markup = """ <!-- before header --> <html> <head> </head> <body> <!-- comment --> <p id="1"><code id="2"></code><img id="3" src="./image.png"/></p> <pre id="4"></pre> <p><span id="5" class="some-class"></span><span id="some-id"></span></p> <pre id="6" class='ignore'> <!-- don't ignore --> </pre> </body> </html> """ soup = self.soup(markup, 'html.parser') # Check that comments on compiled object work just like `sv.comments` pattern = sv.compile('div', None, 0) comments = [sv_util.ustr(c).strip() for c in pattern.comments(soup)] self.assertEqual(sorted(comments), sorted(['before header', 'comment', "don't ignore"]))
def test_cache(self): """Test cache.""" sv.purge() self.assertEqual(sv.cp._cached_css_compile.cache_info().currsize, 0) for x in range(1000): value = '[value="{}"]'.format(sv_util.ustr(random.randint(1, 10000))) p = sv.compile(value) self.assertTrue(p.pattern == value) self.assertTrue(sv.cp._cached_css_compile.cache_info().currsize > 0) self.assertTrue(sv.cp._cached_css_compile.cache_info().currsize == 500) sv.purge() self.assertEqual(sv.cp._cached_css_compile.cache_info().currsize, 0)
def test_comments(self): """Test comments.""" markup = """ <!-- before header --> <html> <head> </head> <body> <!-- comment --> <p id="1"><code id="2"></code><img id="3" src="./image.png"/></p> <pre id="4"></pre> <p><span id="5" class="some-class"></span><span id="some-id"></span></p> <pre id="6" class='ignore'> <!-- don't ignore --> </pre> </body> </html> """ soup = self.soup(markup, 'html5lib') comments = [sv_util.ustr(c).strip() for c in sv.comments(soup)] self.assertEqual(sorted(comments), sorted(['before header', 'comment', "don't ignore"]))