From 0909884d21621394c0e810c508ceb4a4743ab8b5 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Mon, 20 Feb 2012 10:01:21 -0500 Subject: Added code from 2.7's standard library so that the tests will run on Python 2.6. --- bs4/testing.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'bs4/testing.py') diff --git a/bs4/testing.py b/bs4/testing.py index c374a29..967261d 100644 --- a/bs4/testing.py +++ b/bs4/testing.py @@ -1,6 +1,7 @@ """Helper classes for tests.""" import unittest +from unittest import TestCase from bs4 import BeautifulSoup from bs4.element import Comment, SoupStrainer from bs4.builder import LXMLTreeBuilder @@ -31,3 +32,34 @@ class SoupTest(unittest.TestCase): compare_parsed_to = to_parse self.assertEqual(obj.decode(), self.document_for(compare_parsed_to)) + +# Code copied from Python 2.7 standard library, unittest.py, for use +# in Python 2.6. + +def _id(obj): + return obj + + +def skip(reason): + """ + Unconditionally skip a test. + """ + def decorator(test_item): + if not (isinstance(test_item, type) and issubclass(test_item, TestCase)): + @functools.wraps(test_item) + def skip_wrapper(*args, **kwargs): + raise SkipTest(reason) + test_item = skip_wrapper + + test_item.__unittest_skip__ = True + test_item.__unittest_skip_why__ = reason + return test_item + return decorator + +def skipIf(condition, reason): + """ + Skip a test if the condition is true. + """ + if condition: + return skip(reason) + return _id -- cgit v1.2.3 From ab7ed77ab3560f6d574d577befc7a1f593e45327 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Mon, 20 Feb 2012 11:43:46 -0500 Subject: Changd the class structure so that the default parser test class uses html.parser. --- bs4/testing.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'bs4/testing.py') diff --git a/bs4/testing.py b/bs4/testing.py index 967261d..0008821 100644 --- a/bs4/testing.py +++ b/bs4/testing.py @@ -4,14 +4,18 @@ import unittest from unittest import TestCase from bs4 import BeautifulSoup from bs4.element import Comment, SoupStrainer -from bs4.builder import LXMLTreeBuilder - +try: + from bs4.builder import LXMLTreeBuilder + default_builder = LXMLTreeBuilder +except ImportError, e: + from bs4.builder import HTMLParserTreeBuilder + default_builder = HTMLParserTreeBuilder class SoupTest(unittest.TestCase): @property def default_builder(self): - return LXMLTreeBuilder() + return default_builder() def soup(self, markup, **kwargs): """Build a Beautiful Soup object from markup.""" -- cgit v1.2.3 From 3cf25e3143a8765a0fd129a6ab368f617ee1653c Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Mon, 20 Feb 2012 12:19:33 -0500 Subject: copied skipIf didn't work, so made a smaller one. --- bs4/testing.py | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) (limited to 'bs4/testing.py') diff --git a/bs4/testing.py b/bs4/testing.py index 0008821..e5e8c93 100644 --- a/bs4/testing.py +++ b/bs4/testing.py @@ -1,5 +1,6 @@ """Helper classes for tests.""" +import functools import unittest from unittest import TestCase from bs4 import BeautifulSoup @@ -37,33 +38,14 @@ class SoupTest(unittest.TestCase): self.assertEqual(obj.decode(), self.document_for(compare_parsed_to)) -# Code copied from Python 2.7 standard library, unittest.py, for use -# in Python 2.6. - -def _id(obj): - return obj - - -def skip(reason): - """ - Unconditionally skip a test. - """ - def decorator(test_item): - if not (isinstance(test_item, type) and issubclass(test_item, TestCase)): - @functools.wraps(test_item) - def skip_wrapper(*args, **kwargs): - raise SkipTest(reason) - test_item = skip_wrapper +def skipIf(condition, reason): + def nothing(test, *args, **kwargs): + return None - test_item.__unittest_skip__ = True - test_item.__unittest_skip_why__ = reason - return test_item - return decorator + def decorator(test_item): + if condition: + return nothing + else: + return test_item -def skipIf(condition, reason): - """ - Skip a test if the condition is true. - """ - if condition: - return skip(reason) - return _id + return decorator -- cgit v1.2.3