3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
19 from cmframework.utils.cmdependencysort import CMDependencySort as Sort
20 from cmframework.apis.cmerror import CMError
23 class CMDependencySortTest(unittest.TestCase):
26 sorted_list = sort.sort()
27 assert sorted_list == []
29 def test_one_no_deps(self):
30 sort = Sort({'a': []})
31 sorted_list = sort.sort()
32 assert sorted_list == ['a']
34 sort = Sort(None, {'a': []})
35 sorted_list = sort.sort()
36 assert sorted_list == ['a']
38 def test_both_no_deps(self):
39 sort = Sort({'a': []}, {'a': []})
40 sorted_list = sort.sort()
41 assert sorted_list == ['a']
43 sort = Sort({'a': [], 'b': []}, {'a': [], 'b': []})
44 sorted_list = sort.sort()
46 assert len(sorted_list) == 2
47 assert 'a' in sorted_list
48 assert 'b' in sorted_list
51 def _assert_orderings(after, before, sorted_list):
52 for entry, deps in after.iteritems():
54 assert sorted_list.index(entry) > sorted_list.index(dep)
56 for entry, deps in before.iteritems():
58 assert sorted_list.index(entry) < sorted_list.index(dep)
60 def test_mandbc(self):
61 after = {'a': ['m'], 'b': ['d']}
62 before = {'a': ['b', 'c', 'd', 'n'], 'b': ['c']}
63 sort = Sort(after, before)
64 sorted_list = sort.sort()
66 CMDependencySortTest._assert_orderings(after, before, sorted_list)
68 def test_simple_only_after(self):
69 after = {'a': ['b'], 'b': ['c']}
72 sorted_list = sort.sort()
74 CMDependencySortTest._assert_orderings(after, before, sorted_list)
76 def test_simple_only_before(self):
78 before = {'a': ['b'], 'b': ['c']}
79 sort = Sort(None, before)
80 sorted_list = sort.sort()
82 CMDependencySortTest._assert_orderings(after, before, sorted_list)
84 def test_simple(self):
85 after = {'b': ['a'], 'c': ['b']}
86 before = {'a': ['b'], 'b': ['c']}
87 sort = Sort(after, before)
88 sorted_list = sort.sort()
90 CMDependencySortTest._assert_orderings(after, before, sorted_list)
92 def test_cycle_before_after(self):
95 sort = Sort(after, before)
97 with self.assertRaises(CMError) as context:
98 sorted_list = sort.sort()
100 def test_cycle_only_before(self):
102 before = {'b': ['a'], 'a': ['b']}
103 sort = Sort(after, before)
105 with self.assertRaises(CMError) as context:
106 sorted_list = sort.sort()
108 def test_cycle_only_after(self):
109 after = {'b': ['a'], 'a': ['b']}
111 sort = Sort(after, before)
113 with self.assertRaises(CMError) as context:
114 sorted_list = sort.sort()
116 if __name__ == '__main__':