summaryrefslogtreecommitdiffstats
path: root/tools/gn/input_conversion_unittest.cc
blob: aff66674172f37dda1bf5e34331e4e94850ee07b (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// 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/input_file.h"
#include "tools/gn/parse_tree.h"
#include "tools/gn/scheduler.h"
#include "tools/gn/test_with_scope.h"
#include "tools/gn/value.h"

namespace {

// InputConversion needs a global scheduler object.
class InputConversionTest : public testing::Test {
 public:
  InputConversionTest() {}

  const Settings* settings() { return setup_.settings(); }

 private:
  TestWithScope setup_;

  Scheduler scheduler_;
};

}  // namespace

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

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

TEST_F(InputConversionTest, ListLines) {
  Err err;
  std::string input("\nfoo\nbar  \n\n");
  Value result = ConvertInputToValue(settings(), input, nullptr,
                                     Value(nullptr, "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(settings(), input, nullptr,
                               Value(nullptr, "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_F(InputConversionTest, ValueString) {
  Err err;
  std::string input("\"str\"");
  Value result = ConvertInputToValue(settings(), input, nullptr,
                                     Value(nullptr, "value"), &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::STRING, result.type());
  EXPECT_EQ("str", result.string_value());
}

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

TEST_F(InputConversionTest, ValueList) {
  Err err;
  std::string input("\n [ \"a\", 5]");
  Value result = ConvertInputToValue(settings(), input, nullptr,
                                     Value(nullptr, "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_F(InputConversionTest, ValueDict) {
  Err err;
  std::string input("\n a = 5 b = \"foo\" c = a + 2");
  Value result = ConvertInputToValue(settings(), input, nullptr,
                                     Value(nullptr, "scope"), &err);
  EXPECT_FALSE(err.has_error());
  ASSERT_EQ(Value::SCOPE, result.type());

  const Value* a_value = result.scope_value()->GetValue("a");
  ASSERT_TRUE(a_value);
  EXPECT_EQ(5, a_value->int_value());

  const Value* b_value = result.scope_value()->GetValue("b");
  ASSERT_TRUE(b_value);
  EXPECT_EQ("foo", b_value->string_value());

  const Value* c_value = result.scope_value()->GetValue("c");
  ASSERT_TRUE(c_value);
  EXPECT_EQ(7, c_value->int_value());

  // Tests that when we get Values out of the input conversion, the resulting
  // values have an origin set to something corresponding to the input.
  const ParseNode* a_origin = a_value->origin();
  ASSERT_TRUE(a_origin);
  LocationRange a_range = a_origin->GetRange();
  EXPECT_EQ(2, a_range.begin().line_number());
  EXPECT_EQ(6, a_range.begin().char_offset());

  const InputFile* a_file = a_range.begin().file();
  EXPECT_EQ(input, a_file->contents());
}

TEST_F(InputConversionTest, ValueEmpty) {
  Err err;
  Value result = ConvertInputToValue(settings(), "", nullptr,
                                     Value(nullptr, "value"), &err);
  EXPECT_FALSE(err.has_error());
  EXPECT_EQ(Value::NONE, result.type());
}

TEST_F(InputConversionTest, ValueError) {
  static const char* const kTests[] = {
      "\n [ \"a\", 5\nfoo bar",

      // Blocks not allowed.
      "{ foo = 5 }",

      // Function calls not allowed.
      "print(5)",

      // Trailing junk not allowed.
      "233105-1",

      // Non-literals hidden in arrays are not allowed.
      "[233105 - 1]",
      "[rebase_path(\"//\")]",
  };

  for (auto test : kTests) {
    Err err;
    std::string input(test);
    Value result = ConvertInputToValue(settings(), input, nullptr,
                                       Value(nullptr, "value"), &err);
    EXPECT_TRUE(err.has_error()) << test;
  }
}

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

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