# Copyright 2016 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file.
def test_UnescapeGNString(self): # Backslash followed by a \, $, or " means the folling character without # the special meaning. Backslash followed by everything else is a literal.
self.assertEqual(
gn_helpers.UnescapeGNString('\\as\\$\\\\asd\\"'), '\\as$\\asd"')
# Empty strings should return an empty dict.
self.assertEqual(gn_helpers.FromGNArgs(''), {})
self.assertEqual(gn_helpers.FromGNArgs(' \n '), {})
# Comments should work everywhere (and be ignored).
gn_args_lines = [ '# Top-level comment.', '', '# Variable comment.', 'foo = true', 'bar = [', ' # Value comment in list.', ' 1,', ' 2,', ']', '', 'baz # Comment anywhere, really', ' = # also here', ' 4',
]
self.assertEqual(gn_helpers.FromGNArgs('\n'.join(gn_args_lines)), { 'foo': True, 'bar': [1, 2], 'baz': 4
})
# Scope should be parsed, even empty ones.
gn_args_lines = [ 'foo = {', ' a = 1', ' b = [', ' { },', ' {', ' c = 1', ' },', ' ]', '}',
]
self.assertEqual(gn_helpers.FromGNArgs('\n'.join(gn_args_lines)),
{'foo': { 'a': 1, 'b': [
{},
{ 'c': 1,
},
]
}})
# Non-identifiers should raise an exception. with self.assertRaises(gn_helpers.GNError):
gn_helpers.FromGNArgs('123 = true')
# References to other variables should raise an exception. with self.assertRaises(gn_helpers.GNError):
gn_helpers.FromGNArgs('foo = bar')
# References to functions should raise an exception. with self.assertRaises(gn_helpers.GNError):
gn_helpers.FromGNArgs('foo = exec_script("//build/baz.py")')
# Underscores in identifiers should work.
self.assertEqual(gn_helpers.FromGNArgs('_foo = true'),
{'_foo': True})
self.assertEqual(gn_helpers.FromGNArgs('foo_bar = true'),
{'foo_bar': True})
self.assertEqual(gn_helpers.FromGNArgs('foo_=true'),
{'foo_': True})
def test_ReplaceImports(self): # Should be a no-op on args inputs without any imports.
parser = gn_helpers.GNValueParser(
textwrap.dedent("""
some_arg1 = "val1"
some_arg2 = "val2" """))
parser.ReplaceImports()
self.assertEqual(
parser.input,
textwrap.dedent("""
some_arg1 = "val1"
some_arg2 = "val2" """))
# A single "import(...)" line should be replaced with the contents of the # file being imported.
parser = gn_helpers.GNValueParser(
textwrap.dedent("""
some_arg1 = "val1" import("//some/args/file.gni")
some_arg2 = "val2" """))
fake_import = 'some_imported_arg = "imported_val"'
builtin_var = '__builtin__'if sys.version_info.major < 3 else'builtins'
open_fun = '{}.open'.format(builtin_var) with mock.patch(open_fun, mock.mock_open(read_data=fake_import)):
parser.ReplaceImports()
self.assertEqual(
parser.input,
textwrap.dedent("""
some_arg1 = "val1"
some_imported_arg = "imported_val"
some_arg2 = "val2" """))
# No trailing parenthesis should raise an exception. with self.assertRaises(gn_helpers.GNError):
parser = gn_helpers.GNValueParser(
textwrap.dedent('import("//some/args/file.gni"'))
parser.ReplaceImports()
# No double quotes should raise an exception. with self.assertRaises(gn_helpers.GNError):
parser = gn_helpers.GNValueParser(
textwrap.dedent('import(//some/args/file.gni)'))
parser.ReplaceImports()
# A path that's not source absolute should raise an exception. with self.assertRaises(gn_helpers.GNError):
parser = gn_helpers.GNValueParser(
textwrap.dedent('import("some/relative/args/file.gni")'))
parser.ReplaceImports()
if __name__ == '__main__':
unittest.main()
Messung V0.5
[ zur Elbe Produktseite wechseln0.12Quellennavigators
Analyse erneut starten
]