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,TestProgamNameInterfaces)28 TEST(FlagsPathUtilTest, TestProgamNameInterfaces) {
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   flags::SetProgramInvocationName("a/my_test");
48 
49   EXPECT_EQ(flags::ProgramInvocationName(), "a/my_test");
50   EXPECT_EQ(flags::ShortProgramInvocationName(), "my_test");
51 
52   absl::string_view not_null_terminated("absl/aaa/bbb");
53   not_null_terminated = not_null_terminated.substr(1, 10);
54 
55   flags::SetProgramInvocationName(not_null_terminated);
56 
57   EXPECT_EQ(flags::ProgramInvocationName(), "bsl/aaa/bb");
58   EXPECT_EQ(flags::ShortProgramInvocationName(), "bb");
59 }
60 
61 }  // namespace
62