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 11#include "testing/gtest/include/gtest/gtest.h" 12#include "webrtc/tools/simple_command_line_parser.h" 13 14namespace webrtc { 15namespace test { 16 17class CommandLineParserTest : public ::testing::Test { 18 protected: 19 void SetUp() override { 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 } 32 void TearDown() override { 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 45TEST_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 61TEST_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 67TEST_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 73TEST_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 79TEST_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 87