1 /*
2 * Copyright (C) 2021 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 "tools/cmdline_builder.h"
18
19 #include <utility>
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23
24 namespace art {
25 namespace tools {
26 namespace {
27
28 using ::testing::ElementsAre;
29 using ::testing::IsEmpty;
30
31 class CmdlineBuilderTest : public testing::Test {
32 protected:
33 CmdlineBuilder args_;
34 };
35
TEST_F(CmdlineBuilderTest,ContainsOneFormatSpecifier)36 TEST_F(CmdlineBuilderTest, ContainsOneFormatSpecifier) {
37 EXPECT_TRUE(internal::ContainsOneFormatSpecifier("--flag=%s", 's'));
38 EXPECT_TRUE(internal::ContainsOneFormatSpecifier("--flag=[%s]", 's'));
39 EXPECT_TRUE(internal::ContainsOneFormatSpecifier("--flag=%s%%", 's'));
40 EXPECT_TRUE(internal::ContainsOneFormatSpecifier("--flag=[%s%%]", 's'));
41 EXPECT_TRUE(internal::ContainsOneFormatSpecifier("--flag=%%%s", 's'));
42 EXPECT_FALSE(internal::ContainsOneFormatSpecifier("--flag=", 's'));
43 EXPECT_FALSE(internal::ContainsOneFormatSpecifier("--flag=%s%s", 's'));
44 EXPECT_FALSE(internal::ContainsOneFormatSpecifier("--flag=%s%", 's'));
45 EXPECT_FALSE(internal::ContainsOneFormatSpecifier("--flag=%d", 's'));
46 EXPECT_FALSE(internal::ContainsOneFormatSpecifier("--flag=%s%d", 's'));
47 EXPECT_FALSE(internal::ContainsOneFormatSpecifier("--flag=%%s", 's'));
48 }
49
TEST_F(CmdlineBuilderTest,Add)50 TEST_F(CmdlineBuilderTest, Add) {
51 args_.Add("--flag");
52 EXPECT_THAT(args_.Get(), ElementsAre("--flag"));
53 }
54
TEST_F(CmdlineBuilderTest,AddRuntime)55 TEST_F(CmdlineBuilderTest, AddRuntime) {
56 args_.AddRuntime("--flag");
57 EXPECT_THAT(args_.Get(), ElementsAre("--runtime-arg", "--flag"));
58 }
59
TEST_F(CmdlineBuilderTest,AddString)60 TEST_F(CmdlineBuilderTest, AddString) {
61 args_.Add("--flag=[%s]", "foo");
62 EXPECT_THAT(args_.Get(), ElementsAre("--flag=[foo]"));
63 }
64
TEST_F(CmdlineBuilderTest,AddRuntimeString)65 TEST_F(CmdlineBuilderTest, AddRuntimeString) {
66 args_.AddRuntime("--flag=[%s]", "foo");
67 EXPECT_THAT(args_.Get(), ElementsAre("--runtime-arg", "--flag=[foo]"));
68 }
69
TEST_F(CmdlineBuilderTest,AddInt)70 TEST_F(CmdlineBuilderTest, AddInt) {
71 args_.Add("--flag=[%d]", 123);
72 EXPECT_THAT(args_.Get(), ElementsAre("--flag=[123]"));
73 }
74
TEST_F(CmdlineBuilderTest,AddRuntimeInt)75 TEST_F(CmdlineBuilderTest, AddRuntimeInt) {
76 args_.AddRuntime("--flag=[%d]", 123);
77 EXPECT_THAT(args_.Get(), ElementsAre("--runtime-arg", "--flag=[123]"));
78 }
79
TEST_F(CmdlineBuilderTest,AddIfNonEmpty)80 TEST_F(CmdlineBuilderTest, AddIfNonEmpty) {
81 args_.AddIfNonEmpty("--flag=[%s]", "foo");
82 EXPECT_THAT(args_.Get(), ElementsAre("--flag=[foo]"));
83 }
84
TEST_F(CmdlineBuilderTest,AddIfNonEmptyEmpty)85 TEST_F(CmdlineBuilderTest, AddIfNonEmptyEmpty) {
86 args_.AddIfNonEmpty("--flag=[%s]", "");
87 EXPECT_THAT(args_.Get(), IsEmpty());
88 }
89
TEST_F(CmdlineBuilderTest,AddRuntimeIfNonEmpty)90 TEST_F(CmdlineBuilderTest, AddRuntimeIfNonEmpty) {
91 args_.AddRuntimeIfNonEmpty("--flag=[%s]", "foo");
92 EXPECT_THAT(args_.Get(), ElementsAre("--runtime-arg", "--flag=[foo]"));
93 }
94
TEST_F(CmdlineBuilderTest,AddRuntimeIfNonEmptyEmpty)95 TEST_F(CmdlineBuilderTest, AddRuntimeIfNonEmptyEmpty) {
96 args_.AddRuntimeIfNonEmpty("--flag=[%s]", "");
97 EXPECT_THAT(args_.Get(), IsEmpty());
98 }
99
TEST_F(CmdlineBuilderTest,AddIfTrue)100 TEST_F(CmdlineBuilderTest, AddIfTrue) {
101 args_.AddIf(true, "--flag");
102 EXPECT_THAT(args_.Get(), ElementsAre("--flag"));
103 }
104
TEST_F(CmdlineBuilderTest,AddIfFalse)105 TEST_F(CmdlineBuilderTest, AddIfFalse) {
106 args_.AddIf(false, "--flag");
107 EXPECT_THAT(args_.Get(), IsEmpty());
108 }
109
TEST_F(CmdlineBuilderTest,AddRuntimeIfTrue)110 TEST_F(CmdlineBuilderTest, AddRuntimeIfTrue) {
111 args_.AddRuntimeIf(true, "--flag");
112 EXPECT_THAT(args_.Get(), ElementsAre("--runtime-arg", "--flag"));
113 }
114
TEST_F(CmdlineBuilderTest,AddRuntimeIfFalse)115 TEST_F(CmdlineBuilderTest, AddRuntimeIfFalse) {
116 args_.AddRuntimeIf(false, "--flag");
117 EXPECT_THAT(args_.Get(), IsEmpty());
118 }
119
TEST_F(CmdlineBuilderTest,Concat)120 TEST_F(CmdlineBuilderTest, Concat) {
121 args_.Add("--flag1");
122 args_.Add("--flag2");
123
124 CmdlineBuilder other;
125 other.Add("--flag3");
126 other.Add("--flag4");
127
128 args_.Concat(std::move(other));
129 EXPECT_THAT(args_.Get(), ElementsAre("--flag1", "--flag2", "--flag3", "--flag4"));
130 // NOLINTNEXTLINE - checking all args have been moved from other to args_
131 EXPECT_THAT(other.Get(), IsEmpty());
132 }
133
134 } // namespace
135 } // namespace tools
136 } // namespace art
137