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.
8 class UnitTest(unittest.TestCase):
9 def test_ToGNString(self):
11 gn_helpers.ToGNString([1, 'two', [ '"thr$\\', True, False, [] ]]),
12 '[ 1, "two", [ "\\"thr\\$\\\\", true, false, [ ] ] ]')
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.
18 gn_helpers.UnescapeGNString('\\as\\$\\\\asd\\"'),
21 def test_FromGNString(self):
23 gn_helpers.FromGNString('[1, -20, true, false,["as\\"", []]]'),
24 [ 1, -20, True, False, [ 'as"', [] ] ])
26 with self.assertRaises(gn_helpers.GNException):
27 parser = gn_helpers.GNValueParser('123 456')
30 def test_ParseBool(self):
31 parser = gn_helpers.GNValueParser('true')
32 self.assertEqual(parser.Parse(), True)
34 parser = gn_helpers.GNValueParser('false')
35 self.assertEqual(parser.Parse(), False)
37 def test_ParseNumber(self):
38 parser = gn_helpers.GNValueParser('123')
39 self.assertEqual(parser.ParseNumber(), 123)
41 with self.assertRaises(gn_helpers.GNException):
42 parser = gn_helpers.GNValueParser('')
44 with self.assertRaises(gn_helpers.GNException):
45 parser = gn_helpers.GNValueParser('a123')
48 def test_ParseString(self):
49 parser = gn_helpers.GNValueParser('"asdf"')
50 self.assertEqual(parser.ParseString(), 'asdf')
52 with self.assertRaises(gn_helpers.GNException):
53 parser = gn_helpers.GNValueParser('') # Empty.
55 with self.assertRaises(gn_helpers.GNException):
56 parser = gn_helpers.GNValueParser('asdf') # Unquoted.
58 with self.assertRaises(gn_helpers.GNException):
59 parser = gn_helpers.GNValueParser('"trailing') # Unterminated.
62 def test_ParseList(self):
63 parser = gn_helpers.GNValueParser('[1,]') # Optional end comma OK.
64 self.assertEqual(parser.ParseList(), [ 1 ])
66 with self.assertRaises(gn_helpers.GNException):
67 parser = gn_helpers.GNValueParser('') # Empty.
69 with self.assertRaises(gn_helpers.GNException):
70 parser = gn_helpers.GNValueParser('asdf') # No [].
72 with self.assertRaises(gn_helpers.GNException):
73 parser = gn_helpers.GNValueParser('[1, 2') # Unterminated
75 with self.assertRaises(gn_helpers.GNException):
76 parser = gn_helpers.GNValueParser('[1 2]') # No separating comma.
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})
84 # Whitespace is not required; strings should also work.
85 self.assertEqual(gn_helpers.FromGNArgs('foo="bar baz"'),
88 # Comments should work (and be ignored).
90 '# Top-level comment.',
92 'bar = 1 # In-line comment.',
94 self.assertEqual(gn_helpers.FromGNArgs('\n'.join(gn_args_lines)),
95 {'foo': True, 'bar': 1})
98 self.assertEqual(gn_helpers.FromGNArgs('foo=[1, 2, 3]'),
101 # Empty strings should return an empty dict.
102 self.assertEqual(gn_helpers.FromGNArgs(''), {})
103 self.assertEqual(gn_helpers.FromGNArgs(' \n '), {})
105 # Non-identifiers should raise an exception.
106 with self.assertRaises(gn_helpers.GNException):
107 gn_helpers.FromGNArgs('123 = true')
109 # References to other variables should raise an exception.
110 with self.assertRaises(gn_helpers.GNException):
111 gn_helpers.FromGNArgs('foo = bar')
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")')
117 # Underscores in identifiers should work.
118 self.assertEqual(gn_helpers.FromGNArgs('_foo = true'),
120 self.assertEqual(gn_helpers.FromGNArgs('foo_bar = true'),
122 self.assertEqual(gn_helpers.FromGNArgs('foo_=true'),
125 if __name__ == '__main__':