root/tools/gn/input_conversion_unittest.cc

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. TEST
  2. TEST
  3. TEST
  4. TEST
  5. TEST
  6. TEST
  7. TEST
  8. TEST

// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "testing/gtest/include/gtest/gtest.h"
#include "tools/gn/err.h"
#include "tools/gn/input_conversion.h"
#include "tools/gn/value.h"

TEST(InputConversion, String) {
  Err err;
  std::string input("\nfoo bar  \n");
  Value result = ConvertInputToValue(input, NULL, Value(NULL, "string"), &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::STRING, result.type());
  EXPECT_EQ(input, result.string_value());

  // Test with trimming.
  result = ConvertInputToValue(input, NULL, Value(NULL, "trim string"), &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::STRING, result.type());
  EXPECT_EQ("foo bar", result.string_value());
}

TEST(InputConversion, ListLines) {
  Err err;
  std::string input("\nfoo\nbar  \n\n");
  Value result = ConvertInputToValue(input, NULL, Value(NULL, "list lines"),
                                     &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::LIST, result.type());
  ASSERT_EQ(4u, result.list_value().size());
  EXPECT_EQ("",    result.list_value()[0].string_value());
  EXPECT_EQ("foo", result.list_value()[1].string_value());
  EXPECT_EQ("bar", result.list_value()[2].string_value());
  EXPECT_EQ("",    result.list_value()[3].string_value());

  // Test with trimming.
  result = ConvertInputToValue(input, NULL, Value(NULL, "trim list lines"),
                               &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::LIST, result.type());
  ASSERT_EQ(2u, result.list_value().size());
  EXPECT_EQ("foo", result.list_value()[0].string_value());
  EXPECT_EQ("bar", result.list_value()[1].string_value());
}

TEST(InputConversion, ValueString) {
  Err err;
  std::string input("\"str\"");
  Value result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::STRING, result.type());
  EXPECT_EQ("str", result.string_value());
}

TEST(InputConversion, ValueInt) {
  Err err;
  std::string input("\n\n  6 \n ");
  Value result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::INTEGER, result.type());
  EXPECT_EQ(6, result.int_value());
}

TEST(InputConversion, ValueList) {
  Err err;
  std::string input("\n [ \"a\", 5]");
  Value result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
  EXPECT_FALSE(err.has_error());
  ASSERT_EQ(Value::LIST, result.type());
  ASSERT_EQ(2u, result.list_value().size());
  EXPECT_EQ("a", result.list_value()[0].string_value());
  EXPECT_EQ(5,   result.list_value()[1].int_value());
}

TEST(InputConversion, ValueEmpty) {
  Err err;
  Value result = ConvertInputToValue("", NULL, Value(NULL, "value"), &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::NONE, result.type());
}

TEST(InputConversion, ValueError) {
  Err err;
  std::string input("\n [ \"a\", 5\nfoo bar");
  Value result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
  EXPECT_TRUE(err.has_error());

  // Blocks not allowed.
  input = "{ foo = 5 }";
  result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
  EXPECT_TRUE(err.has_error());

  // Function calls not allowed.
  input = "print(5)";
  result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
  EXPECT_TRUE(err.has_error());
}

// Passing none or the empty string for input conversion should ignore the
// result.
TEST(InputConversion, Ignore) {
  Err err;
  Value result = ConvertInputToValue("foo", NULL, Value(), &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::NONE, result.type());

  result = ConvertInputToValue("foo", NULL, Value(NULL, ""), &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::NONE, result.type());
}

/* [<][>][^][v][top][bottom][index][help] */