1#!/usr/bin/env python3
2# Copyright 2018 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import collections
7import os
8import sys
9import unittest
10
11sys.path.insert(
12    0, os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
13from util import build_utils
14
15_DEPS = collections.OrderedDict()
16_DEPS['a'] = []
17_DEPS['b'] = []
18_DEPS['c'] = ['a']
19_DEPS['d'] = ['a']
20_DEPS['e'] = ['f']
21_DEPS['f'] = ['a', 'd']
22_DEPS['g'] = []
23_DEPS['h'] = ['d', 'b', 'f']
24_DEPS['i'] = ['f']
25
26
27class BuildUtilsTest(unittest.TestCase):
28  def testGetSortedTransitiveDependencies_all(self):
29    TOP = _DEPS.keys()
30    EXPECTED = ['a', 'b', 'c', 'd', 'f', 'e', 'g', 'h', 'i']
31    actual = build_utils.GetSortedTransitiveDependencies(TOP, _DEPS.get)
32    self.assertEqual(EXPECTED, actual)
33
34  def testGetSortedTransitiveDependencies_leaves(self):
35    TOP = ['c', 'e', 'g', 'h', 'i']
36    EXPECTED = ['a', 'c', 'd', 'f', 'e', 'g', 'b', 'h', 'i']
37    actual = build_utils.GetSortedTransitiveDependencies(TOP, _DEPS.get)
38    self.assertEqual(EXPECTED, actual)
39
40  def testGetSortedTransitiveDependencies_leavesReverse(self):
41    TOP = ['i', 'h', 'g', 'e', 'c']
42    EXPECTED = ['a', 'd', 'f', 'i', 'b', 'h', 'g', 'e', 'c']
43    actual = build_utils.GetSortedTransitiveDependencies(TOP, _DEPS.get)
44    self.assertEqual(EXPECTED, actual)
45
46
47if __name__ == '__main__':
48  unittest.main()
49