Add ICU message format support
[chromium-blink-merge.git] / tools / gn / input_conversion_unittest.cc
blobaff66674172f37dda1bf5e34331e4e94850ee07b
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/err.h"
7 #include "tools/gn/input_conversion.h"
8 #include "tools/gn/input_file.h"
9 #include "tools/gn/parse_tree.h"
10 #include "tools/gn/scheduler.h"
11 #include "tools/gn/test_with_scope.h"
12 #include "tools/gn/value.h"
14 namespace {
16 // InputConversion needs a global scheduler object.
17 class InputConversionTest : public testing::Test {
18 public:
19 InputConversionTest() {}
21 const Settings* settings() { return setup_.settings(); }
23 private:
24 TestWithScope setup_;
26 Scheduler scheduler_;
29 } // namespace
31 TEST_F(InputConversionTest, String) {
32 Err err;
33 std::string input("\nfoo bar \n");
34 Value result = ConvertInputToValue(settings(), input, nullptr,
35 Value(nullptr, "string"), &err);
36 EXPECT_FALSE(err.has_error());
37 EXPECT_EQ(Value::STRING, result.type());
38 EXPECT_EQ(input, result.string_value());
40 // Test with trimming.
41 result = ConvertInputToValue(settings(), input, nullptr,
42 Value(nullptr, "trim string"), &err);
43 EXPECT_FALSE(err.has_error());
44 EXPECT_EQ(Value::STRING, result.type());
45 EXPECT_EQ("foo bar", result.string_value());
48 TEST_F(InputConversionTest, ListLines) {
49 Err err;
50 std::string input("\nfoo\nbar \n\n");
51 Value result = ConvertInputToValue(settings(), input, nullptr,
52 Value(nullptr, "list lines"), &err);
53 EXPECT_FALSE(err.has_error());
54 EXPECT_EQ(Value::LIST, result.type());
55 ASSERT_EQ(4u, result.list_value().size());
56 EXPECT_EQ("", result.list_value()[0].string_value());
57 EXPECT_EQ("foo", result.list_value()[1].string_value());
58 EXPECT_EQ("bar", result.list_value()[2].string_value());
59 EXPECT_EQ("", result.list_value()[3].string_value());
61 // Test with trimming.
62 result = ConvertInputToValue(settings(), input, nullptr,
63 Value(nullptr, "trim list lines"), &err);
64 EXPECT_FALSE(err.has_error());
65 EXPECT_EQ(Value::LIST, result.type());
66 ASSERT_EQ(2u, result.list_value().size());
67 EXPECT_EQ("foo", result.list_value()[0].string_value());
68 EXPECT_EQ("bar", result.list_value()[1].string_value());
71 TEST_F(InputConversionTest, ValueString) {
72 Err err;
73 std::string input("\"str\"");
74 Value result = ConvertInputToValue(settings(), input, nullptr,
75 Value(nullptr, "value"), &err);
76 EXPECT_FALSE(err.has_error());
77 EXPECT_EQ(Value::STRING, result.type());
78 EXPECT_EQ("str", result.string_value());
81 TEST_F(InputConversionTest, ValueInt) {
82 Err err;
83 std::string input("\n\n 6 \n ");
84 Value result = ConvertInputToValue(settings(), input, nullptr,
85 Value(nullptr, "value"), &err);
86 EXPECT_FALSE(err.has_error());
87 EXPECT_EQ(Value::INTEGER, result.type());
88 EXPECT_EQ(6, result.int_value());
91 TEST_F(InputConversionTest, ValueList) {
92 Err err;
93 std::string input("\n [ \"a\", 5]");
94 Value result = ConvertInputToValue(settings(), input, nullptr,
95 Value(nullptr, "value"), &err);
96 EXPECT_FALSE(err.has_error());
97 ASSERT_EQ(Value::LIST, result.type());
98 ASSERT_EQ(2u, result.list_value().size());
99 EXPECT_EQ("a", result.list_value()[0].string_value());
100 EXPECT_EQ(5, result.list_value()[1].int_value());
103 TEST_F(InputConversionTest, ValueDict) {
104 Err err;
105 std::string input("\n a = 5 b = \"foo\" c = a + 2");
106 Value result = ConvertInputToValue(settings(), input, nullptr,
107 Value(nullptr, "scope"), &err);
108 EXPECT_FALSE(err.has_error());
109 ASSERT_EQ(Value::SCOPE, result.type());
111 const Value* a_value = result.scope_value()->GetValue("a");
112 ASSERT_TRUE(a_value);
113 EXPECT_EQ(5, a_value->int_value());
115 const Value* b_value = result.scope_value()->GetValue("b");
116 ASSERT_TRUE(b_value);
117 EXPECT_EQ("foo", b_value->string_value());
119 const Value* c_value = result.scope_value()->GetValue("c");
120 ASSERT_TRUE(c_value);
121 EXPECT_EQ(7, c_value->int_value());
123 // Tests that when we get Values out of the input conversion, the resulting
124 // values have an origin set to something corresponding to the input.
125 const ParseNode* a_origin = a_value->origin();
126 ASSERT_TRUE(a_origin);
127 LocationRange a_range = a_origin->GetRange();
128 EXPECT_EQ(2, a_range.begin().line_number());
129 EXPECT_EQ(6, a_range.begin().char_offset());
131 const InputFile* a_file = a_range.begin().file();
132 EXPECT_EQ(input, a_file->contents());
135 TEST_F(InputConversionTest, ValueEmpty) {
136 Err err;
137 Value result = ConvertInputToValue(settings(), "", nullptr,
138 Value(nullptr, "value"), &err);
139 EXPECT_FALSE(err.has_error());
140 EXPECT_EQ(Value::NONE, result.type());
143 TEST_F(InputConversionTest, ValueError) {
144 static const char* const kTests[] = {
145 "\n [ \"a\", 5\nfoo bar",
147 // Blocks not allowed.
148 "{ foo = 5 }",
150 // Function calls not allowed.
151 "print(5)",
153 // Trailing junk not allowed.
154 "233105-1",
156 // Non-literals hidden in arrays are not allowed.
157 "[233105 - 1]",
158 "[rebase_path(\"//\")]",
161 for (auto test : kTests) {
162 Err err;
163 std::string input(test);
164 Value result = ConvertInputToValue(settings(), input, nullptr,
165 Value(nullptr, "value"), &err);
166 EXPECT_TRUE(err.has_error()) << test;
170 // Passing none or the empty string for input conversion should ignore the
171 // result.
172 TEST_F(InputConversionTest, Ignore) {
173 Err err;
174 Value result = ConvertInputToValue(settings(), "foo", nullptr, Value(), &err);
175 EXPECT_FALSE(err.has_error());
176 EXPECT_EQ(Value::NONE, result.type());
178 result =
179 ConvertInputToValue(settings(), "foo", nullptr, Value(nullptr, ""), &err);
180 EXPECT_FALSE(err.has_error());
181 EXPECT_EQ(Value::NONE, result.type());