Home | History | Annotate | Download | only in gn
      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/value.h"
      9 
     10 TEST(InputConversion, String) {
     11   Err err;
     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());
     17 }
     18 
     19 TEST(InputConversion, ListLines) {
     20   Err err;
     21   std::string input("\nfoo\nbar  \n");
     22   Value result = ConvertInputToValue(input, NULL, Value(NULL, "list lines"),
     23                                      &err);
     24   EXPECT_FALSE(err.has_error());
     25   EXPECT_EQ(Value::LIST, result.type());
     26   ASSERT_EQ(3u, result.list_value().size());
     27   EXPECT_EQ("",    result.list_value()[0].string_value());
     28   EXPECT_EQ("foo", result.list_value()[1].string_value());
     29   EXPECT_EQ("bar", result.list_value()[2].string_value());
     30 }
     31 
     32 TEST(InputConversion, ValueString) {
     33   Err err;
     34   std::string input("\"str\"");
     35   Value result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
     36   EXPECT_FALSE(err.has_error());
     37   EXPECT_EQ(Value::STRING, result.type());
     38   EXPECT_EQ("str", result.string_value());
     39 }
     40 
     41 TEST(InputConversion, ValueInt) {
     42   Err err;
     43   std::string input("\n\n  6 \n ");
     44   Value result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
     45   EXPECT_FALSE(err.has_error());
     46   EXPECT_EQ(Value::INTEGER, result.type());
     47   EXPECT_EQ(6, result.int_value());
     48 }
     49 
     50 TEST(InputConversion, ValueList) {
     51   Err err;
     52   std::string input("\n [ \"a\", 5]");
     53   Value result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
     54   EXPECT_FALSE(err.has_error());
     55   ASSERT_EQ(Value::LIST, result.type());
     56   ASSERT_EQ(2u, result.list_value().size());
     57   EXPECT_EQ("a", result.list_value()[0].string_value());
     58   EXPECT_EQ(5,   result.list_value()[1].int_value());
     59 }
     60 
     61 TEST(InputConversion, ValueEmpty) {
     62   Err err;
     63   ConvertInputToValue("", NULL, Value(NULL, "value"), &err);
     64 }
     65 
     66 TEST(InputConversion, ValueError) {
     67   Err err;
     68   std::string input("\n [ \"a\", 5\nfoo bar");
     69   Value result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
     70   EXPECT_TRUE(err.has_error());
     71 
     72   // Blocks not allowed.
     73   input = "{ foo = 5 }";
     74   result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
     75   EXPECT_TRUE(err.has_error());
     76 
     77   // Function calls not allowed.
     78   input = "print(5)";
     79   result = ConvertInputToValue(input, NULL, Value(NULL, "value"), &err);
     80   EXPECT_TRUE(err.has_error());
     81 }
     82