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/value.h"
10 TEST(InputConversion
, String
) {
12 std::string
input("\nfoo bar \n");
13 Value result
= ConvertInputToValue(input
, NULL
, Value(NULL
, "string"), &err
);
14 EXPECT_FALSE(err
.has_error());
15 EXPECT_EQ(Value::STRING
, result
.type());
16 EXPECT_EQ(input
, result
.string_value());
18 // Test with trimming.
19 result
= ConvertInputToValue(input
, NULL
, Value(NULL
, "trim string"), &err
);
20 EXPECT_FALSE(err
.has_error());
21 EXPECT_EQ(Value::STRING
, result
.type());
22 EXPECT_EQ("foo bar", result
.string_value());
25 TEST(InputConversion
, ListLines
) {
27 std::string
input("\nfoo\nbar \n\n");
28 Value result
= ConvertInputToValue(input
, NULL
, Value(NULL
, "list lines"),
30 EXPECT_FALSE(err
.has_error());
31 EXPECT_EQ(Value::LIST
, result
.type());
32 ASSERT_EQ(4u, result
.list_value().size());
33 EXPECT_EQ("", result
.list_value()[0].string_value());
34 EXPECT_EQ("foo", result
.list_value()[1].string_value());
35 EXPECT_EQ("bar", result
.list_value()[2].string_value());
36 EXPECT_EQ("", result
.list_value()[3].string_value());
38 // Test with trimming.
39 result
= ConvertInputToValue(input
, NULL
, Value(NULL
, "trim list lines"),
41 EXPECT_FALSE(err
.has_error());
42 EXPECT_EQ(Value::LIST
, result
.type());
43 ASSERT_EQ(2u, result
.list_value().size());
44 EXPECT_EQ("foo", result
.list_value()[0].string_value());
45 EXPECT_EQ("bar", result
.list_value()[1].string_value());
48 TEST(InputConversion
, ValueString
) {
50 std::string
input("\"str\"");
51 Value result
= ConvertInputToValue(input
, NULL
, Value(NULL
, "value"), &err
);
52 EXPECT_FALSE(err
.has_error());
53 EXPECT_EQ(Value::STRING
, result
.type());
54 EXPECT_EQ("str", result
.string_value());
57 TEST(InputConversion
, ValueInt
) {
59 std::string
input("\n\n 6 \n ");
60 Value result
= ConvertInputToValue(input
, NULL
, Value(NULL
, "value"), &err
);
61 EXPECT_FALSE(err
.has_error());
62 EXPECT_EQ(Value::INTEGER
, result
.type());
63 EXPECT_EQ(6, result
.int_value());
66 TEST(InputConversion
, ValueList
) {
68 std::string
input("\n [ \"a\", 5]");
69 Value result
= ConvertInputToValue(input
, NULL
, Value(NULL
, "value"), &err
);
70 EXPECT_FALSE(err
.has_error());
71 ASSERT_EQ(Value::LIST
, result
.type());
72 ASSERT_EQ(2u, result
.list_value().size());
73 EXPECT_EQ("a", result
.list_value()[0].string_value());
74 EXPECT_EQ(5, result
.list_value()[1].int_value());
77 TEST(InputConversion
, ValueEmpty
) {
79 Value result
= ConvertInputToValue("", NULL
, Value(NULL
, "value"), &err
);
80 EXPECT_FALSE(err
.has_error());
81 EXPECT_EQ(Value::NONE
, result
.type());
84 TEST(InputConversion
, ValueError
) {
86 std::string
input("\n [ \"a\", 5\nfoo bar");
87 Value result
= ConvertInputToValue(input
, NULL
, Value(NULL
, "value"), &err
);
88 EXPECT_TRUE(err
.has_error());
90 // Blocks not allowed.
91 input
= "{ foo = 5 }";
92 result
= ConvertInputToValue(input
, NULL
, Value(NULL
, "value"), &err
);
93 EXPECT_TRUE(err
.has_error());
95 // Function calls not allowed.
97 result
= ConvertInputToValue(input
, NULL
, Value(NULL
, "value"), &err
);
98 EXPECT_TRUE(err
.has_error());
101 // Passing none or the empty string for input conversion should ignore the
103 TEST(InputConversion
, Ignore
) {
105 Value result
= ConvertInputToValue("foo", NULL
, Value(), &err
);
106 EXPECT_FALSE(err
.has_error());
107 EXPECT_EQ(Value::NONE
, result
.type());
109 result
= ConvertInputToValue("foo", NULL
, Value(NULL
, ""), &err
);
110 EXPECT_FALSE(err
.has_error());
111 EXPECT_EQ(Value::NONE
, result
.type());