testconverters.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2009 Søren Roug, European Environment Agency
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or at your option any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s):
  19. #
  20. import unittest
  21. from odf import grammar, attrconverters
  22. from odf.namespaces import OFFICENS
  23. def findconv(attribute, element):
  24. converter = attrconverters.attrconverters.get((attribute,element), None)
  25. if converter is not None:
  26. return attribute
  27. else:
  28. converter = attrconverters.attrconverters.get((attribute, None), None)
  29. if converter is not None:
  30. return attribute
  31. return ""
  32. class TestConverters(unittest.TestCase):
  33. allattrs = {}
  34. allqattrs = {}
  35. def testConverters(self):
  36. """ Check that there are converters for all attributes and vice versa"""
  37. for element,attrs in grammar.allowed_attributes.items():
  38. if attrs:
  39. for attr in attrs:
  40. self.allattrs[attr] = 1
  41. self.allqattrs[(attr, element)] = 1
  42. self.assertEqual(attr, findconv(attr, element))
  43. for (attr,elem) in attrconverters.attrconverters.keys():
  44. if attr == (OFFICENS,u'process-content'): # Special attribute
  45. continue
  46. if elem is None:
  47. self.assertEqual(self.allattrs[attr], 1)
  48. else:
  49. self.assertEqual(self.allqattrs[(attr, elem)], 1)
  50. def testBooleanConverter(self):
  51. """ Check that the boolean converter understands the values """
  52. self.assertEqual(attrconverters.cnv_boolean("usesoftpagebreak", 'false', None), 'false')
  53. self.assertEqual(attrconverters.cnv_boolean("usesoftpagebreak", 'true', None), 'true')
  54. self.assertEqual(attrconverters.cnv_boolean("usesoftpagebreak", True, None), 'true')
  55. self.assertEqual(attrconverters.cnv_boolean("usesoftpagebreak", False, None), 'false')
  56. self.assertEqual(attrconverters.cnv_boolean("usesoftpagebreak", 1, None), 'true')
  57. self.assertEqual(attrconverters.cnv_boolean("usesoftpagebreak", 0, None), 'false')
  58. self.assertRaises(ValueError, attrconverters.cnv_boolean, "usesoftpagebreak", '', None)
  59. self.assertRaises(ValueError, attrconverters.cnv_boolean, "usesoftpagebreak", 'on', None)
  60. self.assertRaises(ValueError, attrconverters.cnv_boolean, "usesoftpagebreak", None, None)
  61. # self.assertEqual(attrconverters.cnv_boolean("usesoftpagebreak", '', None), 'false')
  62. # self.assertEqual(attrconverters.cnv_boolean("usesoftpagebreak", 'on', None), 'true')
  63. if __name__ == '__main__':
  64. unittest.main()