blob: a493f6778ae24f2097d682ff1e01c8369d3972ac [file] [log] [blame]
brettw9dffb542016-01-22 18:40:031# 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
5import gn_helpers
6import unittest
7
8class 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_ParseNumber(self):
31 parser = gn_helpers.GNValueParser('123')
32 self.assertEqual(parser.ParseNumber(), 123)
33
34 with self.assertRaises(gn_helpers.GNException):
35 parser = gn_helpers.GNValueParser('')
36 parser.ParseNumber()
37 with self.assertRaises(gn_helpers.GNException):
38 parser = gn_helpers.GNValueParser('a123')
39 parser.ParseNumber()
40
41 def test_ParseString(self):
42 parser = gn_helpers.GNValueParser('"asdf"')
43 self.assertEqual(parser.ParseString(), 'asdf')
44
45 with self.assertRaises(gn_helpers.GNException):
46 parser = gn_helpers.GNValueParser('') # Empty.
47 parser.ParseString()
48 with self.assertRaises(gn_helpers.GNException):
49 parser = gn_helpers.GNValueParser('asdf') # Unquoted.
50 parser.ParseString()
51 with self.assertRaises(gn_helpers.GNException):
52 parser = gn_helpers.GNValueParser('"trailing') # Unterminated.
53 parser.ParseString()
54
55 def test_ParseList(self):
56 parser = gn_helpers.GNValueParser('[1,]') # Optional end comma OK.
57 self.assertEqual(parser.ParseList(), [ 1 ])
58
59 with self.assertRaises(gn_helpers.GNException):
60 parser = gn_helpers.GNValueParser('') # Empty.
61 parser.ParseList()
62 with self.assertRaises(gn_helpers.GNException):
63 parser = gn_helpers.GNValueParser('asdf') # No [].
64 parser.ParseList()
65 with self.assertRaises(gn_helpers.GNException):
66 parser = gn_helpers.GNValueParser('[1, 2') # Unterminated
67 parser.ParseList()
68 with self.assertRaises(gn_helpers.GNException):
69 parser = gn_helpers.GNValueParser('[1 2]') # No separating comma.
70 parser.ParseList()
71
72if __name__ == '__main__':
73 unittest.main()