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 9import gyp.common 10import unittest 11import sys 12 13 14class 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 def GetEdge(node): 24 return tuple(graph[node]) 25 self.assertEqual( 26 gyp.common.TopologicallySorted(graph.keys(), GetEdge), 27 ['a', 'c', 'd', 'b']) 28 29 def test_Cycle(self): 30 """Test that an exception is thrown on a cyclic graph.""" 31 graph = { 32 'a': ['b'], 33 'b': ['c'], 34 'c': ['d'], 35 'd': ['a'], 36 } 37 def GetEdge(node): 38 return tuple(graph[node]) 39 self.assertRaises( 40 gyp.common.CycleError, gyp.common.TopologicallySorted, 41 graph.keys(), GetEdge) 42 43 44class TestGetFlavor(unittest.TestCase): 45 """Test that gyp.common.GetFlavor works as intended""" 46 original_platform = '' 47 48 def setUp(self): 49 self.original_platform = sys.platform 50 51 def tearDown(self): 52 sys.platform = self.original_platform 53 54 def assertFlavor(self, expected, argument, param): 55 sys.platform = argument 56 self.assertEqual(expected, gyp.common.GetFlavor(param)) 57 58 def test_platform_default(self): 59 self.assertFlavor('freebsd', 'freebsd9' , {}) 60 self.assertFlavor('freebsd', 'freebsd10', {}) 61 self.assertFlavor('openbsd', 'openbsd5' , {}) 62 self.assertFlavor('solaris', 'sunos5' , {}); 63 self.assertFlavor('solaris', 'sunos' , {}); 64 self.assertFlavor('linux' , 'linux2' , {}); 65 self.assertFlavor('linux' , 'linux3' , {}); 66 67 def test_param(self): 68 self.assertFlavor('foobar', 'linux2' , {'flavor': 'foobar'}) 69 70 71if __name__ == '__main__': 72 unittest.main() 73