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 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_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 # Lists should work. 89 self.assertEqual(gn_helpers.FromGNArgs('foo=[1, 2, 3]'), 90 {'foo': [1, 2, 3]}) 91 92 # Empty strings should return an empty dict. 93 self.assertEqual(gn_helpers.FromGNArgs(''), {}) 94 self.assertEqual(gn_helpers.FromGNArgs(' \n '), {}) 95 96 # Non-identifiers should raise an exception. 97 with self.assertRaises(gn_helpers.GNException): 98 gn_helpers.FromGNArgs('123 = true') 99 100 # References to other variables should raise an exception. 101 with self.assertRaises(gn_helpers.GNException): 102 gn_helpers.FromGNArgs('foo = bar') 103 104 # References to functions should raise an exception. 105 with self.assertRaises(gn_helpers.GNException): 106 gn_helpers.FromGNArgs('foo = exec_script("//build/baz.py")') 107 108 # Underscores in identifiers should work. 109 self.assertEqual(gn_helpers.FromGNArgs('_foo = true'), 110 {'_foo': True}) 111 self.assertEqual(gn_helpers.FromGNArgs('foo_bar = true'), 112 {'foo_bar': True}) 113 self.assertEqual(gn_helpers.FromGNArgs('foo_=true'), 114 {'foo_': True}) 115 116if __name__ == '__main__': 117 unittest.main() 118