3 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Unittest for generate_gyp_py.
9 It's tough to test the lower-level GetSourceFiles() and GetObjectFiles()
10 functions, so this focuses on the higher-level functions assuming those two
11 functions are working as intended (i.e., producing lists of files).
16 import generate_gyp as gg
19 class ModuleUnittest(unittest.TestCase):
20 def testGetObjectToSourceMapping(self):
31 self.assertEqual(expected, gg.GetObjectToSourceMapping(srcs))
33 def testGetSourceFileSet(self):
43 expected = set(['a.c', 'c.cc'])
44 self.assertEqual(expected, gg.GetSourceFileSet(objs_to_srcs, objs))
46 def testGetSourceFileSet_NotFound(self):
55 self.assertRaises(KeyError, gg.GetSourceFileSet, objs_to_srcs, objs)
58 class SourceSetUnittest(unittest.TestCase):
60 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2']), set(['3']))
61 b = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2']), set(['3']))
62 c = gg.SourceSet(set(['c', 'd']), set(['1']), set(['2']), set(['3']))
63 d = gg.SourceSet(set(['a', 'b']), set(['0']), set(['2']), set(['3']))
64 e = gg.SourceSet(set(['a', 'b']), set(['1']), set(['0']), set(['3']))
65 f = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2']), set(['0']))
67 self.assertEqual(a, b)
68 self.assertNotEqual(a, c)
69 self.assertNotEqual(a, d)
70 self.assertNotEqual(a, e)
71 self.assertNotEqual(a, f)
73 def testIntersect_Exact(self):
74 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2']), set(['5']))
75 b = gg.SourceSet(set(['a', 'b']), set(['3']), set(['4']), set(['6']))
79 self.assertEqual(c.sources, set(['a', 'b']))
80 self.assertEqual(c.architectures, set(['1', '3']))
81 self.assertEqual(c.targets, set(['2', '4']))
82 self.assertEqual(c.platforms, set(['5', '6']))
83 self.assertFalse(c.IsEmpty())
85 def testIntersect_Disjoint(self):
86 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2']), set(['5']))
87 b = gg.SourceSet(set(['c', 'd']), set(['3']), set(['4']), set(['6']))
91 self.assertEqual(c.sources, set())
92 self.assertEqual(c.architectures, set(['1', '3']))
93 self.assertEqual(c.targets, set(['2', '4']))
94 self.assertEqual(c.platforms, set(['5', '6']))
95 self.assertTrue(c.IsEmpty())
97 def testIntersect_Overlap(self):
98 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2']), set(['5']))
99 b = gg.SourceSet(set(['b', 'c']), set(['3']), set(['4']), set(['6']))
103 self.assertEqual(c.sources, set(['b']))
104 self.assertEqual(c.architectures, set(['1', '3']))
105 self.assertEqual(c.targets, set(['2', '4']))
106 self.assertEqual(c.platforms, set(['5', '6']))
107 self.assertFalse(c.IsEmpty())
109 def testDifference_Exact(self):
110 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2']), set(['3']))
111 b = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2']), set(['3']))
115 self.assertEqual(c.sources, set())
116 self.assertEqual(c.architectures, set(['1']))
117 self.assertEqual(c.targets, set(['2']))
118 self.assertEqual(c.platforms, set(['3']))
119 self.assertTrue(c.IsEmpty())
121 def testDifference_Disjoint(self):
122 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2']), set(['5']))
123 b = gg.SourceSet(set(['c', 'd']), set(['3']), set(['4']), set(['6']))
127 self.assertEqual(c.sources, set(['a', 'b']))
128 self.assertEqual(c.architectures, set())
129 self.assertEqual(c.targets, set())
130 self.assertEqual(c.platforms, set())
131 self.assertTrue(c.IsEmpty())
133 def testDifference_Overlap(self):
134 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2']), set(['5']))
135 b = gg.SourceSet(set(['b', 'c', 'd']), set(['1', '3']), set(['2', '4']),
140 self.assertEqual(c.sources, set(['a']))
141 self.assertEqual(c.architectures, set(['1']))
142 self.assertEqual(c.targets, set(['2']))
143 self.assertEqual(c.platforms, set(['5']))
144 self.assertFalse(c.IsEmpty())
146 def testGenerateGypStanza(self):
147 # ia32 should just be ia32. Win should appear as an OS restriction.
148 a = gg.SourceSet(set(['a', 'b']), set(['ia32']), set(['Chromium']),
149 set(['win'])).GenerateGypStanza()
150 string.index(a, 'target_arch == "ia32"')
151 string.index(a, 'OS == "win"')
153 # x64 should just be x64. Linux should not appear as an OS restriction.
154 a = gg.SourceSet(set(['a', 'b']), set(['x64']), set(['Chromium']),
155 set(['linux'])).GenerateGypStanza()
156 string.index(a, 'target_arch == "x64"')
157 self.assertEqual(string.find(a, 'OS == "linux"'), -1)
159 # arm should just be arm.
160 a = gg.SourceSet(set(['a', 'b']), set(['arm']), set(['Chromium']),
161 set(['linux'])).GenerateGypStanza()
162 string.index(a, 'target_arch == "arm"')
164 # arm-neon should be arm and flip the arm_neon switch.
165 a = gg.SourceSet(set(['a', 'b']), set(['arm-neon']), set(['Chromium']),
166 set(['linux'])).GenerateGypStanza()
167 string.index(a, 'target_arch == "arm" and arm_neon == 1')
169 # All architectures and all platforms case.
170 a = gg.SourceSet(set(['a', 'b']), set(['arm', 'arm-neon', 'x64', 'ia32']),
172 set(['win', 'linux'])).GenerateGypStanza()
173 string.index(a, '(1)')
174 self.assertEqual(string.find(a, 'OS == "linux"'), -1)
175 self.assertEqual(string.find(a, 'OS == "win"'), -1)
178 a = gg.SourceSet(set(['a', 'b']), set(['arm']),
179 set(['Chromium', 'ChromiumOS', 'Chrome', 'ChromeOS']),
180 set(['win'])).GenerateGypStanza()
181 string.index(a, '(1)')
183 def testGenerateGnStanza(self):
184 # ia32 should be x86. Win should appear as an OS restriction.
185 a = gg.SourceSet(set(['a', 'b']), set(['ia32']), set(['Chromium']),
186 set(['win'])).GenerateGnStanza()
187 string.index(a, 'cpu_arch == "x86"')
188 string.index(a, 'is_win')
190 # x64 should just be x64. Linux should not appear as an OS restriction.
191 a = gg.SourceSet(set(['a', 'b']), set(['x64']), set(['Chromium']),
192 set(['linux'])).GenerateGnStanza()
193 string.index(a, 'cpu_arch == "x64"')
194 self.assertEqual(string.find(a, 'is_linux'), -1)
196 # arm should just be arm.
197 a = gg.SourceSet(set(['a', 'b']), set(['arm']), set(['Chromium']),
198 set(['linux'])).GenerateGnStanza()
199 string.index(a, 'cpu_arch == "arm"')
201 # arm-neon should be arm and flip the arm_neon switch.
202 a = gg.SourceSet(set(['a', 'b']), set(['arm-neon']), set(['Chromium']),
203 set(['linux'])).GenerateGnStanza()
204 string.index(a, 'cpu_arch == "arm" && arm_use_neon')
206 # All architectures and all platforms case.
207 a = gg.SourceSet(set(['a', 'b']), set(['arm', 'arm-neon', 'x64', 'ia32']),
209 set(['win', 'linux'])).GenerateGnStanza()
210 string.index(a, 'if (ffmpeg_branding == "Chromium")')
213 a = gg.SourceSet(set(['a', 'b']), set(['arm']),
214 set(['Chromium', 'ChromiumOS', 'Chrome', 'ChromeOS']),
215 set(['win'])).GenerateGnStanza()
216 string.index(a, 'cpu_arch == "arm"')
217 string.index(a, 'is_win')
220 def assertEqualSets(self, actual, expected):
221 # Do pairwise checks for easier debugging.
223 self.assertTrue(a in expected, msg='Unexpected set: %s' % a)
225 self.assertTrue(e in actual, msg='Did not find expected set: %s' % e)
227 def testCreatePairwiseDisjointSets_Pair(self):
228 a = gg.SourceSet(set(['common', 'intel']), set(['ia32']),
229 set(['Chromium']), set(['win']))
230 b = gg.SourceSet(set(['common', 'intel', 'chrome']), set(['ia32']),
231 set(['Chrome']), set(['win']))
234 expected.append(gg.SourceSet(set(['common', 'intel']), set(['ia32']),
235 set(['Chromium', 'Chrome']), set(['win'])))
236 expected.append(gg.SourceSet(set(['chrome']), set(['ia32']),
237 set(['Chrome']), set(['win'])))
239 sets = gg.CreatePairwiseDisjointSets([a, b])
240 self.assertEqualSets(sets, expected)
242 def testCreatePairwiseDisjointSets_Triplet(self):
243 a = gg.SourceSet(set(['common', 'intel']), set(['ia32']), set(['Chromium']),
245 b = gg.SourceSet(set(['common', 'intel', 'chrome']), set(['x64']),
246 set(['Chrome']), set(['win']))
247 c = gg.SourceSet(set(['common', 'arm']), set(['arm']), set(['Chromium']),
251 expected.append(gg.SourceSet(set(['common']), set(['ia32', 'x64', 'arm']),
252 set(['Chromium', 'Chrome']), set(['win'])))
253 expected.append(gg.SourceSet(set(['intel']), set(['ia32', 'x64']),
254 set(['Chromium', 'Chrome']), set(['win'])))
255 expected.append(gg.SourceSet(set(['chrome']), set(['x64']),
256 set(['Chrome']), set(['win'])))
257 expected.append(gg.SourceSet(set(['arm']), set(['arm']), set(['Chromium']),
260 sets = gg.CreatePairwiseDisjointSets([a, b, c])
261 self.assertEqualSets(sets, expected)
263 def testCreatePairwiseDisjointSets_Multiple(self):
264 a = gg.SourceSet(set(['common', 'intel']),
268 b = gg.SourceSet(set(['common', 'intel', 'chrome']),
272 c = gg.SourceSet(set(['common', 'intel']),
276 d = gg.SourceSet(set(['common', 'intel', 'chrome']),
280 e = gg.SourceSet(set(['common', 'arm']),
284 f = gg.SourceSet(set(['common', 'arm-neon', 'chrome', 'chromeos']),
290 expected.append(gg.SourceSet(set(['common']),
291 set(['ia32', 'x64', 'arm', 'arm-neon']),
292 set(['Chromium', 'Chrome', 'ChromeOS']),
294 expected.append(gg.SourceSet(set(['intel']),
295 set(['ia32', 'x64']),
296 set(['Chromium', 'Chrome']),
298 expected.append(gg.SourceSet(set(['arm']),
302 expected.append(gg.SourceSet(set(['chrome']),
303 set(['ia32', 'x64', 'arm-neon']),
304 set(['Chrome', 'ChromeOS']),
306 expected.append(gg.SourceSet(set(['arm-neon', 'chromeos']),
311 sets = gg.CreatePairwiseDisjointSets([a, b, c, d, e, f])
312 self.assertEqualSets(sets, expected)
314 if __name__ == '__main__':