1 /*
2 * Copyright (C) 2018, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "code_writer.h"
18
19 #include <gtest/gtest.h>
20 #include <string>
21
22 using android::aidl::CodeWriter;
23 using std::string;
24 using std::unique_ptr;
25
26 namespace android {
27 namespace aidl {
28
TEST(CodeWriterTest,AppendOperator)29 TEST(CodeWriterTest, AppendOperator) {
30 string str;
31 CodeWriterPtr ptr = CodeWriter::ForString(&str);
32 CodeWriter& writer = *ptr;
33 writer << "Write this";
34 writer.Close();
35 EXPECT_EQ(str, "Write this");
36 }
37
TEST(CodeWriterTest,AppendOperatorCascade)38 TEST(CodeWriterTest, AppendOperatorCascade) {
39 string str;
40 CodeWriterPtr ptr = CodeWriter::ForString(&str);
41 CodeWriter& writer = *ptr;
42 writer << "Write this " << "and that";
43 writer.Close();
44 EXPECT_EQ(str, "Write this and that");
45 }
46
TEST(CodeWriterTest,MultilineCommentEscapeNoChange)47 TEST(CodeWriterTest, MultilineCommentEscapeNoChange) {
48 const std::string example = "this has nothing weird";
49 EXPECT_EQ(example, MultilineCommentEscape(example));
50 }
TEST(CodeWriterTest,MultilineCommentEscapeUnescapesUnicode)51 TEST(CodeWriterTest, MultilineCommentEscapeUnescapesUnicode) {
52 // b/380411222: Windows paths sometimes look like unicode
53 EXPECT_EQ("a\\\\u", MultilineCommentEscape("a\\u"));
54 }
TEST(CodeWriterTest,MultilineCommentEscapePreventsEndOfComment)55 TEST(CodeWriterTest, MultilineCommentEscapePreventsEndOfComment) {
56 EXPECT_EQ("a* /b", MultilineCommentEscape("a*/b"));
57 }
58
TEST(CodeWriterTest,WorksWithNonAscii)59 TEST(CodeWriterTest, WorksWithNonAscii) {
60 // Due to b/174366536(basic_stringbuf implicit-conversion), the following snippet crashes
61 // std::stringstream s;
62 // s << "가";
63 std::string str;
64 CodeWriterPtr ptr = CodeWriter::ForString(&str);
65 CodeWriter& writer = *ptr;
66 writer << "가";
67 writer.Close();
68 EXPECT_EQ(str, "가");
69 }
70
71 } // namespace aidl
72 } // namespace android
73