1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#!/usr/bin/env python3
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Tests for java_cpp_strings.py.
This test suite contains various tests for the C++ -> Java string generator.
"""
import unittest
import java_cpp_strings
from util import java_cpp_utils
class _TestStringsParser(unittest.TestCase):
def testParseComments(self):
test_data = """
/**
* This should be ignored as well.
*/
// Comment followed by a blank line.
// Comment followed by unrelated code.
int foo() { return 3; }
// Real comment.
const char kASwitch[] = "a-value";
// Real comment that spans
// multiple lines.
const char kAnotherSwitch[] = "another-value";
// Comment followed by nothing.
""".split('\n')
string_file_parser = java_cpp_utils.CppConstantParser(
java_cpp_strings.StringParserDelegate(), test_data)
strings = string_file_parser.Parse()
self.assertEqual(2, len(strings))
self.assertEqual('A_SWITCH', strings[0].name)
self.assertEqual('"a-value"', strings[0].value)
self.assertEqual(1, len(strings[0].comments.split('\n')))
self.assertEqual('ANOTHER_SWITCH', strings[1].name)
self.assertEqual('"another-value"', strings[1].value)
self.assertEqual(2, len(strings[1].comments.split('\n')))
def testStringValues(self):
test_data = r"""
// Single line string constants.
const char kAString[] = "a-value";
const char kNoComment[] = "no-comment";
namespace myfeature {
const char kMyFeatureNoComment[] = "myfeature.no-comment";
}
// Single line switch with a big space.
const char kAStringWithSpace[] = "a-value";
// Wrapped constant definition.
const char kAStringWithAVeryLongNameThatWillHaveToWrap[] =
"a-string-with-a-very-long-name-that-will-have-to-wrap";
// This one has no comment before it.
const char kAStringWithAVeryLongNameThatWillHaveToWrap2[] =
"a-string-with-a-very-long-name-that-will-have-to-wrap2";
const char kStringWithEscapes[] = "tab\tquote\"newline\n";
const char kStringWithEscapes2[] =
"tab\tquote\"newline\n";
const char kEmptyString[] = "";
// These are valid C++ but not currently supported by the script.
const char kInvalidLineBreak[] =
"invalid-line-break";
const char kConcatenateMultipleStringLiterals[] =
"first line"
"second line";
""".split('\n')
string_file_parser = java_cpp_utils.CppConstantParser(
java_cpp_strings.StringParserDelegate(), test_data)
strings = string_file_parser.Parse()
self.assertEqual(9, len(strings))
self.assertEqual('A_STRING', strings[0].name)
self.assertEqual('"a-value"', strings[0].value)
self.assertEqual('NO_COMMENT', strings[1].name)
self.assertEqual('"no-comment"', strings[1].value)
self.assertEqual('MY_FEATURE_NO_COMMENT', strings[2].name)
self.assertEqual('"myfeature.no-comment"', strings[2].value)
self.assertEqual('A_STRING_WITH_SPACE', strings[3].name)
self.assertEqual('"a-value"', strings[3].value)
self.assertEqual('A_STRING_WITH_A_VERY_LONG_NAME_THAT_WILL_HAVE_TO_WRAP',
strings[4].name)
self.assertEqual('"a-string-with-a-very-long-name-that-will-have-to-wrap"',
strings[4].value)
self.assertEqual('A_STRING_WITH_A_VERY_LONG_NAME_THAT_WILL_HAVE_TO_WRAP2',
strings[5].name)
self.assertEqual('"a-string-with-a-very-long-name-that-will-have-to-wrap2"',
strings[5].value)
self.assertEqual('STRING_WITH_ESCAPES', strings[6].name)
self.assertEqual(r'"tab\tquote\"newline\n"', strings[6].value)
self.assertEqual('STRING_WITH_ESCAPES2', strings[7].name)
self.assertEqual(r'"tab\tquote\"newline\n"', strings[7].value)
self.assertEqual('EMPTY_STRING', strings[8].name)
self.assertEqual('""', strings[8].value)
def testTreatWebViewLikeOneWord(self):
test_data = """
const char kSomeWebViewSwitch[] = "some-webview-switch";
const char kWebViewOtherSwitch[] = "webview-other-switch";
const char kSwitchWithPluralWebViews[] = "switch-with-plural-webviews";
""".split('\n')
string_file_parser = java_cpp_utils.CppConstantParser(
java_cpp_strings.StringParserDelegate(), test_data)
strings = string_file_parser.Parse()
self.assertEqual('SOME_WEBVIEW_SWITCH', strings[0].name)
self.assertEqual('"some-webview-switch"', strings[0].value)
self.assertEqual('WEBVIEW_OTHER_SWITCH', strings[1].name)
self.assertEqual('"webview-other-switch"', strings[1].value)
self.assertEqual('SWITCH_WITH_PLURAL_WEBVIEWS', strings[2].name)
self.assertEqual('"switch-with-plural-webviews"', strings[2].value)
def testTemplateParsing(self):
test_data = """
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package my.java.package;
public any sort of class MyClass {{
{NATIVE_STRINGS}
}}
"""
package, class_name = java_cpp_utils.ParseTemplateFile(test_data)
self.assertEqual('my.java.package', package)
self.assertEqual('MyClass', class_name)
if __name__ == '__main__':
unittest.main()
|