kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 | [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 04:47:31 | [diff] [blame] | 11 | #include "rtc_tools/simple_command_line_parser.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
| 14 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 15 | #include "test/gtest.h" |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | namespace test { |
| 19 | |
| 20 | class CommandLineParserTest : public ::testing::Test { |
| 21 | protected: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 22 | void SetUp() override { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 | [diff] [blame] | 23 | parser_ = new CommandLineParser(); |
| 24 | |
| 25 | test_flags_length_ = 3; |
| 26 | int flag_size = 32; |
| 27 | test_flags_ = new char*[test_flags_length_]; |
| 28 | for (int i = 0; i < test_flags_length_; ++i) { |
| 29 | test_flags_[i] = new char[flag_size]; |
| 30 | } |
| 31 | strncpy(test_flags_[0], "tools_unittest", flag_size); |
| 32 | strncpy(test_flags_[1], "--foo", flag_size); |
| 33 | strncpy(test_flags_[2], "--bar=1", flag_size); |
| 34 | } |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 35 | void TearDown() override { |
kjellander@webrtc.org | b2d7497 | 2013-01-26 16:36:40 | [diff] [blame] | 36 | for (int i = 0; i < test_flags_length_; ++i) { |
| 37 | delete[] test_flags_[i]; |
| 38 | } |
| 39 | delete[] test_flags_; |
| 40 | delete parser_; |
| 41 | } |
| 42 | CommandLineParser* parser_; |
| 43 | // Test flags to emulate a program's argv arguments. |
| 44 | char** test_flags_; |
| 45 | int test_flags_length_; |
| 46 | }; |
| 47 | |
| 48 | TEST_F(CommandLineParserTest, ProcessFlags) { |
| 49 | // Setup supported flags to parse. |
| 50 | parser_->SetFlag("foo", "false"); |
| 51 | parser_->SetFlag("foo-foo", "false"); // To test boolean flags defaults. |
| 52 | parser_->SetFlag("bar", "222"); |
| 53 | parser_->SetFlag("baz", "333"); // To test the default value functionality. |
| 54 | |
| 55 | parser_->Init(test_flags_length_, test_flags_); |
| 56 | parser_->ProcessFlags(); |
| 57 | EXPECT_EQ("true", parser_->GetFlag("foo")); |
| 58 | EXPECT_EQ("false", parser_->GetFlag("foo-foo")); |
| 59 | EXPECT_EQ("1", parser_->GetFlag("bar")); |
| 60 | EXPECT_EQ("333", parser_->GetFlag("baz")); |
| 61 | EXPECT_EQ("", parser_->GetFlag("unknown")); |
| 62 | } |
| 63 | |
| 64 | TEST_F(CommandLineParserTest, IsStandaloneFlag) { |
| 65 | EXPECT_TRUE(parser_->IsStandaloneFlag("--foo")); |
| 66 | EXPECT_TRUE(parser_->IsStandaloneFlag("--foo-foo")); |
| 67 | EXPECT_FALSE(parser_->IsStandaloneFlag("--foo=1")); |
| 68 | } |
| 69 | |
| 70 | TEST_F(CommandLineParserTest, IsFlagWellFormed) { |
| 71 | EXPECT_TRUE(parser_->IsFlagWellFormed("--foo")); |
| 72 | EXPECT_TRUE(parser_->IsFlagWellFormed("--foo-foo")); |
| 73 | EXPECT_TRUE(parser_->IsFlagWellFormed("--bar=1")); |
| 74 | } |
| 75 | |
| 76 | TEST_F(CommandLineParserTest, GetCommandLineFlagName) { |
| 77 | EXPECT_EQ("foo", parser_->GetCommandLineFlagName("--foo")); |
| 78 | EXPECT_EQ("foo-foo", parser_->GetCommandLineFlagName("--foo-foo")); |
| 79 | EXPECT_EQ("bar", parser_->GetCommandLineFlagName("--bar=1")); |
| 80 | } |
| 81 | |
| 82 | TEST_F(CommandLineParserTest, GetCommandLineFlagValue) { |
| 83 | EXPECT_EQ("", parser_->GetCommandLineFlagValue("--foo")); |
| 84 | EXPECT_EQ("", parser_->GetCommandLineFlagValue("--foo-foo")); |
| 85 | EXPECT_EQ("1", parser_->GetCommandLineFlagValue("--bar=1")); |
| 86 | } |
| 87 | |
| 88 | } // namespace test |
| 89 | } // namespace webrtc |