This source file includes following definitions.
- IsValueIntegerEqualing
- IsValueStringEqualing
- ListWithLiteral
- TEST
- TEST
#include "testing/gtest/include/gtest/gtest.h"
#include "tools/gn/operators.h"
#include "tools/gn/parse_tree.h"
#include "tools/gn/pattern.h"
#include "tools/gn/test_with_scope.h"
namespace {
bool IsValueIntegerEqualing(const Value& v, int64 i) {
if (v.type() != Value::INTEGER)
return false;
return v.int_value() == i;
}
bool IsValueStringEqualing(const Value& v, const char* s) {
if (v.type() != Value::STRING)
return false;
return v.string_value() == s;
}
scoped_ptr<ListNode> ListWithLiteral(const Token& token) {
scoped_ptr<ListNode> list(new ListNode);
list->append_item(scoped_ptr<ParseNode>(new LiteralNode(token)));
return list.Pass();
}
}
TEST(Operators, SourcesAppend) {
Err err;
TestWithScope setup;
const char sources[] = "sources";
setup.scope()->SetValue(sources, Value(NULL, Value::LIST), NULL);
BinaryOpNode node;
const char token_value[] = "+=";
Token op(Location(), Token::PLUS_EQUALS, token_value);
node.set_op(op);
Token identifier_token(Location(), Token::IDENTIFIER, sources);
node.set_left(scoped_ptr<ParseNode>(new IdentifierNode(identifier_token)));
scoped_ptr<PatternList> pattern_list(new PatternList);
pattern_list->Append(Pattern("*rm"));
setup.scope()->set_sources_assignment_filter(pattern_list.Pass());
const char integer_value[] = "5";
Token integer(Location(), Token::INTEGER, integer_value);
node.set_right(ListWithLiteral(integer).PassAs<ParseNode>());
node.Execute(setup.scope(), &err);
EXPECT_FALSE(err.has_error());
const char string_1_value[] = "\"good\"";
Token string_1(Location(), Token::STRING, string_1_value);
node.set_right(ListWithLiteral(string_1).PassAs<ParseNode>());
node.Execute(setup.scope(), &err);
EXPECT_FALSE(err.has_error());
const char string_2_value[] = "\"foo-rm\"";
Token string_2(Location(), Token::STRING, string_2_value);
node.set_right(ListWithLiteral(string_2).PassAs<ParseNode>());
node.Execute(setup.scope(), &err);
EXPECT_FALSE(err.has_error());
ListNode list;
list.append_item(scoped_ptr<ParseNode>(new LiteralNode(string_1)));
list.append_item(scoped_ptr<ParseNode>(new LiteralNode(string_2)));
ExecuteBinaryOperator(setup.scope(), &node, node.left(), &list, &err);
EXPECT_FALSE(err.has_error());
const Value* value = setup.scope()->GetValue(sources);
ASSERT_TRUE(value);
ASSERT_EQ(Value::LIST, value->type());
ASSERT_EQ(3u, value->list_value().size());
EXPECT_TRUE(IsValueIntegerEqualing(value->list_value()[0], 5));
EXPECT_TRUE(IsValueStringEqualing(value->list_value()[1], "good"));
EXPECT_TRUE(IsValueStringEqualing(value->list_value()[2], "good"));
}
TEST(Operators, ListAppend) {
Err err;
TestWithScope setup;
const char foo[] = "foo";
setup.scope()->SetValue(foo, Value(NULL, Value::LIST), NULL);
BinaryOpNode node;
const char token_value[] = "+=";
Token op(Location(), Token::PLUS_EQUALS, token_value);
node.set_op(op);
Token identifier_token(Location(), Token::IDENTIFIER, foo);
node.set_left(scoped_ptr<ParseNode>(new IdentifierNode(identifier_token)));
scoped_ptr<ListNode> outer_list(new ListNode);
const char twelve_str[] = "12";
Token twelve(Location(), Token::INTEGER, twelve_str);
outer_list->append_item(ListWithLiteral(twelve).PassAs<ParseNode>());
node.set_right(outer_list.PassAs<ParseNode>());
Value ret = ExecuteBinaryOperator(setup.scope(), &node, node.left(),
node.right(), &err);
EXPECT_FALSE(err.has_error());
EXPECT_EQ(Value::NONE, ret.type());
Value result = *setup.scope()->GetValue(foo);
ASSERT_EQ(Value::LIST, result.type());
ASSERT_EQ(1u, result.list_value().size());
ASSERT_EQ(Value::LIST, result.list_value()[0].type());
ASSERT_EQ(1u, result.list_value()[0].list_value().size());
ASSERT_EQ(Value::INTEGER, result.list_value()[0].list_value()[0].type());
ASSERT_EQ(12, result.list_value()[0].list_value()[0].int_value());
const char str_str[] = "\"hi\"";
Token str(Location(), Token::STRING, str_str);
node.set_right(scoped_ptr<ParseNode>(new LiteralNode(str)));
ExecuteBinaryOperator(setup.scope(), &node, node.left(), node.right(), &err);
EXPECT_TRUE(err.has_error());
err = Err();
node.set_right(scoped_ptr<ParseNode>(new LiteralNode(twelve)));
ExecuteBinaryOperator(setup.scope(), &node, node.left(), node.right(), &err);
EXPECT_TRUE(err.has_error());
}