summaryrefslogtreecommitdiffstats
path: root/tools/gn/input_conversion_unittest.cc
blob: 59c012344c9cb06f0898602749db8943d3e57358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 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(InputConversion, ListLines) {
  Err err;
  std::string input("\nfoo\nbar  \n");
  Value result = ConvertInputToValue(input, NULL, Value(NULL, "list lines"),
                                     &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::LIST, result.type());
  ASSERT_EQ(3u, 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());
}

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;
  ConvertInputToValue("", NULL, Value(NULL, "value"), &err);
}

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());
}