/Lib/test/test_fnmatch.py

http://unladen-swallow.googlecode.com/ · Python · 46 lines · 33 code · 10 blank · 3 comment · 3 complexity · 0ef68988f81a89858fbe90aef06693b7 MD5 · raw file

  1. """Test cases for the fnmatch module."""
  2. from test import test_support
  3. import unittest
  4. from fnmatch import fnmatch, fnmatchcase
  5. class FnmatchTestCase(unittest.TestCase):
  6. def check_match(self, filename, pattern, should_match=1):
  7. if should_match:
  8. self.assert_(fnmatch(filename, pattern),
  9. "expected %r to match pattern %r"
  10. % (filename, pattern))
  11. else:
  12. self.assert_(not fnmatch(filename, pattern),
  13. "expected %r not to match pattern %r"
  14. % (filename, pattern))
  15. def test_fnmatch(self):
  16. check = self.check_match
  17. check('abc', 'abc')
  18. check('abc', '?*?')
  19. check('abc', '???*')
  20. check('abc', '*???')
  21. check('abc', '???')
  22. check('abc', '*')
  23. check('abc', 'ab[cd]')
  24. check('abc', 'ab[!de]')
  25. check('abc', 'ab[de]', 0)
  26. check('a', '??', 0)
  27. check('a', 'b', 0)
  28. # these test that '\' is handled correctly in character sets;
  29. # see SF bug #???
  30. check('\\', r'[\]')
  31. check('a', r'[!\]')
  32. check('\\', r'[!\]', 0)
  33. def test_main():
  34. test_support.run_unittest(FnmatchTestCase)
  35. if __name__ == "__main__":
  36. test_main()