1 // Copyright 2015 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 "gn/args.h"
6
7 #include "gn/scheduler.h"
8 #include "gn/test_with_scope.h"
9 #include "util/test/test.h"
10
11 // Assertions for VerifyAllOverridesUsed() and DeclareArgs() with multiple
12 // toolchains.
TEST(ArgsTest,VerifyAllOverridesUsed)13 TEST(ArgsTest, VerifyAllOverridesUsed) {
14 TestWithScope setup1, setup2;
15 Args args;
16 Scope::KeyValueMap key_value_map1;
17 Err err;
18 LiteralNode assignment1;
19
20 setup1.scope()->SetValue("a", Value(nullptr, true), &assignment1);
21 setup1.scope()->GetCurrentScopeValues(&key_value_map1);
22 EXPECT_TRUE(args.DeclareArgs(key_value_map1, setup1.scope(), &err));
23
24 LiteralNode assignment2;
25 setup2.scope()->SetValue("b", Value(nullptr, true), &assignment2);
26 Scope::KeyValueMap key_value_map2;
27 setup2.scope()->GetCurrentScopeValues(&key_value_map2);
28 EXPECT_TRUE(args.DeclareArgs(key_value_map2, setup2.scope(), &err));
29
30 // Override "a", shouldn't see any errors as "a" was defined.
31 args.AddArgOverride("a", Value(nullptr, true));
32 EXPECT_TRUE(args.VerifyAllOverridesUsed(&err));
33
34 // Override "a", & "b", shouldn't see any errors as both were defined.
35 args.AddArgOverride("b", Value(nullptr, true));
36 EXPECT_TRUE(args.VerifyAllOverridesUsed(&err));
37
38 // Override "a", "b" and "c", should fail as "c" was not defined.
39 args.AddArgOverride("c", Value(nullptr, true));
40 EXPECT_FALSE(args.VerifyAllOverridesUsed(&err));
41 }
42
43 // Ensure that arg overrides get only set after the they were declared.
TEST(ArgsTest,VerifyOverrideScope)44 TEST(ArgsTest, VerifyOverrideScope) {
45 TestWithScope setup;
46 Args args;
47 Err err;
48
49 args.AddArgOverride("a", Value(nullptr, "avalue"));
50 args.AddArgOverride("current_os", Value(nullptr, "theiros"));
51
52 Scope::KeyValueMap toolchain_overrides;
53 toolchain_overrides["b"] = Value(nullptr, "bvalue");
54 toolchain_overrides["current_os"] = Value(nullptr, "myos");
55 args.SetupRootScope(setup.scope(), toolchain_overrides);
56
57 // Overrides of arguments not yet declared aren't applied yet.
58 EXPECT_EQ(nullptr, setup.scope()->GetValue("a"));
59 EXPECT_EQ(nullptr, setup.scope()->GetValue("b"));
60
61 // |current_os| is a system var. and already declared.
62 // Thus it should have our override value.
63 ASSERT_NE(nullptr, setup.scope()->GetValue("current_os"));
64 EXPECT_EQ(Value(nullptr, "myos"), *setup.scope()->GetValue("current_os"));
65
66 Scope::KeyValueMap key_value_map1;
67 key_value_map1["a"] = Value(nullptr, "avalue2");
68 key_value_map1["b"] = Value(nullptr, "bvalue2");
69 key_value_map1["c"] = Value(nullptr, "cvalue2");
70 EXPECT_TRUE(args.DeclareArgs(key_value_map1, setup.scope(), &err));
71
72 ASSERT_NE(nullptr, setup.scope()->GetValue("a"));
73 EXPECT_EQ(Value(nullptr, "avalue"), *setup.scope()->GetValue("a"));
74
75 ASSERT_NE(nullptr, setup.scope()->GetValue("b"));
76 EXPECT_EQ(Value(nullptr, "bvalue"), *setup.scope()->GetValue("b"));
77
78 // This wasn't overwritten, so it should have the default value.
79 ASSERT_NE(nullptr, setup.scope()->GetValue("c"));
80 EXPECT_EQ(Value(nullptr, "cvalue2"), *setup.scope()->GetValue("c"));
81 }
82