blob: 5dfe05c4013af4939d8f15f8d1faacd51185a642 [file] [log] [blame]
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_tools/simple_command_line_parser.h"
Yves Gerey3e707812018-11-28 16:47:49 +010012
13#include <string.h>
14
Yves Gerey665174f2018-06-19 15:03:05 +020015#include "test/gtest.h"
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000016
17namespace webrtc {
18namespace test {
19
20class CommandLineParserTest : public ::testing::Test {
21 protected:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000022 void SetUp() override {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000023 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.org14665ff2015-03-04 12:58:35 +000035 void TearDown() override {
kjellander@webrtc.orgb2d74972013-01-26 16:36:40 +000036 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
48TEST_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
64TEST_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
70TEST_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
76TEST_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
82TEST_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