Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_pkg.TestPkg Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def run_code
def mkhier
def test_1
def test_2
def test_3
def test_4
def test_5
def test_6
def test_7
def test_8

Public Attributes

 root
 pkgname
 syspath
 modules_before

Detailed Description

Definition at line 45 of file test_pkg.py.


Member Function Documentation

def test.test_pkg.TestPkg.mkhier (   self,
  descr 
)

Definition at line 69 of file test_pkg.py.

00069 
00070     def mkhier(self, descr):
00071         root = tempfile.mkdtemp()
00072         sys.path.insert(0, root)
00073         if not os.path.isdir(root):
00074             os.mkdir(root)
00075         for name, contents in descr:
00076             comps = name.split()
00077             fullname = root
00078             for c in comps:
00079                 fullname = os.path.join(fullname, c)
00080             if contents is None:
00081                 os.mkdir(fullname)
00082             else:
00083                 f = open(fullname, "w")
00084                 f.write(contents)
00085                 if contents and contents[-1] != '\n':
00086                     f.write('\n')
00087                 f.close()
00088         self.root = root
00089         # package name is the name of the first item
00090         self.pkgname = descr[0][0]

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_pkg.TestPkg.run_code (   self,
  code 
)

Definition at line 66 of file test_pkg.py.

00066 
00067     def run_code(self, code):
00068         exec(textwrap.dedent(code), globals(), {"self": self})

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 47 of file test_pkg.py.

00047 
00048     def setUp(self):
00049         self.root = None
00050         self.pkgname = None
00051         self.syspath = list(sys.path)
00052         self.modules_before = support.modules_setup()

Here is the caller graph for this function:

Definition at line 53 of file test_pkg.py.

00053 
00054     def tearDown(self):
00055         sys.path[:] = self.syspath
00056         support.modules_cleanup(*self.modules_before)
00057         if self.root: # Only clean if the test was actually run
00058             cleanout(self.root)
00059 
00060         # delete all modules concerning the tested hierarchy
00061         if self.pkgname:
00062             modules = [name for name in sys.modules
00063                        if self.pkgname in name.split('.')]
00064             for name in modules:
00065                 del sys.modules[name]

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 91 of file test_pkg.py.

00091 
00092     def test_1(self):
00093         hier = [("t1", None), ("t1 __init__.py", "")]
00094         self.mkhier(hier)
00095         import t1

Here is the call graph for this function:

Definition at line 96 of file test_pkg.py.

00096 
00097     def test_2(self):
00098         hier = [
00099          ("t2", None),
00100          ("t2 __init__.py", "'doc for t2'"),
00101          ("t2 sub", None),
00102          ("t2 sub __init__.py", ""),
00103          ("t2 sub subsub", None),
00104          ("t2 sub subsub __init__.py", "spam = 1"),
00105         ]
00106         self.mkhier(hier)
00107 
00108         import t2.sub
00109         import t2.sub.subsub
00110         self.assertEqual(t2.__name__, "t2")
00111         self.assertEqual(t2.sub.__name__, "t2.sub")
00112         self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
00113 
00114         # This exec crap is needed because Py3k forbids 'import *' outside
00115         # of module-scope and __import__() is insufficient for what we need.
00116         s = """
00117             import t2
00118             from t2 import *
00119             self.assertEqual(dir(), ['self', 'sub', 't2'])
00120             """
00121         self.run_code(s)
00122 
00123         from t2 import sub
00124         from t2.sub import subsub
00125         from t2.sub.subsub import spam
00126         self.assertEqual(sub.__name__, "t2.sub")
00127         self.assertEqual(subsub.__name__, "t2.sub.subsub")
00128         self.assertEqual(sub.subsub.__name__, "t2.sub.subsub")
00129         for name in ['spam', 'sub', 'subsub', 't2']:
00130             self.assertTrue(locals()["name"], "Failed to import %s" % name)
00131 
00132         import t2.sub
00133         import t2.sub.subsub
00134         self.assertEqual(t2.__name__, "t2")
00135         self.assertEqual(t2.sub.__name__, "t2.sub")
00136         self.assertEqual(t2.sub.subsub.__name__, "t2.sub.subsub")
00137 
00138         s = """
00139             from t2 import *
00140             self.assertTrue(dir(), ['self', 'sub'])
00141             """
00142         self.run_code(s)

Here is the call graph for this function:

Definition at line 143 of file test_pkg.py.

00143 
00144     def test_3(self):
00145         hier = [
00146                 ("t3", None),
00147                 ("t3 __init__.py", ""),
00148                 ("t3 sub", None),
00149                 ("t3 sub __init__.py", ""),
00150                 ("t3 sub subsub", None),
00151                 ("t3 sub subsub __init__.py", "spam = 1"),
00152                ]
00153         self.mkhier(hier)
00154 
00155         import t3.sub.subsub
00156         self.assertEqual(t3.__name__, "t3")
00157         self.assertEqual(t3.sub.__name__, "t3.sub")
00158         self.assertEqual(t3.sub.subsub.__name__, "t3.sub.subsub")

Here is the call graph for this function:

Definition at line 159 of file test_pkg.py.

