1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_bluetooth_sapphire/internal/host/testing/parse_args.h"
16
17 #include <string>
18 #include <string_view>
19
20 #include "pw_unit_test/framework.h"
21
22 namespace bt::testing {
23 namespace {
24
TEST(ParseArgsTest,GetArgValueNoHyphens)25 TEST(ParseArgsTest, GetArgValueNoHyphens) {
26 int argc = 2;
27 std::string arg0("test");
28 std::string arg1("key=value");
29 char* argv[] = {arg0.data(), arg1.data()};
30 EXPECT_FALSE(GetArgValue("key", argc, argv));
31 }
32
TEST(ParseArgsTest,GetArgValueNoValue)33 TEST(ParseArgsTest, GetArgValueNoValue) {
34 int argc = 2;
35 std::string arg0("test");
36 std::string arg1("--key");
37 char* argv[] = {arg0.data(), arg1.data()};
38 EXPECT_FALSE(GetArgValue("key", argc, argv));
39 }
40
TEST(ParseArgsTest,GetArgValueEmptyValue)41 TEST(ParseArgsTest, GetArgValueEmptyValue) {
42 int argc = 2;
43 std::string arg0("test");
44 std::string arg1("--key=");
45 char* argv[] = {arg0.data(), arg1.data()};
46 std::optional<std::string_view> value = GetArgValue("key", argc, argv);
47 ASSERT_TRUE(value.has_value());
48 EXPECT_EQ(value->size(), 0u);
49 }
50
TEST(ParseArgsTest,GetArgValueSuccess)51 TEST(ParseArgsTest, GetArgValueSuccess) {
52 int argc = 2;
53 std::string arg0("test");
54 std::string arg1("--key=value");
55 std::string expected_value("value");
56 char* argv[] = {arg0.data(), arg1.data()};
57 std::optional<std::string_view> value = GetArgValue("key", argc, argv);
58 ASSERT_TRUE(value.has_value());
59 EXPECT_EQ(value.value(), expected_value);
60 }
61
TEST(ParseArgsTest,GetArgValueMultipleArgs)62 TEST(ParseArgsTest, GetArgValueMultipleArgs) {
63 int argc = 3;
64 std::string arg0("test");
65 std::string arg1("--abc=def");
66 std::string arg2("--key=value");
67 std::string expected_value("value");
68 char* argv[] = {arg0.data(), arg1.data(), arg2.data()};
69 std::optional<std::string_view> value = GetArgValue("key", argc, argv);
70 ASSERT_TRUE(value.has_value());
71 EXPECT_EQ(value.value(), expected_value);
72 }
73
74 } // namespace
75 } // namespace bt::testing
76