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