program_name_test.cc 2.0 KB

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