2016-06-05 00:54:28 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
2016-06-05 16:15:12 +00:00
|
|
|
test_polyloader
|
2016-06-05 00:54:28 +00:00
|
|
|
----------------------------------
|
|
|
|
|
2016-06-05 16:15:12 +00:00
|
|
|
Tests for `polyloader` module.
|
2016-06-05 00:54:28 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2016-06-05 16:15:12 +00:00
|
|
|
from polyloader import polyloader
|
2016-06-05 00:54:28 +00:00
|
|
|
|
2016-06-05 16:15:12 +00:00
|
|
|
# Note that these compilers don't actually load much out of the
|
2016-06-05 00:54:28 +00:00
|
|
|
# source files. That's not the point. The point is to show that the
|
|
|
|
# correct compiler has been found for a given extension.
|
|
|
|
|
2016-06-05 16:15:12 +00:00
|
|
|
def compiler(pt):
|
|
|
|
def _compiler(source_path, modulename):
|
|
|
|
with open(source_path, "r") as file:
|
|
|
|
return compile("result='Success for %s: %s'" % (pt, file.readline().rstrip()), modulename, "exec")
|
|
|
|
return _compiler
|
2016-06-05 00:54:28 +00:00
|
|
|
|
|
|
|
class Test_Polymorph_1(object):
|
|
|
|
def test_import1(self):
|
2016-06-05 16:15:12 +00:00
|
|
|
polyloader.install(compiler("2"), ['.2'])
|
|
|
|
polyloader.install(compiler("3"), ['.3'])
|
|
|
|
from .polytestmix import test2
|
|
|
|
from .polytestmix import test3
|
|
|
|
assert(test2.result == "Success for 2: Test Two")
|
|
|
|
assert(test3.result == "Success for 3: Test Three")
|
2016-06-05 00:54:28 +00:00
|
|
|
|
|
|
|
class Test_Polymorph_Iterator(object):
|
2016-06-05 16:15:12 +00:00
|
|
|
''' The Django Compatibility test: Can we load arbitrary modules from a package? '''
|
2016-06-05 00:54:28 +00:00
|
|
|
def test_iterator(self):
|
2016-06-05 16:15:12 +00:00
|
|
|
import os
|
|
|
|
import pkgutil
|
|
|
|
import inspect
|
|
|
|
polyloader.install(compiler("2"), ['.2'])
|
|
|
|
polyloader.install(compiler("3"), ['.3'])
|
|
|
|
from .polytestmix import test2
|
|
|
|
from .polytestmix import test3
|
|
|
|
target_dir = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))),
|
|
|
|
'polytestmix')
|
|
|
|
modules = set([name for _, name, is_pkg in pkgutil.iter_modules([target_dir])
|
2016-06-05 00:54:28 +00:00
|
|
|
if not is_pkg and not name.startswith('_')])
|
2016-06-05 16:15:12 +00:00
|
|
|
assert(modules == set(['test1', 'test2', 'test3']))
|
2016-06-05 00:54:28 +00:00
|
|
|
|
|
|
|
|
2016-06-05 16:15:12 +00:00
|
|
|
|