• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/inherited_libraries.h"
6 #include "gn/target.h"
7 #include "gn/test_with_scope.h"
8 #include "util/test/test.h"
9 
10 namespace {
11 
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.
Pair(const Target * t,bool b)15 std::pair<const Target*, bool> Pair(const Target* t, bool b) {
16   return std::pair<const Target*, bool>(t, b);
17 }
18 
19 }  // namespace
20 
TEST(InheritedLibraries,Unique)21 TEST(InheritedLibraries, Unique) {
22   TestWithScope setup;
23 
24   Target a(setup.settings(), Label(SourceDir("//foo/"), "a"));
25   Target b(setup.settings(), Label(SourceDir("//foo/"), "b"));
26 
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]);
35 
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]);
43 
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]);
51 
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]);
59 }
60 
TEST(InheritedLibraries,AppendInherited)61 TEST(InheritedLibraries, AppendInherited) {
62   TestWithScope setup;
63 
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"));
70 
71   InheritedLibraries libs;
72   libs.Append(&a, false);
73   libs.Append(&b, false);
74 
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);
83 
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]);
90 
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);
98 
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]);
107 }
108 
TEST(InheritedLibraries,AppendPublicSharedLibraries)109 TEST(InheritedLibraries, AppendPublicSharedLibraries) {
110   TestWithScope setup;
111   InheritedLibraries append;
112 
113   // Two source sets.
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);
120 
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);
128 
129   InheritedLibraries libs;
130   libs.AppendPublicSharedLibraries(append, true);
131 
132   auto result = libs.GetOrderedAndPublicFlag();
133   ASSERT_EQ(1u, result.size());
134   EXPECT_EQ(Pair(&sh_pub, true), result[0]);
135 }
136