1 //
2 // Copyright 2019 The Abseil Authors.
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 // https://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 #include "absl/flags/internal/program_name.h"
17
18 #include <string>
19
20 #include "gtest/gtest.h"
21 #include "absl/strings/match.h"
22 #include "absl/strings/string_view.h"
23
24 namespace {
25
26 namespace flags = absl::flags_internal;
27
TEST(FlagsPathUtilTest,TestInitialProgamName)28 TEST(FlagsPathUtilTest, TestInitialProgamName) {
29 flags::SetProgramInvocationName("absl/flags/program_name_test");
30 std::string program_name = flags::ProgramInvocationName();
31 for (char& c : program_name)
32 if (c == '\\') c = '/';
33
34 #if !defined(__wasm__) && !defined(__asmjs__)
35 const std::string expect_name = "absl/flags/program_name_test";
36 const std::string expect_basename = "program_name_test";
37 #else
38 // For targets that generate javascript or webassembly the invocation name
39 // has the special value below.
40 const std::string expect_name = "this.program";
41 const std::string expect_basename = "this.program";
42 #endif
43
44 EXPECT_TRUE(absl::EndsWith(program_name, expect_name)) << program_name;
45 EXPECT_EQ(flags::ShortProgramInvocationName(), expect_basename);
46 }
47
TEST(FlagsPathUtilTest,TestProgamNameInterfaces)48 TEST(FlagsPathUtilTest, TestProgamNameInterfaces) {
49 flags::SetProgramInvocationName("a/my_test");
50
51 EXPECT_EQ(flags::ProgramInvocationName(), "a/my_test");
52 EXPECT_EQ(flags::ShortProgramInvocationName(), "my_test");
53
54 absl::string_view not_null_terminated("absl/aaa/bbb");
55 not_null_terminated = not_null_terminated.substr(1, 10);
56
57 flags::SetProgramInvocationName(not_null_terminated);
58
59 EXPECT_EQ(flags::ProgramInvocationName(), "bsl/aaa/bb");
60 EXPECT_EQ(flags::ShortProgramInvocationName(), "bb");
61 }
62
63 } // namespace
64