1 | #!/usr/bin/env python
|
---|
2 |
|
---|
3 | # Copyright (c) 2012 Google Inc. 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 | """Unit tests for the common.py file."""
|
---|
8 |
|
---|
9 | import gyp.common
|
---|
10 | import unittest
|
---|
11 | import sys
|
---|
12 |
|
---|
13 |
|
---|
14 | class TestTopologicallySorted(unittest.TestCase):
|
---|
15 | def test_Valid(self):
|
---|
16 | """Test that sorting works on a valid graph with one possible order."""
|
---|
17 | graph = {
|
---|
18 | "a": ["b", "c"],
|
---|
19 | "b": [],
|
---|
20 | "c": ["d"],
|
---|
21 | "d": ["b"],
|
---|
22 | }
|
---|
23 |
|
---|
24 | def GetEdge(node):
|
---|
25 | return tuple(graph[node])
|
---|
26 |
|
---|
27 | self.assertEqual(
|
---|
28 | gyp.common.TopologicallySorted(graph.keys(), GetEdge), ["a", "c", "d", "b"]
|
---|
29 | )
|
---|
30 |
|
---|
31 | def test_Cycle(self):
|
---|
32 | """Test that an exception is thrown on a cyclic graph."""
|
---|
33 | graph = {
|
---|
34 | "a": ["b"],
|
---|
35 | "b": ["c"],
|
---|
36 | "c": ["d"],
|
---|
37 | "d": ["a"],
|
---|
38 | }
|
---|
39 |
|
---|
40 | def GetEdge(node):
|
---|
41 | return tuple(graph[node])
|
---|
42 |
|
---|
43 | self.assertRaises(
|
---|
44 | gyp.common.CycleError, gyp.common.TopologicallySorted, graph.keys(), GetEdge
|
---|
45 | )
|
---|
46 |
|
---|
47 |
|
---|
48 | class TestGetFlavor(unittest.TestCase):
|
---|
49 | """Test that gyp.common.GetFlavor works as intended"""
|
---|
50 |
|
---|
51 | original_platform = ""
|
---|
52 |
|
---|
53 | def setUp(self):
|
---|
54 | self.original_platform = sys.platform
|
---|
55 |
|
---|
56 | def tearDown(self):
|
---|
57 | sys.platform = self.original_platform
|
---|
58 |
|
---|
59 | def assertFlavor(self, expected, argument, param):
|
---|
60 | sys.platform = argument
|
---|
61 | self.assertEqual(expected, gyp.common.GetFlavor(param))
|
---|
62 |
|
---|
63 | def test_platform_default(self):
|
---|
64 | self.assertFlavor("freebsd", "freebsd9", {})
|
---|
65 | self.assertFlavor("freebsd", "freebsd10", {})
|
---|
66 | self.assertFlavor("openbsd", "openbsd5", {})
|
---|
67 | self.assertFlavor("solaris", "sunos5", {})
|
---|
68 | self.assertFlavor("solaris", "sunos", {})
|
---|
69 | self.assertFlavor("linux", "linux2", {})
|
---|
70 | self.assertFlavor("linux", "linux3", {})
|
---|
71 | self.assertFlavor("linux", "linux", {})
|
---|
72 |
|
---|
73 | def test_param(self):
|
---|
74 | self.assertFlavor("foobar", "linux2", {"flavor": "foobar"})
|
---|
75 |
|
---|
76 |
|
---|
77 | if __name__ == "__main__":
|
---|
78 | unittest.main()
|
---|