kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "test/gtest.h" |
| 12 | #include "rtc_tools/simple_command_line_parser.h" |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 13 | |
| 14 | namespace webrtc { |
| 15 | namespace test { |
| 16 | |
| 17 | class CommandLineParserTest : public ::testing::Test { |
| 18 | protected: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 19 | void SetUp() override { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 20 | parser_ = new CommandLineParser(); |
| 21 | |
| 22 | test_flags_length_ = 3; |
| 23 | int flag_size = 32; |
| 24 | test_flags_ = new char*[test_flags_length_]; |
| 25 | for (int i = 0; i < test_flags_length_; ++i) { |
| 26 | test_flags_[i] = new char[flag_size]; |
| 27 | } |
| 28 | strncpy(test_flags_[0], "tools_unittest", flag_size); |
| 29 | strncpy(test_flags_[1], "--foo", flag_size); |
| 30 | strncpy(test_flags_[2], "--bar=1", flag_size); |
| 31 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 32 | void TearDown() override { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 +0000 | [diff] [blame] | 33 | for (int i = 0; i < test_flags_length_; ++i) { |
| 34 | delete[] test_flags_[i]; |
| 35 | } |
| 36 | delete[] test_flags_; |
| 37 | delete parser_; |
| 38 | } |
| 39 | CommandLineParser* parser_; |
| 40 | // Test flags to emulate a program's argv arguments. |
| 41 | char** test_flags_; |
| 42 | int test_flags_length_; |
| 43 | }; |
| 44 | |
| 45 | TEST_F(CommandLineParserTest, ProcessFlags) { |
| 46 | // Setup supported flags to parse. |
| 47 | parser_->SetFlag("foo", "false"); |
| 48 | parser_->SetFlag("foo-foo", "false"); // To test boolean flags defaults. |
| 49 | parser_->SetFlag("bar", "222"); |
| 50 | parser_->SetFlag("baz", "333"); // To test the default value functionality. |
| 51 | |
| 52 | parser_->Init(test_flags_length_, test_flags_); |
| 53 | parser_->ProcessFlags(); |
| 54 | EXPECT_EQ("true", parser_->GetFlag("foo")); |
| 55 | EXPECT_EQ("false", parser_->GetFlag("foo-foo")); |
| 56 | EXPECT_EQ("1", parser_->GetFlag("bar")); |
| 57 | EXPECT_EQ("333", parser_->GetFlag("baz")); |
| 58 | EXPECT_EQ("", parser_->GetFlag("unknown")); |
| 59 | } |
| 60 | |
| 61 | TEST_F(CommandLineParserTest, IsStandaloneFlag) { |
| 62 | EXPECT_TRUE(parser_->IsStandaloneFlag("--foo")); |
| 63 | EXPECT_TRUE(parser_->IsStandaloneFlag("--foo-foo")); |
| 64 | EXPECT_FALSE(parser_->IsStandaloneFlag("--foo=1")); |
| 65 | } |
| 66 | |
| 67 | TEST_F(CommandLineParserTest, IsFlagWellFormed) { |
| 68 | EXPECT_TRUE(parser_->IsFlagWellFormed("--foo")); |
| 69 | EXPECT_TRUE(parser_->IsFlagWellFormed("--foo-foo")); |
| 70 | EXPECT_TRUE(parser_->IsFlagWellFormed("--bar=1")); |
| 71 | } |
| 72 | |
| 73 | TEST_F(CommandLineParserTest, GetCommandLineFlagName) { |
| 74 | EXPECT_EQ("foo", parser_->GetCommandLineFlagName("--foo")); |
| 75 | EXPECT_EQ("foo-foo", parser_->GetCommandLineFlagName("--foo-foo")); |
| 76 | EXPECT_EQ("bar", parser_->GetCommandLineFlagName("--bar=1")); |
| 77 | } |
| 78 | |
| 79 | TEST_F(CommandLineParserTest, GetCommandLineFlagValue) { |
| 80 | EXPECT_EQ("", parser_->GetCommandLineFlagValue("--foo")); |
| 81 | EXPECT_EQ("", parser_->GetCommandLineFlagValue("--foo-foo")); |
| 82 | EXPECT_EQ("1", parser_->GetCommandLineFlagValue("--bar=1")); |
| 83 | } |
| 84 | |
| 85 | } // namespace test |
| 86 | } // namespace webrtc |