bc4a6f893e50410288e72e1c03884c646edd46a601e1e0a2ce78775dd72e317da9e3bb1b1dc1c4ca56956f46f9699a148d4625eba5286bea6b9d9d4e16083d 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2011 Google Inc. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """ Unit tests for the easy_xml.py file. """
  6. import gyp.easy_xml as easy_xml
  7. import unittest
  8. from io import StringIO
  9. class TestSequenceFunctions(unittest.TestCase):
  10. def setUp(self):
  11. self.stderr = StringIO()
  12. def test_EasyXml_simple(self):
  13. self.assertEqual(
  14. easy_xml.XmlToString(["test"]),
  15. '<?xml version="1.0" encoding="utf-8"?><test/>',
  16. )
  17. self.assertEqual(
  18. easy_xml.XmlToString(["test"], encoding="Windows-1252"),
  19. '<?xml version="1.0" encoding="Windows-1252"?><test/>',
  20. )
  21. def test_EasyXml_simple_with_attributes(self):
  22. self.assertEqual(
  23. easy_xml.XmlToString(["test2", {"a": "value1", "b": "value2"}]),
  24. '<?xml version="1.0" encoding="utf-8"?><test2 a="value1" b="value2"/>',
  25. )
  26. def test_EasyXml_escaping(self):
  27. original = "<test>'\"\r&\nfoo"
  28. converted = "&lt;test&gt;'&quot;&#xD;&amp;&#xA;foo"
  29. converted_apos = converted.replace("'", "&apos;")
  30. self.assertEqual(
  31. easy_xml.XmlToString(["test3", {"a": original}, original]),
  32. '<?xml version="1.0" encoding="utf-8"?><test3 a="%s">%s</test3>'
  33. % (converted, converted_apos),
  34. )
  35. def test_EasyXml_pretty(self):
  36. self.assertEqual(
  37. easy_xml.XmlToString(
  38. ["test3", ["GrandParent", ["Parent1", ["Child"]], ["Parent2"]]],
  39. pretty=True,
  40. ),
  41. '<?xml version="1.0" encoding="utf-8"?>\n'
  42. "<test3>\n"
  43. " <GrandParent>\n"
  44. " <Parent1>\n"
  45. " <Child/>\n"
  46. " </Parent1>\n"
  47. " <Parent2/>\n"
  48. " </GrandParent>\n"
  49. "</test3>\n",
  50. )
  51. def test_EasyXml_complex(self):
  52. # We want to create:
  53. target = (
  54. '<?xml version="1.0" encoding="utf-8"?>'
  55. "<Project>"
  56. '<PropertyGroup Label="Globals">'
  57. "<ProjectGuid>{D2250C20-3A94-4FB9-AF73-11BC5B73884B}</ProjectGuid>"
  58. "<Keyword>Win32Proj</Keyword>"
  59. "<RootNamespace>automated_ui_tests</RootNamespace>"
  60. "</PropertyGroup>"
  61. '<Import Project="$(VCTargetsPath)\\Microsoft.Cpp.props"/>'
  62. "<PropertyGroup "
  63. "Condition=\"'$(Configuration)|$(Platform)'=="
  64. '\'Debug|Win32\'" Label="Configuration">'
  65. "<ConfigurationType>Application</ConfigurationType>"
  66. "<CharacterSet>Unicode</CharacterSet>"
  67. "<SpectreMitigation>SpectreLoadCF</SpectreMitigation>"
  68. "<VCToolsVersion>14.36.32532</VCToolsVersion>"
  69. "</PropertyGroup>"
  70. "</Project>"
  71. )
  72. xml = easy_xml.XmlToString(
  73. [
  74. "Project",
  75. [
  76. "PropertyGroup",
  77. {"Label": "Globals"},
  78. ["ProjectGuid", "{D2250C20-3A94-4FB9-AF73-11BC5B73884B}"],
  79. ["Keyword", "Win32Proj"],
  80. ["RootNamespace", "automated_ui_tests"],
  81. ],
  82. ["Import", {"Project": "$(VCTargetsPath)\\Microsoft.Cpp.props"}],
  83. [
  84. "PropertyGroup",
  85. {
  86. "Condition": "'$(Configuration)|$(Platform)'=='Debug|Win32'",
  87. "Label": "Configuration",
  88. },
  89. ["ConfigurationType", "Application"],
  90. ["CharacterSet", "Unicode"],
  91. ["SpectreMitigation", "SpectreLoadCF"],
  92. ["VCToolsVersion", "14.36.32532"],
  93. ],
  94. ]
  95. )
  96. self.assertEqual(xml, target)
  97. if __name__ == "__main__":
  98. unittest.main()