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
3 // found in the LICENSE file.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/functions.h"
7 #include "tools/gn/parse_tree.h"
8 #include "tools/gn/test_with_scope.h"
9 #include "tools/gn/value.h"
11 TEST(Functions
, Defined
) {
14 //InputFile input_file(SourceFile("//foo"));
15 FunctionCallNode function_call
;
18 // Test an undefined identifier.
19 Token
undefined_token(Location(), Token::IDENTIFIER
, "undef");
20 ListNode args_list_identifier_undefined
;
21 args_list_identifier_undefined
.append_item(
22 scoped_ptr
<ParseNode
>(new IdentifierNode(undefined_token
)));
23 Value result
= functions::RunDefined(setup
.scope(), &function_call
,
24 &args_list_identifier_undefined
, &err
);
25 ASSERT_EQ(Value::BOOLEAN
, result
.type());
26 EXPECT_FALSE(result
.boolean_value());
28 // Define a value that's itself a scope value.
29 const char kDef
[] = "def"; // Defined variable name.
30 Scope
nested(setup
.scope());
31 setup
.scope()->SetValue(kDef
, Value(NULL
, &nested
), NULL
);
33 // Test the defined identifier.
34 Token
defined_token(Location(), Token::IDENTIFIER
, kDef
);
35 ListNode args_list_identifier_defined
;
36 args_list_identifier_defined
.append_item(
37 scoped_ptr
<ParseNode
>(new IdentifierNode(defined_token
)));
38 result
= functions::RunDefined(setup
.scope(), &function_call
,
39 &args_list_identifier_defined
, &err
);
40 ASSERT_EQ(Value::BOOLEAN
, result
.type());
41 EXPECT_TRUE(result
.boolean_value());
43 // Should also work by passing an accessor node so you can do
44 // "defined(def.foo)" to see if foo is defined on the def scope.
45 scoped_ptr
<AccessorNode
> undef_accessor(new AccessorNode
);
46 undef_accessor
->set_base(defined_token
);
47 undef_accessor
->set_member(scoped_ptr
<IdentifierNode
>(
48 new IdentifierNode(undefined_token
)));
49 ListNode args_list_accessor_defined
;
50 args_list_accessor_defined
.append_item(undef_accessor
.PassAs
<ParseNode
>());
51 result
= functions::RunDefined(setup
.scope(), &function_call
,
52 &args_list_accessor_defined
, &err
);
53 ASSERT_EQ(Value::BOOLEAN
, result
.type());
54 EXPECT_FALSE(result
.boolean_value());