test_modules.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Jun 24 09:43:53 2014
  4. @author: pietro
  5. """
  6. import sys
  7. from fnmatch import fnmatch
  8. from grass.gunittest.case import TestCase
  9. from grass.gunittest.main import test
  10. from grass.script.core import get_commands
  11. from grass.pygrass.modules.interface import Module
  12. PY2 = sys.version_info[0] == 2
  13. if PY2:
  14. from StringIO import StringIO
  15. else:
  16. from io import BytesIO as StringIO
  17. SKIP = ["g.parser", ]
  18. # taken from six
  19. def with_metaclass(meta, *bases):
  20. """Create a base class with a metaclass."""
  21. # This requires a bit of explanation: the basic idea is to make a dummy
  22. # metaclass for one level of class instantiation that replaces itself with
  23. # the actual metaclass.
  24. class metaclass(meta):
  25. def __new__(cls, name, this_bases, d):
  26. return meta(name, bases, d)
  27. return type.__new__(metaclass, 'temporary_class', (), {})
  28. class ModulesMeta(type):
  29. def __new__(mcs, name, bases, dict):
  30. def gen_test(cmd):
  31. def test(self):
  32. Module(cmd)
  33. return test
  34. cmds = [c for c in sorted(list(get_commands()[0]))
  35. if c not in SKIP and not fnmatch(c, "g.gui.*")]
  36. for cmd in cmds:
  37. test_name = "test__%s" % cmd.replace('.', '_')
  38. dict[test_name] = gen_test(cmd)
  39. return type.__new__(mcs, name, bases, dict)
  40. class TestModules(with_metaclass(ModulesMeta, TestCase)):
  41. pass
  42. class TestModulesPickability(TestCase):
  43. def test_rsun(self):
  44. """Test if a Module instance is pickable"""
  45. import pickle
  46. out = StringIO()
  47. pickle.dump(Module('r.sun'), out)
  48. out.close()
  49. if __name__ == '__main__':
  50. test()