Commit c450aa3b authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[test][lib] Code cleanup

parent 5de3888b
......@@ -46,70 +46,73 @@ from ..library import Library, Storage
def test_valid():
l = Library(prefix, "user/valid/1")
assert l.valid, "\n * %s" % "\n * ".join(l.errors)
nose.tools.eq_(len(l.uses), 0)
nose.tools.eq_(len(l.libraries), 0)
lib = Library(prefix, "user/valid/1")
nose.tools.assert_true(lib.valid, "\n * %s" % "\n * ".join(lib.errors))
nose.tools.eq_(len(lib.uses), 0)
nose.tools.eq_(len(lib.libraries), 0)
# tries to call the function `f()' on that library
module = l.load()
assert isinstance(module, types.ModuleType)
module = lib.load()
nose.tools.assert_true(isinstance(module, types.ModuleType))
nose.tools.eq_(module.f(), "OK")
nose.tools.eq_(module.pyver(), "%d.%d" % sys.version_info[:2])
def test_nested_1():
l = Library(prefix, "user/nest1/1")
assert l.valid, "\n * %s" % "\n * ".join(l.errors)
nose.tools.eq_(len(l.uses), 1)
nose.tools.eq_(len(l.libraries), 1)
lib = Library(prefix, "user/nest1/1")
nose.tools.assert_true(lib.valid, "\n * %s" % "\n * ".join(lib.errors))
nose.tools.eq_(len(lib.uses), 1)
nose.tools.eq_(len(lib.libraries), 1)
# tries to call the function `f()' on that library
module = l.load()
assert isinstance(module, types.ModuleType)
module = lib.load()
nose.tools.assert_true(isinstance(module, types.ModuleType))
nose.tools.eq_(module.f("-extra"), "OK-extra")
nose.tools.eq_(module.pyver("-extra"), "%d.%d-extra" % sys.version_info[:2])
def test_nested_2():
l = Library(prefix, "user/nest2/1")
assert l.valid, "\n * %s" % "\n * ".join(l.errors)
nose.tools.eq_(len(l.uses), 1)
nose.tools.eq_(len(l.libraries), 1)
lib = Library(prefix, "user/nest2/1")
nose.tools.assert_true(lib.valid, "\n * %s" % "\n * ".join(lib.errors))
nose.tools.eq_(len(lib.uses), 1)
nose.tools.eq_(len(lib.libraries), 1)
# tries to call the function `f()' on that library
module = l.load()
assert isinstance(module, types.ModuleType)
module = lib.load()
nose.tools.assert_true(isinstance(module, types.ModuleType))
nose.tools.eq_(module.f("-x"), "OK-x-x")
nose.tools.eq_(module.pyver("-x"), "%d.%d-x-x" % sys.version_info[:2])
def test_direct_recursion():
l = Library(prefix, "user/direct_recursion/1")
assert not l.valid
nose.tools.eq_(len(l.errors), 1)
assert l.errors[0].find("recursion for library") != -1
assert l.errors[0].find(l.name) != -1
lib = Library(prefix, "user/direct_recursion/1")
nose.tools.assert_false(lib.valid)
nose.tools.eq_(len(lib.errors), 1)
nose.tools.assert_not_equal(lib.errors[0].find("recursion for library"), -1)
nose.tools.assert_not_equal(lib.errors[0].find(lib.name), -1)
def test_indirect_recursion():
l = Library(prefix, "user/indirect_recursion/1")
assert not l.valid
nose.tools.eq_(len(l.errors), 1)
assert l.errors[0].find("referred library") != -1
assert l.errors[0].find("user/indirect_recursion_next/1") != -1
lib = Library(prefix, "user/indirect_recursion/1")
nose.tools.assert_false(lib.valid)
nose.tools.eq_(len(lib.errors), 1)
nose.tools.assert_not_equal(lib.errors[0].find("referred library"), -1)
nose.tools.assert_not_equal(
lib.errors[0].find("user/indirect_recursion_next/1"), -1
)
def test_invalid_mix():
l = Library(prefix, "user/invalid_mix/1")
assert not l.valid
nose.tools.eq_(len(l.errors), 1)
assert l.errors[0].find("differs from current language") != -1
lib = Library(prefix, "user/invalid_mix/1")
nose.tools.assert_false(lib.valid)
nose.tools.eq_(len(lib.errors), 1)
nose.tools.assert_not_equal(lib.errors[0].find("differs from current language"), -1)
@nose.tools.with_setup(teardown=cleanup)
......@@ -118,79 +121,79 @@ def test_dependencies():
name = "user/for_dep/1"
dep_name = "user/dep/1"
l = Library(prefix, name)
assert l.valid, "\n * %s" % "\n * ".join(l.errors)
nose.tools.eq_(len(l.uses), 0)
nose.tools.eq_(len(l.libraries), 0)
lib = Library(prefix, name)
nose.tools.assert_true(lib.valid, "\n * %s" % "\n * ".join(lib.errors))
nose.tools.eq_(len(lib.uses), 0)
nose.tools.eq_(len(lib.libraries), 0)
# Save to temporary storage, so we can test modifications on it
new_storage = Storage(tmp_prefix, name)
l.write(new_storage)
lib.write(new_storage)
l_dep = Library(prefix, dep_name)
assert l_dep.valid
nose.tools.assert_true(l_dep.valid)
new_dep_storage = Storage(tmp_prefix, dep_name)
l_dep.write(new_dep_storage)
# Reload
l = Library(tmp_prefix, name)
assert l.valid, "\n * %s" % "\n * ".join(l.errors)
lib = Library(tmp_prefix, name)
nose.tools.assert_true(lib.valid, "\n * %s" % "\n * ".join(lib.errors))
l_dep = Library(tmp_prefix, dep_name)
assert l_dep.valid
nose.tools.assert_true(l_dep.valid)
l.uses["dep1"] = l_dep.name
l.write() # rewrite
lib.uses["dep1"] = l_dep.name
lib.write() # rewrite
# Re-validate
l = Library(tmp_prefix, name)
assert l.valid, "\n * %s" % "\n * ".join(l.errors)
lib = Library(tmp_prefix, name)
nose.tools.assert_true(lib.valid, "\n * %s" % "\n * ".join(lib.errors))
nose.tools.eq_(len(l.uses), 1)
nose.tools.eq_(len(l.libraries), 1)
nose.tools.eq_(list(l.uses.keys())[0], "dep1")
nose.tools.eq_(list(l.uses.values())[0], "user/dep/1")
nose.tools.eq_(len(lib.uses), 1)
nose.tools.eq_(len(lib.libraries), 1)
nose.tools.eq_(list(lib.uses.keys())[0], "dep1")
nose.tools.eq_(list(lib.uses.values())[0], "user/dep/1")
l.uses = {} # reset
l.uses["mod1"] = l_dep.name
l.write() # rewrite
lib.uses = {} # reset
lib.uses["mod1"] = l_dep.name
lib.write() # rewrite
# Re-validate
l = Library(tmp_prefix, name)
assert l.valid, "\n * %s" % "\n * ".join(l.errors)
lib = Library(tmp_prefix, name)
nose.tools.assert_true(lib.valid, "\n * %s" % "\n * ".join(lib.errors))
nose.tools.eq_(len(l.uses), 1)
nose.tools.eq_(len(l.libraries), 1)
nose.tools.eq_(list(l.uses.keys())[0], "mod1")
nose.tools.eq_(list(l.uses.values())[0], "user/dep/1")
nose.tools.eq_(len(lib.uses), 1)
nose.tools.eq_(len(lib.libraries), 1)
nose.tools.eq_(list(lib.uses.keys())[0], "mod1")
nose.tools.eq_(list(lib.uses.values())[0], "user/dep/1")
l.uses = {} # reset
l.write() # rewrite
lib.uses = {} # reset
lib.write() # rewrite
# Re-validate
l = Library(tmp_prefix, name)
assert l.valid, "\n * %s" % "\n * ".join(l.errors)
lib = Library(tmp_prefix, name)
nose.tools.assert_true(lib.valid, "\n * %s" % "\n * ".join(lib.errors))
nose.tools.eq_(len(l.uses), 0)
nose.tools.eq_(len(l.libraries), 0)
nose.tools.eq_(len(lib.uses), 0)
nose.tools.eq_(len(lib.libraries), 0)
@nose.tools.with_setup(teardown=cleanup)
def test_adding_self():
name = "user/for_dep/1"
l = Library(prefix, name)
assert l.valid, "\n * %s" % "\n * ".join(l.errors)
nose.tools.eq_(len(l.uses), 0)
nose.tools.eq_(len(l.libraries), 0)
lib = Library(prefix, name)
nose.tools.assert_true(lib.valid, "\n * %s" % "\n * ".join(lib.errors))
nose.tools.eq_(len(lib.uses), 0)
nose.tools.eq_(len(lib.libraries), 0)
l.uses["dep"] = l.name
lib.uses["dep"] = lib.name
new_storage = Storage(tmp_prefix, name)
l.write(new_storage) # rewrite
lib.write(new_storage) # rewrite
# Re-validate
l = Library(tmp_prefix, name)
assert not l.valid
assert l.errors[0].find("recursion") != -1
lib = Library(tmp_prefix, name)
nose.tools.assert_false(lib.valid)
nose.tools.assert_not_equal(lib.errors[0].find("recursion"), -1)
@nose.tools.with_setup(teardown=cleanup)
......@@ -199,33 +202,33 @@ def test_invalid_dependencies():
name = "user/for_dep/1"
dep_name = "user/invalid_dep/1"
l = Library(prefix, name)
assert l.valid, "\n * %s" % "\n * ".join(l.errors)
nose.tools.eq_(len(l.uses), 0)
nose.tools.eq_(len(l.libraries), 0)
lib = Library(prefix, name)
nose.tools.assert_true(lib.valid, "\n * %s" % "\n * ".join(lib.errors))
nose.tools.eq_(len(lib.uses), 0)
nose.tools.eq_(len(lib.libraries), 0)
# Save to temporary storage, so we can test modifications on it
new_storage = Storage(tmp_prefix, name)
l.write(new_storage)
lib.write(new_storage)
l_dep = Library(prefix, dep_name)
assert l_dep.valid
nose.tools.assert_true(l_dep.valid)
new_dep_storage = Storage(tmp_prefix, dep_name)
l_dep.write(new_dep_storage)
# Reload
l = Library(tmp_prefix, name)
assert l.valid, "\n * %s" % "\n * ".join(l.errors)
lib = Library(tmp_prefix, name)
nose.tools.assert_true(lib.valid, "\n * %s" % "\n * ".join(lib.errors))
l_dep = Library(tmp_prefix, dep_name)
assert l_dep.valid
nose.tools.assert_true(l_dep.valid)
l.uses["dep1"] = l_dep.name
l.write() # rewrite
lib.uses["dep1"] = l_dep.name
lib.write() # rewrite
# Re-validate
l = Library(tmp_prefix, name)
assert not l.valid
assert l.errors[0].find("differs from current language") != -1
lib = Library(tmp_prefix, name)
nose.tools.assert_false(lib.valid)
nose.tools.assert_not_equal(lib.errors[0].find("differs from current language"), -1)
@nose.tools.with_setup(teardown=cleanup)
......@@ -233,10 +236,10 @@ def test_export():
name = "user/nest2/1"
obj = Library(prefix, name)
assert obj.valid, "\n * %s" % "\n * ".join(obj.errors)
nose.tools.assert_true(obj.valid, "\n * %s" % "\n * ".join(obj.errors))
obj.export(tmp_prefix)
# load from tmp_prefix and validates
exported = Library(tmp_prefix, name)
assert exported.valid, "\n * %s" % "\n * ".join(exported.errors)
nose.tools.assert_true(exported.valid, "\n * %s" % "\n * ".join(exported.errors))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment