| OLD | NEW | 
|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "tools/gn/functions.h" | 5 #include "tools/gn/functions.h" | 
| 6 | 6 | 
| 7 #include <utility> | 7 #include <utility> | 
| 8 | 8 | 
|  | 9 #include "base/memory/ptr_util.h" | 
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" | 
| 10 #include "tools/gn/parse_tree.h" | 11 #include "tools/gn/parse_tree.h" | 
| 11 #include "tools/gn/test_with_scope.h" | 12 #include "tools/gn/test_with_scope.h" | 
| 12 #include "tools/gn/value.h" | 13 #include "tools/gn/value.h" | 
| 13 | 14 | 
| 14 TEST(Functions, Defined) { | 15 TEST(Functions, Defined) { | 
| 15   TestWithScope setup; | 16   TestWithScope setup; | 
| 16 | 17 | 
| 17   FunctionCallNode function_call; | 18   FunctionCallNode function_call; | 
| 18   Err err; | 19   Err err; | 
| 19 | 20 | 
| 20   // Test an undefined identifier. | 21   // Test an undefined identifier. | 
| 21   Token undefined_token(Location(), Token::IDENTIFIER, "undef"); | 22   Token undefined_token(Location(), Token::IDENTIFIER, "undef"); | 
| 22   ListNode args_list_identifier_undefined; | 23   ListNode args_list_identifier_undefined; | 
| 23   args_list_identifier_undefined.append_item( | 24   args_list_identifier_undefined.append_item( | 
| 24       scoped_ptr<ParseNode>(new IdentifierNode(undefined_token))); | 25       std::unique_ptr<ParseNode>(new IdentifierNode(undefined_token))); | 
| 25   Value result = functions::RunDefined(setup.scope(), &function_call, | 26   Value result = functions::RunDefined(setup.scope(), &function_call, | 
| 26                                        &args_list_identifier_undefined, &err); | 27                                        &args_list_identifier_undefined, &err); | 
| 27   ASSERT_EQ(Value::BOOLEAN, result.type()); | 28   ASSERT_EQ(Value::BOOLEAN, result.type()); | 
| 28   EXPECT_FALSE(result.boolean_value()); | 29   EXPECT_FALSE(result.boolean_value()); | 
| 29 | 30 | 
| 30   // Define a value that's itself a scope value. | 31   // Define a value that's itself a scope value. | 
| 31   const char kDef[] = "def";  // Defined variable name. | 32   const char kDef[] = "def";  // Defined variable name. | 
| 32   setup.scope()->SetValue( | 33   setup.scope()->SetValue( | 
| 33       kDef, Value(nullptr, scoped_ptr<Scope>(new Scope(setup.scope()))), | 34       kDef, Value(nullptr, std::unique_ptr<Scope>(new Scope(setup.scope()))), | 
| 34       nullptr); | 35       nullptr); | 
| 35 | 36 | 
| 36   // Test the defined identifier. | 37   // Test the defined identifier. | 
| 37   Token defined_token(Location(), Token::IDENTIFIER, kDef); | 38   Token defined_token(Location(), Token::IDENTIFIER, kDef); | 
| 38   ListNode args_list_identifier_defined; | 39   ListNode args_list_identifier_defined; | 
| 39   args_list_identifier_defined.append_item( | 40   args_list_identifier_defined.append_item( | 
| 40       scoped_ptr<ParseNode>(new IdentifierNode(defined_token))); | 41       std::unique_ptr<ParseNode>(new IdentifierNode(defined_token))); | 
| 41   result = functions::RunDefined(setup.scope(), &function_call, | 42   result = functions::RunDefined(setup.scope(), &function_call, | 
| 42                                  &args_list_identifier_defined, &err); | 43                                  &args_list_identifier_defined, &err); | 
| 43   ASSERT_EQ(Value::BOOLEAN, result.type()); | 44   ASSERT_EQ(Value::BOOLEAN, result.type()); | 
| 44   EXPECT_TRUE(result.boolean_value()); | 45   EXPECT_TRUE(result.boolean_value()); | 
| 45 | 46 | 
| 46   // Should also work by passing an accessor node so you can do | 47   // Should also work by passing an accessor node so you can do | 
| 47   // "defined(def.foo)" to see if foo is defined on the def scope. | 48   // "defined(def.foo)" to see if foo is defined on the def scope. | 
| 48   scoped_ptr<AccessorNode> undef_accessor(new AccessorNode); | 49   std::unique_ptr<AccessorNode> undef_accessor(new AccessorNode); | 
| 49   undef_accessor->set_base(defined_token); | 50   undef_accessor->set_base(defined_token); | 
| 50   undef_accessor->set_member(scoped_ptr<IdentifierNode>( | 51   undef_accessor->set_member( | 
| 51       new IdentifierNode(undefined_token))); | 52       base::WrapUnique(new IdentifierNode(undefined_token))); | 
| 52   ListNode args_list_accessor_defined; | 53   ListNode args_list_accessor_defined; | 
| 53   args_list_accessor_defined.append_item(std::move(undef_accessor)); | 54   args_list_accessor_defined.append_item(std::move(undef_accessor)); | 
| 54   result = functions::RunDefined(setup.scope(), &function_call, | 55   result = functions::RunDefined(setup.scope(), &function_call, | 
| 55                                  &args_list_accessor_defined, &err); | 56                                  &args_list_accessor_defined, &err); | 
| 56   ASSERT_EQ(Value::BOOLEAN, result.type()); | 57   ASSERT_EQ(Value::BOOLEAN, result.type()); | 
| 57   EXPECT_FALSE(result.boolean_value()); | 58   EXPECT_FALSE(result.boolean_value()); | 
| 58 } | 59 } | 
| 59 | 60 | 
| 60 // Tests that an error is thrown when a {} is supplied to a function that | 61 // Tests that an error is thrown when a {} is supplied to a function that | 
| 61 // doesn't take one. | 62 // doesn't take one. | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
| 82   EXPECT_FALSE(defined_no_scope.has_error()); | 83   EXPECT_FALSE(defined_no_scope.has_error()); | 
| 83   result = defined_no_scope.parsed()->Execute(setup.scope(), &err); | 84   result = defined_no_scope.parsed()->Execute(setup.scope(), &err); | 
| 84   EXPECT_FALSE(err.has_error()); | 85   EXPECT_FALSE(err.has_error()); | 
| 85 | 86 | 
| 86   // A block to defined should fail. | 87   // A block to defined should fail. | 
| 87   TestParseInput defined_with_scope("defined(foo) {}"); | 88   TestParseInput defined_with_scope("defined(foo) {}"); | 
| 88   EXPECT_FALSE(defined_with_scope.has_error()); | 89   EXPECT_FALSE(defined_with_scope.has_error()); | 
| 89   result = defined_with_scope.parsed()->Execute(setup.scope(), &err); | 90   result = defined_with_scope.parsed()->Execute(setup.scope(), &err); | 
| 90   EXPECT_TRUE(err.has_error()); | 91   EXPECT_TRUE(err.has_error()); | 
| 91 } | 92 } | 
| OLD | NEW | 
|---|