test_modules.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.exceptions import ParameterError
  12. from grass.pygrass.modules.interface import Module
  13. PY2 = sys.version_info[0] == 2
  14. if PY2:
  15. from StringIO import StringIO
  16. else:
  17. from io import BytesIO as StringIO
  18. SKIP = ["g.parser", ]
  19. # taken from six
  20. def with_metaclass(meta, *bases):
  21. """Create a base class with a metaclass."""
  22. # This requires a bit of explanation: the basic idea is to make a dummy
  23. # metaclass for one level of class instantiation that replaces itself with
  24. # the actual metaclass.
  25. class metaclass(meta):
  26. def __new__(cls, name, this_bases, d):
  27. return meta(name, bases, d)
  28. return type.__new__(metaclass, 'temporary_class', (), {})
  29. class ModulesMeta(type):
  30. def __new__(mcs, name, bases, dict):
  31. def gen_test(cmd):
  32. def test(self):
  33. Module(cmd)
  34. return test
  35. cmds = [c for c in sorted(list(get_commands()[0]))
  36. if c not in SKIP and not fnmatch(c, "g.gui.*")]
  37. for cmd in cmds:
  38. test_name = "test__%s" % cmd.replace('.', '_')
  39. dict[test_name] = gen_test(cmd)
  40. return type.__new__(mcs, name, bases, dict)
  41. class TestModules(with_metaclass(ModulesMeta, TestCase)):
  42. pass
  43. class TestModulesPickability(TestCase):
  44. def test_rsun(self):
  45. """Test if a Module instance is pickable"""
  46. import pickle
  47. out = StringIO()
  48. pickle.dump(Module('r.sun'), out)
  49. out.close()
  50. class TestModulesCheck(TestCase):
  51. def test_flags_with_suppress_required(self):
  52. """Test if flags with suppress required are handle correctly"""
  53. gextension = Module('g.extension')
  54. # check if raise an error if required parameter are missing
  55. with self.assertRaises(ParameterError):
  56. gextension.check()
  57. # check if the flag suppress the required parameters
  58. gextension.flags.a = True
  59. self.assertIsNone(gextension.check())
  60. if __name__ == '__main__':
  61. test()