codekingpro/portable-devtools
114k
1#!/usr/bin/env python3
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 unittest
10from io import StringIO
11
12from gyp import easy_xml
13
14
15class TestSequenceFunctions(unittest.TestCase):
16 def setUp(self):
17 self.stderr = StringIO()
18
19 def test_EasyXml_simple(self):
20 self.assertEqual(
21 easy_xml.XmlToString(["test"]),
22 '<?xml version="1.0" encoding="utf-8"?><test/>',
23 )
24
25 self.assertEqual(
26 easy_xml.XmlToString(["test"], encoding="Windows-1252"),
27 '<?xml version="1.0" encoding="Windows-1252"?><test/>',
28 )
29
30 def test_EasyXml_simple_with_attributes(self):
31 self.assertEqual(
32 easy_xml.XmlToString(["test2", {"a": "value1", "b": "value2"}]),
33 '<?xml version="1.0" encoding="utf-8"?><test2 a="value1" b="value2"/>',
34 )
35
36 def test_EasyXml_escaping(self):
37 original = "<test>'\"\r&\nfoo"
38 converted = "<test>'"
&
foo"
39 converted_apos = converted.replace("'", "'")
40 self.assertEqual(
41 easy_xml.XmlToString(["test3", {"a": original}, original]),
42 '<?xml version="1.0" encoding="utf-8"?><test3 a="%s">%s</test3>'
43 % (converted, converted_apos),
44 )
45
46 def test_EasyXml_pretty(self):
47 self.assertEqual(
48 easy_xml.XmlToString(
49 ["test3", ["GrandParent", ["Parent1", ["Child"]], ["Parent2"]]],
50 pretty=True,
51 ),
52 '<?xml version="1.0" encoding="utf-8"?>\n'
53 "<test3>\n"
54 " <GrandParent>\n"
55 " <Parent1>\n"
56 " <Child/>\n"
57 " </Parent1>\n"
58 " <Parent2/>\n"
59 " </GrandParent>\n"
60 "</test3>\n",
61 )
62
63 def test_EasyXml_complex(self):
64 # We want to create:
65 target = (
66 '<?xml version="1.0" encoding="utf-8"?>'
67 "<Project>"
68 '<PropertyGroup Label="Globals">'
69 "<ProjectGuid>{D2250C20-3A94-4FB9-AF73-11BC5B73884B}</ProjectGuid>"
70 "<Keyword>Win32Proj</Keyword>"
71 "<RootNamespace>automated_ui_tests</RootNamespace>"
72 "</PropertyGroup>"
73 '<Import Project="$(VCTargetsPath)\\Microsoft.Cpp.props"/>'
74 "<PropertyGroup "
75 "Condition=\"'$(Configuration)|$(Platform)'=="
76 '\'Debug|Win32\'" Label="Configuration">'
77 "<ConfigurationType>Application</ConfigurationType>"
78 "<CharacterSet>Unicode</CharacterSet>"
79 "<SpectreMitigation>SpectreLoadCF</SpectreMitigation>"
80 "<VCToolsVersion>14.36.32532</VCToolsVersion>"
81 "</PropertyGroup>"
82 "</Project>"
83 )
84
85 xml = easy_xml.XmlToString(
86 [
87 "Project",
88 [
89 "PropertyGroup",
90 {"Label": "Globals"},
91 ["ProjectGuid", "{D2250C20-3A94-4FB9-AF73-11BC5B73884B}"],
92 ["Keyword", "Win32Proj"],
93 ["RootNamespace", "automated_ui_tests"],
94 ],
95 ["Import", {"Project": "$(VCTargetsPath)\\Microsoft.Cpp.props"}],
96 [
97 "PropertyGroup",
98 {
99 "Condition": "'$(Configuration)|$(Platform)'=='Debug|Win32'",
100 "Label": "Configuration",
101 },
102 ["ConfigurationType", "Application"],
103 ["CharacterSet", "Unicode"],
104 ["SpectreMitigation", "SpectreLoadCF"],
105 ["VCToolsVersion", "14.36.32532"],
106 ],
107 ]
108 )
109 self.assertEqual(xml, target)
110
111
112if __name__ == "__main__":
113 unittest.main()
114 