def test_one_requirement(self): """Tests the correct tag is injected if there is one requirement.""" requirement = 'mísc.javascript' caterpillar.inject_script_tags( self.soup, [requirement], 'path', BOILERPLATE_DIR, '') self.assertIsNotNone( self.soup.find('script', src=os.path.join('path', BOILERPLATE_DIR, requirement)))
def test_one_requirement(self): """Tests the correct tag is injected if there is one requirement.""" requirement = 'mísc.javascript' caterpillar.inject_script_tags(self.soup, [requirement], 'path', BOILERPLATE_DIR, '') self.assertIsNotNone( self.soup.find('script', src=os.path.join('path', BOILERPLATE_DIR, requirement)))
def test_many_requirements_tag_existence(self): """Tests the correct tags are injected if there are many requirements.""" requirements = ['réq', 'uir', 'edf', 'ile', 's.j', 'ava', 'scr', 'ipt'] caterpillar.inject_script_tags( self.soup, requirements, 'path', BOILERPLATE_DIR, '') for requirement in requirements: self.assertIsNotNone( self.soup.find('script', src=os.path.join('path', BOILERPLATE_DIR, requirement)))
def test_many_requirements_tag_existence(self): """Tests the correct tags are injected if there are many requirements.""" requirements = ['réq', 'uir', 'edf', 'ile', 's.j', 'ava', 'scr', 'ipt'] caterpillar.inject_script_tags(self.soup, requirements, 'path', BOILERPLATE_DIR, '') for requirement in requirements: self.assertIsNotNone( self.soup.find('script', src=os.path.join('path', BOILERPLATE_DIR, requirement)))
def test_many_requirements_tag_order(self): """Tests tags are injected in order.""" requirements = ['réq', 'uir', 'edf', 'ile', 's.j', 'ava', 'scr', 'ipt'] caterpillar.inject_script_tags( self.soup, requirements, 'path', BOILERPLATE_DIR, '') srcs = [script['src'] for script in self.soup('script')] upto = -1 for requirement in requirements: # Monotonically increasing indices <-> order is preserved. index = srcs.index(os.path.join('path', BOILERPLATE_DIR, requirement)) self.assertGreater(index, upto) upto = index
def test_many_requirements_tag_order(self): """Tests tags are injected in order.""" requirements = ['réq', 'uir', 'edf', 'ile', 's.j', 'ava', 'scr', 'ipt'] caterpillar.inject_script_tags(self.soup, requirements, 'path', BOILERPLATE_DIR, '') srcs = [script['src'] for script in self.soup('script')] upto = -1 for requirement in requirements: # Monotonically increasing indices <-> order is preserved. index = srcs.index( os.path.join('path', BOILERPLATE_DIR, requirement)) self.assertGreater(index, upto) upto = index
def test_no_body_or_html(self): """Tests the correct tags are injected if there is no body or html tag.""" html = """\ <!DOCTYPE html> <h1> Hello, Wo®ld! </h1> """ soup = bs4.BeautifulSoup(html, 'html.parser') requirements = ['réq', 'uir'] caterpillar.inject_script_tags( soup, requirements, 'path', BOILERPLATE_DIR, '') self.assertEqual(re.sub(r'\s+', ' ', unicode(soup)), re.sub(r'\s+', ' ', """\ <!DOCTYPE html> <h1> Hello, Wo®ld! </h1> <script src="path/bóilerplate dir/réq"></script><script src="path/bóilerplate dir/uir"></script>"""))
def test_no_body_or_html(self): """Tests the correct tags are injected if there is no body or html tag.""" html = """\ <!DOCTYPE html> <h1> Hello, Wo®ld! </h1> """ soup = bs4.BeautifulSoup(html, 'html.parser') requirements = ['réq', 'uir'] caterpillar.inject_script_tags(soup, requirements, 'path', BOILERPLATE_DIR, '') self.assertEqual( re.sub(r'\s+', ' ', unicode(soup)), re.sub( r'\s+', ' ', """\ <!DOCTYPE html> <h1> Hello, Wo®ld! </h1> <script src="path/bóilerplate dir/réq"></script><script src="path/bóilerplate dir/uir"></script>"""))
def test_no_requirements(self): """Tests no tags are injected if there are no requirements.""" init_soup = copy.copy(self.soup) caterpillar.inject_script_tags(self.soup, [], '.', BOILERPLATE_DIR, '') self.assertEqual(self.soup, init_soup)