llvm-capstone/lldb/unittests/Utility/ArgsTest.cpp
Pavel Labath 145d95c964 Move Args.cpp from Interpreter to Utility
Summary:
The Args class is used in plenty of places besides the command
interpreter (e.g., anything requiring an argc+argv combo, such as when
launching a process), so it needs to be in a lower layer. Now that the
class has no external dependencies, it can be moved down to the Utility
module.

This removes the last (direct) dependency from the Host module to
Interpreter, so I remove the Interpreter module from Host's dependency
list.

Reviewers: zturner, jingham, davide

Subscribers: mgorny, lldb-commits

Differential Revision: https://reviews.llvm.org/D45480

llvm-svn: 330200
2018-04-17 18:53:35 +00:00

192 lines
5.8 KiB
C++

//===-- ArgsTest.cpp --------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "lldb/Utility/Args.h"
#include "lldb/Utility/StringList.h"
#include <limits>
#include <sstream>
using namespace lldb_private;
TEST(ArgsTest, TestSingleArg) {
Args args;
args.SetCommandString("arg");
EXPECT_EQ(1u, args.GetArgumentCount());
EXPECT_STREQ(args.GetArgumentAtIndex(0), "arg");
}
TEST(ArgsTest, TestSingleQuotedArgWithSpace) {
Args args;
args.SetCommandString("\"arg with space\"");
EXPECT_EQ(1u, args.GetArgumentCount());
EXPECT_STREQ(args.GetArgumentAtIndex(0), "arg with space");
}
TEST(ArgsTest, TestSingleArgWithQuotedSpace) {
Args args;
args.SetCommandString("arg\\ with\\ space");
EXPECT_EQ(1u, args.GetArgumentCount());
EXPECT_STREQ(args.GetArgumentAtIndex(0), "arg with space");
}
TEST(ArgsTest, TestMultipleArgs) {
Args args;
args.SetCommandString("this has multiple args");
EXPECT_EQ(4u, args.GetArgumentCount());
EXPECT_STREQ(args.GetArgumentAtIndex(0), "this");
EXPECT_STREQ(args.GetArgumentAtIndex(1), "has");
EXPECT_STREQ(args.GetArgumentAtIndex(2), "multiple");
EXPECT_STREQ(args.GetArgumentAtIndex(3), "args");
}
TEST(ArgsTest, TestOverwriteArgs) {
Args args;
args.SetCommandString("this has multiple args");
EXPECT_EQ(4u, args.GetArgumentCount());
args.SetCommandString("arg");
EXPECT_EQ(1u, args.GetArgumentCount());
EXPECT_STREQ(args.GetArgumentAtIndex(0), "arg");
}
TEST(ArgsTest, TestAppendArg) {
Args args;
args.SetCommandString("first_arg");
EXPECT_EQ(1u, args.GetArgumentCount());
args.AppendArgument(llvm::StringRef("second_arg"));
EXPECT_EQ(2u, args.GetArgumentCount());
EXPECT_STREQ(args.GetArgumentAtIndex(0), "first_arg");
EXPECT_STREQ(args.GetArgumentAtIndex(1), "second_arg");
}
TEST(ArgsTest, TestInsertArg) {
Args args;
args.AppendArgument("1");
args.AppendArgument("2");
args.AppendArgument("3");
args.InsertArgumentAtIndex(1, "1.5");
args.InsertArgumentAtIndex(4, "3.5");
ASSERT_EQ(5u, args.GetArgumentCount());
EXPECT_STREQ("1", args.GetArgumentAtIndex(0));
EXPECT_STREQ("1.5", args.GetArgumentAtIndex(1));
EXPECT_STREQ("2", args.GetArgumentAtIndex(2));
EXPECT_STREQ("3", args.GetArgumentAtIndex(3));
EXPECT_STREQ("3.5", args.GetArgumentAtIndex(4));
}
TEST(ArgsTest, TestArgv) {
Args args;
EXPECT_EQ(nullptr, args.GetArgumentVector());
args.AppendArgument("1");
EXPECT_NE(nullptr, args.GetArgumentVector()[0]);
EXPECT_EQ(nullptr, args.GetArgumentVector()[1]);
args.AppendArgument("2");
EXPECT_NE(nullptr, args.GetArgumentVector()[0]);
EXPECT_NE(nullptr, args.GetArgumentVector()[1]);
EXPECT_EQ(nullptr, args.GetArgumentVector()[2]);
args.AppendArgument("3");
EXPECT_NE(nullptr, args.GetArgumentVector()[0]);
EXPECT_NE(nullptr, args.GetArgumentVector()[1]);
EXPECT_NE(nullptr, args.GetArgumentVector()[2]);
EXPECT_EQ(nullptr, args.GetArgumentVector()[3]);
args.InsertArgumentAtIndex(1, "1.5");
EXPECT_NE(nullptr, args.GetArgumentVector()[0]);
EXPECT_NE(nullptr, args.GetArgumentVector()[1]);
EXPECT_NE(nullptr, args.GetArgumentVector()[2]);
EXPECT_NE(nullptr, args.GetArgumentVector()[3]);
EXPECT_EQ(nullptr, args.GetArgumentVector()[4]);
args.InsertArgumentAtIndex(4, "3.5");
EXPECT_NE(nullptr, args.GetArgumentVector()[0]);
EXPECT_NE(nullptr, args.GetArgumentVector()[1]);
EXPECT_NE(nullptr, args.GetArgumentVector()[2]);
EXPECT_NE(nullptr, args.GetArgumentVector()[3]);
EXPECT_NE(nullptr, args.GetArgumentVector()[4]);
EXPECT_EQ(nullptr, args.GetArgumentVector()[5]);
}
TEST(ArgsTest, StringListConstructor) {
StringList list;
list << "foo"
<< "bar"
<< "baz";
Args args(list);
ASSERT_EQ(3u, args.GetArgumentCount());
EXPECT_EQ("foo", args[0].ref);
EXPECT_EQ("bar", args[1].ref);
EXPECT_EQ("baz", args[2].ref);
}
TEST(ArgsTest, GetQuotedCommandString) {
Args args;
const char *str = "process launch -o stdout.txt -- \"a b c\"";
args.SetCommandString(str);
std::string stdstr;
ASSERT_TRUE(args.GetQuotedCommandString(stdstr));
EXPECT_EQ(str, stdstr);
}
TEST(ArgsTest, BareSingleQuote) {
Args args;
args.SetCommandString("a\\'b");
EXPECT_EQ(1u, args.GetArgumentCount());
EXPECT_STREQ("a'b", args.GetArgumentAtIndex(0));
}
TEST(ArgsTest, DoubleQuotedItem) {
Args args;
args.SetCommandString("\"a b c\"");
EXPECT_EQ(1u, args.GetArgumentCount());
EXPECT_STREQ("a b c", args.GetArgumentAtIndex(0));
}
TEST(ArgsTest, AppendArguments) {
Args args;
const char *argv[] = {"1", "2", nullptr};
const char *argv2[] = {"3", "4", nullptr};
args.AppendArguments(argv);
ASSERT_EQ(2u, args.GetArgumentCount());
EXPECT_STREQ("1", args.GetArgumentVector()[0]);
EXPECT_STREQ("2", args.GetArgumentVector()[1]);
EXPECT_EQ(nullptr, args.GetArgumentVector()[2]);
EXPECT_STREQ("1", args.GetArgumentAtIndex(0));
EXPECT_STREQ("2", args.GetArgumentAtIndex(1));
args.AppendArguments(argv2);
ASSERT_EQ(4u, args.GetArgumentCount());
EXPECT_STREQ("1", args.GetArgumentVector()[0]);
EXPECT_STREQ("2", args.GetArgumentVector()[1]);
EXPECT_STREQ("3", args.GetArgumentVector()[2]);
EXPECT_STREQ("4", args.GetArgumentVector()[3]);
EXPECT_EQ(nullptr, args.GetArgumentVector()[4]);
EXPECT_STREQ("1", args.GetArgumentAtIndex(0));
EXPECT_STREQ("2", args.GetArgumentAtIndex(1));
EXPECT_STREQ("3", args.GetArgumentAtIndex(2));
EXPECT_STREQ("4", args.GetArgumentAtIndex(3));
}
TEST(ArgsTest, GetArgumentArrayRef) {
Args args("foo bar");
auto ref = args.GetArgumentArrayRef();
ASSERT_EQ(2u, ref.size());
EXPECT_STREQ("foo", ref[0]);
EXPECT_STREQ("bar", ref[1]);
}