| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/python |
| 2 # |
| 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. |
| 6 |
| 7 """Unittest for generate_gyp_py. |
| 8 |
| 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). |
| 12 """ |
| 13 |
| 14 import string |
| 15 import unittest |
| 16 import generate_gyp as gg |
| 17 |
| 18 class ModuleUnittest(unittest.TestCase): |
| 19 def testGetObjectToSourceMapping(self): |
| 20 srcs = [ |
| 21 'a.c', |
| 22 'b.asm', |
| 23 'c.cc', |
| 24 ] |
| 25 expected = { |
| 26 'a.o': 'a.c', |
| 27 'b.o': 'b.asm', |
| 28 'c.o': 'c.cc', |
| 29 } |
| 30 self.assertEqual(expected, gg.GetObjectToSourceMapping(srcs)) |
| 31 |
| 32 def testGetSourceFileSet(self): |
| 33 objs_to_srcs = { |
| 34 'a.o': 'a.c', |
| 35 'b.o': 'b.asm', |
| 36 'c.o': 'c.cc', |
| 37 } |
| 38 objs = [ |
| 39 'a.o', |
| 40 'c.o', |
| 41 ] |
| 42 expected = set(['a.c', 'c.cc']) |
| 43 self.assertEqual(expected, gg.GetSourceFileSet(objs_to_srcs, objs)) |
| 44 |
| 45 def testGetSourceFileSet_NotFound(self): |
| 46 objs_to_srcs = { |
| 47 'a.o': 'a.c', |
| 48 'b.o': 'b.asm', |
| 49 'c.o': 'c.cc', |
| 50 } |
| 51 objs = [ |
| 52 'd.o', |
| 53 ] |
| 54 self.assertRaises(KeyError, gg.GetSourceFileSet, objs_to_srcs, objs) |
| 55 |
| 56 class SourceSetUnittest(unittest.TestCase): |
| 57 def testEquals(self): |
| 58 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2'])) |
| 59 b = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2'])) |
| 60 c = gg.SourceSet(set(['c', 'd']), set(['1']), set(['2'])) |
| 61 d = gg.SourceSet(set(['a', 'b']), set(['0']), set(['2'])) |
| 62 e = gg.SourceSet(set(['a', 'b']), set(['1']), set(['0'])) |
| 63 |
| 64 self.assertEqual(a, b) |
| 65 self.assertNotEqual(a, c) |
| 66 self.assertNotEqual(a, d) |
| 67 self.assertNotEqual(a, e) |
| 68 |
| 69 def testIntersect_Exact(self): |
| 70 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2'])) |
| 71 b = gg.SourceSet(set(['a', 'b']), set(['3']), set(['4'])) |
| 72 |
| 73 c = a.Intersect(b) |
| 74 |
| 75 self.assertEqual(c.sources, set(['a', 'b'])) |
| 76 self.assertEqual(c.architectures, set(['1', '3'])) |
| 77 self.assertEqual(c.targets, set(['2', '4'])) |
| 78 self.assertFalse(c.IsEmpty()) |
| 79 |
| 80 def testIntersect_Disjoint(self): |
| 81 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2'])) |
| 82 b = gg.SourceSet(set(['c', 'd']), set(['3']), set(['4'])) |
| 83 |
| 84 c = a.Intersect(b) |
| 85 |
| 86 self.assertEqual(c.sources, set()) |
| 87 self.assertEqual(c.architectures, set(['1', '3'])) |
| 88 self.assertEqual(c.targets, set(['2', '4'])) |
| 89 self.assertTrue(c.IsEmpty()) |
| 90 |
| 91 def testIntersect_Overlap(self): |
| 92 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2'])) |
| 93 b = gg.SourceSet(set(['b', 'c']), set(['3']), set(['4'])) |
| 94 |
| 95 c = a.Intersect(b) |
| 96 |
| 97 self.assertEqual(c.sources, set(['b'])) |
| 98 self.assertEqual(c.architectures, set(['1', '3'])) |
| 99 self.assertEqual(c.targets, set(['2', '4'])) |
| 100 self.assertFalse(c.IsEmpty()) |
| 101 |
| 102 def testDifference_Exact(self): |
| 103 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2'])) |
| 104 b = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2'])) |
| 105 |
| 106 c = a.Difference(b) |
| 107 |
| 108 self.assertEqual(c.sources, set()) |
| 109 self.assertEqual(c.architectures, set(['1'])) |
| 110 self.assertEqual(c.targets, set(['2'])) |
| 111 self.assertTrue(c.IsEmpty()) |
| 112 |
| 113 def testDifference_Disjoint(self): |
| 114 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2'])) |
| 115 b = gg.SourceSet(set(['c', 'd']), set(['3']), set(['4'])) |
| 116 |
| 117 c = a.Difference(b) |
| 118 |
| 119 self.assertEqual(c.sources, set(['a', 'b'])) |
| 120 self.assertEqual(c.architectures, set()) |
| 121 self.assertEqual(c.targets, set()) |
| 122 self.assertTrue(c.IsEmpty()) |
| 123 |
| 124 def testDifference_Overlap(self): |
| 125 a = gg.SourceSet(set(['a', 'b']), set(['1']), set(['2'])) |
| 126 b = gg.SourceSet(set(['b', 'c', 'd']), set(['1', '3']), set(['2', '4'])) |
| 127 |
| 128 c = a.Difference(b) |
| 129 |
| 130 self.assertEqual(c.sources, set(['a'])) |
| 131 self.assertEqual(c.architectures, set(['1'])) |
| 132 self.assertEqual(c.targets, set(['2'])) |
| 133 self.assertFalse(c.IsEmpty()) |
| 134 |
| 135 def testGenerateGypStanza(self): |
| 136 # ia32 should just be ia32. |
| 137 a = gg.SourceSet(set(['a', 'b']), set(['ia32']), set(['Chromium'])) |
| 138 string.index(a.GenerateGypStanza(), |
| 139 'target_arch=="ia32"') |
| 140 |
| 141 # x64 should just be x64. |
| 142 a = gg.SourceSet(set(['a', 'b']), set(['x64']), set(['Chromium'])) |
| 143 string.index(a.GenerateGypStanza(), |
| 144 'target_arch=="x64"') |
| 145 |
| 146 # arm should just be arm. |
| 147 a = gg.SourceSet(set(['a', 'b']), set(['arm']), set(['Chromium'])) |
| 148 string.index(a.GenerateGypStanza(), |
| 149 'target_arch=="arm"') |
| 150 |
| 151 # arm-neon should be arm and flip the arm_neon switch. |
| 152 a = gg.SourceSet(set(['a', 'b']), set(['arm-neon']), set(['Chromium'])) |
| 153 string.index(a.GenerateGypStanza(), |
| 154 'target_arch=="arm" and arm_neon==1') |
| 155 |
| 156 # All architectures case. |
| 157 a = gg.SourceSet(set(['a', 'b']), set(['arm', 'arm-neon', 'x64', 'ia32']), |
| 158 set(['Chromium'])) |
| 159 string.index(a.GenerateGypStanza(), '(1)') |
| 160 |
| 161 # All targets case. |
| 162 a = gg.SourceSet(set(['a', 'b']), set(['arm']), |
| 163 set(['Chromium', 'ChromiumOS', 'Chrome', 'ChromeOS'])) |
| 164 string.index(a.GenerateGypStanza(), '(1)') |
| 165 |
| 166 def assertEqualSets(self, actual, expected): |
| 167 # Do pairwise checks for easier debugging. |
| 168 for a in actual: |
| 169 self.assertTrue(a in expected, msg='Unexpected set: %s' % a) |
| 170 for e in expected: |
| 171 self.assertTrue(e in actual, msg='Did not find expected set: %s' % e) |
| 172 |
| 173 def testCreatePairwiseDisjointSets_Pair(self): |
| 174 a = gg.SourceSet(set(['common', 'intel']), set(['ia32']), |
| 175 set(['Chromium'])) |
| 176 b = gg.SourceSet(set(['common', 'intel', 'chrome']), set(['ia32']), |
| 177 set(['Chrome'])) |
| 178 |
| 179 expected = [] |
| 180 expected.append(gg.SourceSet(set(['common', 'intel']), set(['ia32']), |
| 181 set(['Chromium', 'Chrome']))) |
| 182 expected.append(gg.SourceSet(set(['chrome']), set(['ia32']), |
| 183 set(['Chrome']))) |
| 184 |
| 185 sets = gg.CreatePairwiseDisjointSets([a, b]) |
| 186 self.assertEqualSets(sets, expected) |
| 187 |
| 188 def testCreatePairwiseDisjointSets_Triplet(self): |
| 189 a = gg.SourceSet(set(['common', 'intel']), set(['ia32']), set(['Chromium'])) |
| 190 b = gg.SourceSet(set(['common', 'intel', 'chrome']), set(['x64']), |
| 191 set(['Chrome'])) |
| 192 c = gg.SourceSet(set(['common', 'arm']), set(['arm']), set(['Chromium'])) |
| 193 |
| 194 expected = [] |
| 195 expected.append(gg.SourceSet(set(['common']), set(['ia32', 'x64', 'arm']), |
| 196 set(['Chromium', 'Chrome']))) |
| 197 expected.append(gg.SourceSet(set(['intel']), set(['ia32', 'x64']), |
| 198 set(['Chromium', 'Chrome']))) |
| 199 expected.append(gg.SourceSet(set(['chrome']), set(['x64']), |
| 200 set(['Chrome']))) |
| 201 expected.append(gg.SourceSet(set(['arm']), set(['arm']), set(['Chromium']))) |
| 202 |
| 203 sets = gg.CreatePairwiseDisjointSets([a, b, c]) |
| 204 self.assertEqualSets(sets, expected) |
| 205 |
| 206 def testCreatePairwiseDisjointSets_Multiple(self): |
| 207 a = gg.SourceSet(set(['common', 'intel']), |
| 208 set(['ia32']), |
| 209 set(['Chromium'])) |
| 210 b = gg.SourceSet(set(['common', 'intel', 'chrome']), |
| 211 set(['ia32']), |
| 212 set(['Chrome'])) |
| 213 c = gg.SourceSet(set(['common', 'intel']), |
| 214 set(['x64']), |
| 215 set(['Chromium'])) |
| 216 d = gg.SourceSet(set(['common', 'intel', 'chrome']), |
| 217 set(['x64']), |
| 218 set(['Chrome'])) |
| 219 e = gg.SourceSet(set(['common', 'arm']), |
| 220 set(['arm']), |
| 221 set(['Chromium'])) |
| 222 f = gg.SourceSet(set(['common', 'arm-neon', 'chrome', 'chromeos']), |
| 223 set(['arm-neon']), |
| 224 set(['ChromeOS'])) |
| 225 |
| 226 expected = [] |
| 227 expected.append(gg.SourceSet(set(['common']), |
| 228 set(['ia32', 'x64', 'arm', 'arm-neon']), |
| 229 set(['Chromium', 'Chrome', 'ChromeOS']))) |
| 230 expected.append(gg.SourceSet(set(['intel']), |
| 231 set(['ia32', 'x64']), |
| 232 set(['Chromium', 'Chrome']))) |
| 233 expected.append(gg.SourceSet(set(['arm']), |
| 234 set(['arm']), |
| 235 set(['Chromium']))) |
| 236 expected.append(gg.SourceSet(set(['chrome']), |
| 237 set(['ia32', 'x64', 'arm-neon']), |
| 238 set(['Chrome', 'ChromeOS']))) |
| 239 expected.append(gg.SourceSet(set(['arm-neon', 'chromeos']), |
| 240 set(['arm-neon']), |
| 241 set(['ChromeOS']))) |
| 242 |
| 243 sets = gg.CreatePairwiseDisjointSets([a, b, c, d, e, f]) |
| 244 self.assertEqualSets(sets, expected) |
| 245 |
| 246 if __name__ == '__main__': |
| 247 unittest.main() |
| OLD | NEW |