00159 
00160     def test_4(self):
00161         hier = [
00162         ("t4.py", "raise RuntimeError('Shouldnt load t4.py')"),
00163         ("t4", None),
00164         ("t4 __init__.py", ""),
00165         ("t4 sub.py", "raise RuntimeError('Shouldnt load sub.py')"),
00166         ("t4 sub", None),
00167         ("t4 sub __init__.py", ""),
00168         ("t4 sub subsub.py",
00169          "raise RuntimeError('Shouldnt load subsub.py')"),
00170         ("t4 sub subsub", None),
00171         ("t4 sub subsub __init__.py", "spam = 1"),
00172                ]
00173         self.mkhier(hier)
00174 
00175         s = """
00176             from t4.sub.subsub import *
00177             self.assertEqual(spam, 1)
00178             """
00179         self.run_code(s)

Here is the call graph for this function:

Definition at line 180 of file test_pkg.py.

00180 
00181     def test_5(self):
00182         hier = [
00183         ("t5", None),
00184         ("t5 __init__.py", "import t5.foo"),
00185         ("t5 string.py", "spam = 1"),
00186         ("t5 foo.py",
00187          "from . import string; assert string.spam == 1"),
00188          ]
00189         self.mkhier(hier)
00190 
00191         import t5
00192         s = """
00193             from t5 import *
00194             self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
00195             """
00196         self.run_code(s)
00197 
00198         import t5
00199         self.assertEqual(fixdir(dir(t5)),
00200                          ['__cached__', '__doc__', '__file__', '__name__',
00201                           '__package__', '__path__', 'foo', 'string', 't5'])
00202         self.assertEqual(fixdir(dir(t5.foo)),
00203                          ['__cached__', '__doc__', '__file__', '__name__',
00204                           '__package__', 'string'])
00205         self.assertEqual(fixdir(dir(t5.string)),
00206                          ['__cached__', '__doc__', '__file__', '__name__',
00207                           '__package__', 'spam'])

Here is the call graph for this function:

Definition at line 208 of file test_pkg.py.

00208 
00209     def test_6(self):
00210         hier = [
00211                 ("t6", None),
00212                 ("t6 __init__.py",
00213                  "__all__ = ['spam', 'ham', 'eggs']"),
00214                 ("t6 spam.py", ""),
00215                 ("t6 ham.py", ""),
00216                 ("t6 eggs.py", ""),
00217                ]
00218         self.mkhier(hier)
00219 
00220         import t6
00221         self.assertEqual(fixdir(dir(t6)),
00222                          ['__all__', '__cached__', '__doc__', '__file__',
00223                           '__name__', '__package__', '__path__'])
00224         s = """
00225             import t6
00226             from t6 import *
00227             self.assertEqual(fixdir(dir(t6)),
00228                              ['__all__', '__cached__', '__doc__', '__file__',
00229                               '__name__', '__package__', '__path__',
00230                               'eggs', 'ham', 'spam'])
00231             self.assertEqual(dir(), ['eggs', 'ham', 'self', 'spam', 't6'])
00232             """
00233         self.run_code(s)

Here is the call graph for this function:

Definition at line 234 of file test_pkg.py.

00234 
00235     def test_7(self):
00236         hier = [
00237                 ("t7.py", ""),
00238                 ("t7", None),
00239                 ("t7 __init__.py", ""),
00240                 ("t7 sub.py",
00241                  "raise RuntimeError('Shouldnt load sub.py')"),
00242                 ("t7 sub", None),
00243                 ("t7 sub __init__.py", ""),
00244                 ("t7 sub .py",
00245                  "raise RuntimeError('Shouldnt load subsub.py')"),
00246                 ("t7 sub subsub", None),
00247                 ("t7 sub subsub __init__.py",
00248                  "spam = 1"),
00249                ]
00250         self.mkhier(hier)
00251 
00252 
00253         t7, sub, subsub = None, None, None
00254         import t7 as tas
00255         self.assertEqual(fixdir(dir(tas)),
00256                          ['__cached__', '__doc__', '__file__', '__name__',
00257                           '__package__', '__path__'])
00258         self.assertFalse(t7)
00259         from t7 import sub as subpar
00260         self.assertEqual(fixdir(dir(subpar)),
00261                          ['__cached__', '__doc__', '__file__', '__name__',
00262                           '__package__', '__path__'])
00263         self.assertFalse(t7)
00264         self.assertFalse(sub)
00265         from t7.sub import subsub as subsubsub
00266         self.assertEqual(fixdir(dir(subsubsub)),
00267                          ['__cached__', '__doc__', '__file__', '__name__',
00268                          '__package__', '__path__', 'spam'])
00269         self.assertFalse(t7)
00270         self.assertFalse(sub)
00271         self.assertFalse(subsub)
00272         from t7.sub.subsub import spam as ham
00273         self.assertEqual(ham, 1)
00274         self.assertFalse(t7)
00275         self.assertFalse(sub)
00276         self.assertFalse(subsub)

Here is the call graph for this function:

Definition at line 279 of file test_pkg.py.

00279 
00280     def test_8(self):
00281         hier = [
00282                 ("t8", None),
00283                 ("t8 __init__"+os.extsep+"py", "'doc for t8'"),
00284                ]
00285         self.mkhier(hier)
00286 
00287         import t8
00288         self.assertEqual(t8.__doc__, "doc for t8")

Here is the call graph for this function:


Member Data Documentation

Definition at line 51 of file test_pkg.py.

Definition at line 49 of file test_pkg.py.

Definition at line 48 of file test_pkg.py.

Definition at line 50 of file test_pkg.py.


The documentation for this class was generated from the following file: