def test_class_loader_loads_from_module(): mox = Mox() mox.StubOutWithMock(io, 'os') mox.StubOutWithMock(io.sys, 'path') io.os.path = mox.CreateMockAnything() io.__import__ = mox.CreateMockAnything() class_path = '/full/path/to/module/' module_name = 'module' module_dir = '/full/path/to/' io.os.path = mox.CreateMockAnything() io.os.path.isdir(class_path).AndReturn(True) io.os.path.split(class_path.rstrip('/')).AndReturn((module_dir, module_name)) io.sys.path.append(module_dir) io.sys.path.pop() module_mock = mox.CreateMockAnything() module_mock.ClassIWantToLoad = 'should_be_expected_class' io.__import__('module').AndReturn(module_mock) mox.ReplayAll() try: cl = io.ClassLoader(class_path) assert_equals(cl.load('ClassIWantToLoad'), 'should_be_expected_class') mox.VerifyAll() finally: io.__import__ = __import__ mox.UnsetStubs()
def test_class_loader_loads_from_file(): mox = Mox() mox.StubOutWithMock(io, 'os') mox.StubOutWithMock(io.sys, 'path') io.os.path = mox.CreateMockAnything() io.__import__ = mox.CreateMockAnything() class_dir = '/full/path/to/module/or' class_file = 'file.py' class_path = '%s/%s' % (class_dir, class_file) io.os.path.isdir(class_path).AndReturn(False) io.os.path.split(class_path).AndReturn((class_dir, class_file)) io.os.path.splitext(class_file).AndReturn(('file', '.py')) io.sys.path.append(class_dir) io.sys.path.pop() module_mock = mox.CreateMockAnything() module_mock.ClassIWantToLoad = 'should_be_expected_class' io.__import__('file').AndReturn(module_mock) mox.ReplayAll() try: cl = io.ClassLoader(class_path) assert_equals(cl.load('ClassIWantToLoad'), 'should_be_expected_class') mox.VerifyAll() finally: io.__import__ = __import__ mox.UnsetStubs()
def test_class_loader_loads_from_module(): mox = Mox() mox.StubOutWithMock(io, 'os') mox.StubOutWithMock(io.sys, 'path') io.os.path = mox.CreateMockAnything() io.__import__ = mox.CreateMockAnything() class_path = '/full/path/to/module/' module_name = 'module' module_dir = '/full/path/to/' io.os.path = mox.CreateMockAnything() io.os.path.isdir(class_path).AndReturn(True) io.os.path.split(class_path.rstrip('/')).AndReturn( (module_dir, module_name)) io.sys.path.append(module_dir) io.sys.path.pop() module_mock = mox.CreateMockAnything() module_mock.ClassIWantToLoad = 'should_be_expected_class' io.__import__('module').AndReturn(module_mock) mox.ReplayAll() try: cl = io.ClassLoader(class_path) assert_equals(cl.load('ClassIWantToLoad'), 'should_be_expected_class') mox.VerifyAll() finally: io.__import__ = __import__ mox.UnsetStubs()