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