Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / build / gn_helpers_unittest.py
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import gn_helpers
6 import unittest
7
8 class UnitTest(unittest.TestCase):
9   def test_ToGNString(self):
10     self.assertEqual(
11         gn_helpers.ToGNString([1, 'two', [ '"thr$\\', True, False, [] ]]),
12         '[ 1, "two", [ "\\"thr\\$\\\\", true, false, [  ] ] ]')
13
14   def test_UnescapeGNString(self):
15     # Backslash followed by a \, $, or " means the folling character without
16     # the special meaning. Backslash followed by everything else is a literal.
17     self.assertEqual(
18         gn_helpers.UnescapeGNString('\\as\\$\\\\asd\\"'),
19         '\\as$\\asd"')
20
21   def test_FromGNString(self):
22     self.assertEqual(
23         gn_helpers.FromGNString('[1, -20, true, false,["as\\"", []]]'),
24         [ 1, -20, True, False, [ 'as"', [] ] ])
25
26     with self.assertRaises(gn_helpers.GNException):
27       parser = gn_helpers.GNValueParser('123 456')
28       parser.Parse()
29
30   def test_ParseBool(self):
31     parser = gn_helpers.GNValueParser('true')
32     self.assertEqual(parser.Parse(), True)
33
34     parser = gn_helpers.GNValueParser('false')
35     self.assertEqual(parser.Parse(), False)
36
37   def test_ParseNumber(self):
38     parser = gn_helpers.GNValueParser('123')
39     self.assertEqual(parser.ParseNumber(), 123)
40
41     with self.assertRaises(gn_helpers.GNException):
42       parser = gn_helpers.GNValueParser('')
43       parser.ParseNumber()
44     with self.assertRaises(gn_helpers.GNException):
45       parser = gn_helpers.GNValueParser('a123')
46       parser.ParseNumber()
47
48   def test_ParseString(self):
49     parser = gn_helpers.GNValueParser('"asdf"')
50     self.assertEqual(parser.ParseString(), 'asdf')
51
52     with self.assertRaises(gn_helpers.GNException):
53       parser = gn_helpers.GNValueParser('')  # Empty.
54       parser.ParseString()
55     with self.assertRaises(gn_helpers.GNException):
56       parser = gn_helpers.GNValueParser('asdf')  # Unquoted.
57       parser.ParseString()
58     with self.assertRaises(gn_helpers.GNException):
59       parser = gn_helpers.GNValueParser('"trailing')  # Unterminated.
60       parser.ParseString()
61
62   def test_ParseList(self):
63     parser = gn_helpers.GNValueParser('[1,]')  # Optional end comma OK.
64     self.assertEqual(parser.ParseList(), [ 1 ])
65
66     with self.assertRaises(gn_helpers.GNException):
67       parser = gn_helpers.GNValueParser('')  # Empty.
68       parser.ParseList()
69     with self.assertRaises(gn_helpers.GNException):
70       parser = gn_helpers.GNValueParser('asdf')  # No [].
71       parser.ParseList()
72     with self.assertRaises(gn_helpers.GNException):
73       parser = gn_helpers.GNValueParser('[1, 2')  # Unterminated
74       parser.ParseList()
75     with self.assertRaises(gn_helpers.GNException):
76       parser = gn_helpers.GNValueParser('[1 2]')  # No separating comma.
77       parser.ParseList()
78
79   def test_FromGNArgs(self):
80     # Booleans and numbers should work; whitespace is allowed works.
81     self.assertEqual(gn_helpers.FromGNArgs('foo = true\nbar = 1\n'),
82                      {'foo': True, 'bar': 1})
83
84     # Whitespace is not required; strings should also work.
85     self.assertEqual(gn_helpers.FromGNArgs('foo="bar baz"'),
86                      {'foo': 'bar baz'})
87
88     # Comments should work (and be ignored).
89     gn_args_lines = [
90         '# Top-level comment.',
91         'foo = true',
92         'bar = 1  # In-line comment.',
93     ]
94     self.assertEqual(gn_helpers.FromGNArgs('\n'.join(gn_args_lines)),
95                      {'foo': True, 'bar': 1})
96
97     # Lists should work.
98     self.assertEqual(gn_helpers.FromGNArgs('foo=[1, 2, 3]'),
99                      {'foo': [1, 2, 3]})
100
101     # Empty strings should return an empty dict.
102     self.assertEqual(gn_helpers.FromGNArgs(''), {})
103     self.assertEqual(gn_helpers.FromGNArgs(' \n '), {})
104
105     # Non-identifiers should raise an exception.
106     with self.assertRaises(gn_helpers.GNException):
107       gn_helpers.FromGNArgs('123 = true')
108
109     # References to other variables should raise an exception.
110     with self.assertRaises(gn_helpers.GNException):
111       gn_helpers.FromGNArgs('foo = bar')
112
113     # References to functions should raise an exception.
114     with self.assertRaises(gn_helpers.GNException):
115       gn_helpers.FromGNArgs('foo = exec_script("//build/baz.py")')
116
117     # Underscores in identifiers should work.
118     self.assertEqual(gn_helpers.FromGNArgs('_foo = true'),
119                      {'_foo': True})
120     self.assertEqual(gn_helpers.FromGNArgs('foo_bar = true'),
121                      {'foo_bar': True})
122     self.assertEqual(gn_helpers.FromGNArgs('foo_=true'),
123                      {'foo_': True})
124
125 if __name__ == '__main__':
126   unittest.main()