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. 4 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" 13 14 namespace { 15 16 // InputConversion needs a global scheduler object. 17 class InputConversionTest : public testing::Test { 18 public: 19 InputConversionTest() {} 20 21 const Settings* settings() { return setup_.settings(); } 22 23 private: 24 TestWithScope setup_; 25 26 Scheduler scheduler_; 27 }; 28 29 } // namespace 30 31 TEST_F(InputConversionTest, String) { 32 Err err; 33 std::string input("\nfoo bar \n"); 34 Value result = ConvertInputToValue(settings(), input, NULL, 35 Value(NULL, "string"), &err); 36 EXPECT_FALSE(err.has_error()); 37 EXPECT_EQ(Value::STRING, result.type()); 38 EXPECT_EQ(input, result.string_value()); 39 40 // Test with trimming. 41 result = ConvertInputToValue(settings(), input, NULL, 42 Value(NULL, "trim string"), &err); 43 EXPECT_FALSE(err.has_error()); 44 EXPECT_EQ(Value::STRING, result.type()); 45 EXPECT_EQ("foo bar", result.string_value()); 46 } 47 48 TEST_F(InputConversionTest, ListLines) { 49 Err err; 50 std::string input("\nfoo\nbar \n\n"); 51 Value result = ConvertInputToValue(settings(), input, NULL, 52 Value(NULL, "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()); 60 61 // Test with trimming. 62 result = ConvertInputToValue(settings(), input, NULL, 63 Value(NULL, "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()); 69 } 70 71 TEST_F(InputConversionTest, ValueString) { 72 Err err; 73 std::string input("\"str\""); 74 Value result = ConvertInputToValue(settings(), input, NULL, 75 Value(NULL, "value"), &err); 76 EXPECT_FALSE(err.has_error()); 77 EXPECT_EQ(Value::STRING, result.type()); 78 EXPECT_EQ("str", result.string_value()); 79 } 80 81 TEST_F(InputConversionTest, ValueInt) { 82 Err err; 83 std::string input("\n\n 6 \n "); 84 Value result = ConvertInputToValue(settings(), input, NULL, 85 Value(NULL, "value"), &err); 86 EXPECT_FALSE(err.has_error()); 87 EXPECT_EQ(Value::INTEGER, result.type()); 88 EXPECT_EQ(6, result.int_value()); 89 } 90 91 TEST_F(InputConversionTest, ValueList) { 92 Err err; 93 std::string input("\n [ \"a\", 5]"); 94 Value result = ConvertInputToValue(settings(), input, NULL, 95 Value(NULL, "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()); 101 } 102 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, NULL, 107 Value(NULL, "scope"), &err); 108 EXPECT_FALSE(err.has_error()); 109 ASSERT_EQ(Value::SCOPE, result.type()); 110 111 const Value* a_value = result.scope_value()->GetValue("a"); 112 ASSERT_TRUE(a_value); 113 EXPECT_EQ(5, a_value->int_value()); 114 115 const Value* b_value = result.scope_value()->GetValue("b"); 116 ASSERT_TRUE(b_value); 117 EXPECT_EQ("foo", b_value->string_value()); 118 119 const Value* c_value = result.scope_value()->GetValue("c"); 120 ASSERT_TRUE(c_value); 121 EXPECT_EQ(7, c_value->int_value()); 122 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()); 130 131 const InputFile* a_file = a_range.begin().file(); 132 EXPECT_EQ(input, a_file->contents()); 133 } 134 135 TEST_F(InputConversionTest, ValueEmpty) { 136 Err err; 137 Value result = ConvertInputToValue(settings(), "", NULL, 138 Value(NULL, "value"), &err); 139 EXPECT_FALSE(err.has_error()); 140 EXPECT_EQ(Value::NONE, result.type()); 141 } 142 143 TEST_F(InputConversionTest, ValueError) { 144 Err err; 145 std::string input("\n [ \"a\", 5\nfoo bar"); 146 Value result = ConvertInputToValue(settings(), input, NULL, 147 Value(NULL, "value"), &err); 148 EXPECT_TRUE(err.has_error()); 149 150 // Blocks not allowed. 151 input = "{ foo = 5 }"; 152 result = ConvertInputToValue(settings(), input, NULL, 153 Value(NULL, "value"), &err); 154 EXPECT_TRUE(err.has_error()); 155 156 // Function calls not allowed. 157 input = "print(5)"; 158 result = ConvertInputToValue(settings(), input, NULL, 159 Value(NULL, "value"), &err); 160 EXPECT_TRUE(err.has_error()); 161 } 162 163 // Passing none or the empty string for input conversion should ignore the 164 // result. 165 TEST_F(InputConversionTest, Ignore) { 166 Err err; 167 Value result = ConvertInputToValue(settings(), "foo", NULL, Value(), &err); 168 EXPECT_FALSE(err.has_error()); 169 EXPECT_EQ(Value::NONE, result.type()); 170 171 result = ConvertInputToValue(settings(), "foo", NULL, Value(NULL, ""), &err); 172 EXPECT_FALSE(err.has_error()); 173 EXPECT_EQ(Value::NONE, result.type()); 174 } 175