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.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/inherited_libraries.h"
7 #include "tools/gn/target.h"
8 #include "tools/gn/test_with_scope.h"
12 // In these tests, Pair can't be used conveniently because the
13 // "const" won't be inferred and the types won't match. This helper makes the
14 // right type of pair with the const Target.
15 std::pair
<const Target
*, bool> Pair(const Target
* t
, bool b
) {
16 return std::pair
<const Target
*, bool>(t
, b
);
21 TEST(InheritedLibraries
, Unique
) {
24 Target
a(setup
.settings(), Label(SourceDir("//foo/"), "a"));
25 Target
b(setup
.settings(), Label(SourceDir("//foo/"), "b"));
27 // Setup, add the two targets as private.
28 InheritedLibraries libs
;
29 libs
.Append(&a
, false);
30 libs
.Append(&b
, false);
31 auto result
= libs
.GetOrderedAndPublicFlag();
32 ASSERT_EQ(2u, result
.size());
33 EXPECT_EQ(Pair(&a
, false), result
[0]);
34 EXPECT_EQ(Pair(&b
, false), result
[1]);
36 // Add again as private, this should be a NOP.
37 libs
.Append(&a
, false);
38 libs
.Append(&b
, false);
39 result
= libs
.GetOrderedAndPublicFlag();
40 ASSERT_EQ(2u, result
.size());
41 EXPECT_EQ(Pair(&a
, false), result
[0]);
42 EXPECT_EQ(Pair(&b
, false), result
[1]);
44 // Add as public, this should make both public.
45 libs
.Append(&a
, true);
46 libs
.Append(&b
, true);
47 result
= libs
.GetOrderedAndPublicFlag();
48 ASSERT_EQ(2u, result
.size());
49 EXPECT_EQ(Pair(&a
, true), result
[0]);
50 EXPECT_EQ(Pair(&b
, true), result
[1]);
52 // Add again private, they should stay public.
53 libs
.Append(&a
, false);
54 libs
.Append(&b
, false);
55 result
= libs
.GetOrderedAndPublicFlag();
56 ASSERT_EQ(2u, result
.size());
57 EXPECT_EQ(Pair(&a
, true), result
[0]);
58 EXPECT_EQ(Pair(&b
, true), result
[1]);
61 TEST(InheritedLibraries
, AppendInherited
) {
64 Target
a(setup
.settings(), Label(SourceDir("//foo/"), "a"));
65 Target
b(setup
.settings(), Label(SourceDir("//foo/"), "b"));
66 Target
w(setup
.settings(), Label(SourceDir("//foo/"), "w"));
67 Target
x(setup
.settings(), Label(SourceDir("//foo/"), "x"));
68 Target
y(setup
.settings(), Label(SourceDir("//foo/"), "y"));
69 Target
z(setup
.settings(), Label(SourceDir("//foo/"), "z"));
71 InheritedLibraries libs
;
72 libs
.Append(&a
, false);
73 libs
.Append(&b
, false);
75 // Appending these things with private inheritance should make them private,
76 // no matter how they're listed in the appended class.
77 InheritedLibraries append_private
;
78 append_private
.Append(&a
, true);
79 append_private
.Append(&b
, false);
80 append_private
.Append(&w
, true);
81 append_private
.Append(&x
, false);
82 libs
.AppendInherited(append_private
, false);
84 auto result
= libs
.GetOrderedAndPublicFlag();
85 ASSERT_EQ(4u, result
.size());
86 EXPECT_EQ(Pair(&a
, false), result
[0]);
87 EXPECT_EQ(Pair(&b
, false), result
[1]);
88 EXPECT_EQ(Pair(&w
, false), result
[2]);
89 EXPECT_EQ(Pair(&x
, false), result
[3]);
91 // Appending these things with public inheritance should convert them.
92 InheritedLibraries append_public
;
93 append_public
.Append(&a
, true);
94 append_public
.Append(&b
, false);
95 append_public
.Append(&y
, true);
96 append_public
.Append(&z
, false);
97 libs
.AppendInherited(append_public
, true);
99 result
= libs
.GetOrderedAndPublicFlag();
100 ASSERT_EQ(6u, result
.size());
101 EXPECT_EQ(Pair(&a
, true), result
[0]); // Converted to public.
102 EXPECT_EQ(Pair(&b
, false), result
[1]);
103 EXPECT_EQ(Pair(&w
, false), result
[2]);
104 EXPECT_EQ(Pair(&x
, false), result
[3]);
105 EXPECT_EQ(Pair(&y
, true), result
[4]); // Appended as public.
106 EXPECT_EQ(Pair(&z
, false), result
[5]);
109 TEST(InheritedLibraries
, AppendPublicSharedLibraries
) {
111 InheritedLibraries append
;
114 Target
set_pub(setup
.settings(), Label(SourceDir("//foo/"), "set_pub"));
115 set_pub
.set_output_type(Target::SOURCE_SET
);
116 append
.Append(&set_pub
, true);
117 Target
set_priv(setup
.settings(), Label(SourceDir("//foo/"), "set_priv"));
118 set_priv
.set_output_type(Target::SOURCE_SET
);
119 append
.Append(&set_priv
, false);
121 // Two shared libraries.
122 Target
sh_pub(setup
.settings(), Label(SourceDir("//foo/"), "sh_pub"));
123 sh_pub
.set_output_type(Target::SHARED_LIBRARY
);
124 append
.Append(&sh_pub
, true);
125 Target
sh_priv(setup
.settings(), Label(SourceDir("//foo/"), "sh_priv"));
126 sh_priv
.set_output_type(Target::SHARED_LIBRARY
);
127 append
.Append(&sh_priv
, false);
129 InheritedLibraries libs
;
130 libs
.AppendPublicSharedLibraries(append
, true);
132 auto result
= libs
.GetOrderedAndPublicFlag();
133 ASSERT_EQ(1u, result
.size());
134 EXPECT_EQ(Pair(&sh_pub
, true), result
[0]);