test_win32rcparser.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import sys, os
  2. import unittest
  3. import win32rcparser
  4. import win32con
  5. import tempfile
  6. class TestParser(unittest.TestCase):
  7. def setUp(self):
  8. rc_file = os.path.join(os.path.dirname(__file__), "win32rcparser", "test.rc")
  9. self.resources = win32rcparser.Parse(rc_file)
  10. def testStrings(self):
  11. for sid, expected in [
  12. ("IDS_TEST_STRING4", "Test 'single quoted' string"),
  13. ("IDS_TEST_STRING1", 'Test "quoted" string'),
  14. ("IDS_TEST_STRING3", 'String with single " quote'),
  15. ("IDS_TEST_STRING2", 'Test string'),
  16. ]:
  17. got = self.resources.stringTable[sid].value
  18. self.assertEqual(got, expected)
  19. def testStandardIds(self):
  20. for idc in "IDOK IDCANCEL".split():
  21. correct = getattr(win32con, idc)
  22. self.assertEqual(self.resources.names[correct], idc)
  23. self.assertEqual(self.resources.ids[idc], correct)
  24. def testTabStop(self):
  25. d = self.resources.dialogs["IDD_TEST_DIALOG2"]
  26. tabstop_names = ["IDC_EDIT1", "IDOK"] # should have WS_TABSTOP
  27. tabstop_ids = [self.resources.ids[name] for name in tabstop_names]
  28. notabstop_names = ["IDC_EDIT2"] # should have WS_TABSTOP
  29. notabstop_ids = [self.resources.ids[name] for name in notabstop_names]
  30. num_ok = 0
  31. for cdef in d[1:]: # skip dlgdef
  32. #print cdef
  33. cid = cdef[2]
  34. style = cdef[-2]
  35. styleex = cdef[-1]
  36. if cid in tabstop_ids:
  37. self.failUnlessEqual(style & win32con.WS_TABSTOP, win32con.WS_TABSTOP)
  38. num_ok += 1
  39. elif cid in notabstop_ids:
  40. self.failUnlessEqual(style & win32con.WS_TABSTOP, 0)
  41. num_ok += 1
  42. self.failUnlessEqual(num_ok, len(tabstop_ids) + len(notabstop_ids))
  43. class TestGenerated(TestParser):
  44. def setUp(self):
  45. # don't call base!
  46. rc_file = os.path.join(os.path.dirname(__file__), "win32rcparser", "test.rc")
  47. py_file = tempfile.mktemp('test_win32rcparser.py')
  48. try:
  49. win32rcparser.GenerateFrozenResource(rc_file, py_file)
  50. py_source = open(py_file).read()
  51. finally:
  52. if os.path.isfile(py_file):
  53. os.unlink(py_file)
  54. # poor-man's import :)
  55. globs = {}
  56. exec(py_source, globs, globs)
  57. self.resources = globs["FakeParser"]()
  58. if __name__=='__main__':
  59. unittest.main()