00001 // 00002 // Copyright 2019 The Abseil Authors. 00003 // 00004 // Licensed under the Apache License, Version 2.0 (the "License"); 00005 // you may not use this file except in compliance with the License. 00006 // You may obtain a copy of the License at 00007 // 00008 // https://www.apache.org/licenses/LICENSE-2.0 00009 // 00010 // Unless required by applicable law or agreed to in writing, software 00011 // distributed under the License is distributed on an "AS IS" BASIS, 00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 // See the License for the specific language governing permissions and 00014 // limitations under the License. 00015 00016 #include "absl/flags/internal/program_name.h" 00017 00018 #include "gtest/gtest.h" 00019 #include "absl/strings/match.h" 00020 00021 namespace { 00022 00023 namespace flags = absl::flags_internal; 00024 00025 TEST(FlagsPathUtilTest, TestInitialProgamName) { 00026 flags::SetProgramInvocationName("absl/flags/program_name_test"); 00027 std::string program_name = flags::ProgramInvocationName(); 00028 for (char& c : program_name) 00029 if (c == '\\') c = '/'; 00030 00031 #if !defined(__wasm__) && !defined(__asmjs__) 00032 const std::string expect_name = "absl/flags/program_name_test"; 00033 const std::string expect_basename = "program_name_test"; 00034 #else 00035 // For targets that generate javascript or webassembly the invocation name 00036 // has the special value below. 00037 const std::string expect_name = "this.program"; 00038 const std::string expect_basename = "this.program"; 00039 #endif 00040 00041 EXPECT_TRUE(absl::EndsWith(program_name, expect_name)) << program_name; 00042 EXPECT_EQ(flags::ShortProgramInvocationName(), expect_basename); 00043 } 00044 00045 TEST(FlagsPathUtilTest, TestProgamNameInterfaces) { 00046 flags::SetProgramInvocationName("a/my_test"); 00047 00048 EXPECT_EQ(flags::ProgramInvocationName(), "a/my_test"); 00049 EXPECT_EQ(flags::ShortProgramInvocationName(), "my_test"); 00050 00051 absl::string_view not_null_terminated("absl/aaa/bbb"); 00052 not_null_terminated = not_null_terminated.substr(1, 10); 00053 00054 flags::SetProgramInvocationName(not_null_terminated); 00055 00056 EXPECT_EQ(flags::ProgramInvocationName(), "bsl/aaa/bb"); 00057 EXPECT_EQ(flags::ShortProgramInvocationName(), "bb"); 00058 } 00059 00060 } // namespace