Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | # You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | from __future__ import unicode_literals |
michael@0 | 5 | |
michael@0 | 6 | import unittest |
michael@0 | 7 | |
michael@0 | 8 | from mozunit import ( |
michael@0 | 9 | main, |
michael@0 | 10 | ) |
michael@0 | 11 | |
michael@0 | 12 | from dumbmake.dumbmake import ( |
michael@0 | 13 | add_extra_dependencies, |
michael@0 | 14 | all_dependencies, |
michael@0 | 15 | dependency_map, |
michael@0 | 16 | indentation, |
michael@0 | 17 | ) |
michael@0 | 18 | |
michael@0 | 19 | class TestDumbmake(unittest.TestCase): |
michael@0 | 20 | def test_indentation(self): |
michael@0 | 21 | self.assertEqual(indentation(""), 0) |
michael@0 | 22 | self.assertEqual(indentation("x"), 0) |
michael@0 | 23 | self.assertEqual(indentation(" x"), 1) |
michael@0 | 24 | self.assertEqual(indentation("\tx"), 1) |
michael@0 | 25 | self.assertEqual(indentation(" \tx"), 2) |
michael@0 | 26 | self.assertEqual(indentation("\t x"), 2) |
michael@0 | 27 | self.assertEqual(indentation(" x "), 1) |
michael@0 | 28 | self.assertEqual(indentation("\tx\t"), 1) |
michael@0 | 29 | self.assertEqual(indentation(" x"), 2) |
michael@0 | 30 | self.assertEqual(indentation(" x"), 4) |
michael@0 | 31 | |
michael@0 | 32 | def test_dependency_map(self): |
michael@0 | 33 | self.assertEqual(dependency_map([]), {}) |
michael@0 | 34 | self.assertEqual(dependency_map(["a"]), {"a": []}) |
michael@0 | 35 | self.assertEqual(dependency_map(["a", "b"]), {"a": [], "b": []}) |
michael@0 | 36 | self.assertEqual(dependency_map(["a", "b", "c"]), {"a": [], "b": [], "c": []}) |
michael@0 | 37 | # indentation |
michael@0 | 38 | self.assertEqual(dependency_map(["a", "\tb", "a", "\tc"]), {"a": [], "b": ["a"], "c": ["a"]}) |
michael@0 | 39 | self.assertEqual(dependency_map(["a", "\tb", "\t\tc"]), {"a": [], "b": ["a"], "c": ["b", "a"]}) |
michael@0 | 40 | self.assertEqual(dependency_map(["a", "\tb", "\t\tc", "\td", "\te", "f"]), {"a": [], "b": ["a"], "c": ["b", "a"], "d": ["a"], "e": ["a"], "f": []}) |
michael@0 | 41 | # irregular indentation |
michael@0 | 42 | self.assertEqual(dependency_map(["\ta", "b"]), {"a": [], "b": []}) |
michael@0 | 43 | self.assertEqual(dependency_map(["a", "\t\t\tb", "\t\tc"]), {"a": [], "b": ["a"], "c": ["a"]}) |
michael@0 | 44 | self.assertEqual(dependency_map(["a", "\t\tb", "\t\t\tc", "\t\td", "\te", "f"]), {"a": [], "b": ["a"], "c": ["b", "a"], "d": ["a"], "e": ["a"], "f": []}) |
michael@0 | 45 | # repetitions |
michael@0 | 46 | self.assertEqual(dependency_map(["a", "\tb", "a", "\tb"]), {"a": [], "b": ["a"]}) |
michael@0 | 47 | self.assertEqual(dependency_map(["a", "\tb", "\t\tc", "b", "\td", "\t\te"]), {"a": [], "b": ["a"], "d": ["b"], "e": ["d", "b"], "c": ["b", "a"]}) |
michael@0 | 48 | # cycles are okay |
michael@0 | 49 | self.assertEqual(dependency_map(["a", "\tb", "\t\ta"]), {"a": ["b", "a"], "b": ["a"]}) |
michael@0 | 50 | |
michael@0 | 51 | def test_all_dependencies(self): |
michael@0 | 52 | dm = {"a": [], "b": ["a"], "c": ["b", "a"], "d": ["a"], "e": ["a"], "f": []} |
michael@0 | 53 | self.assertEqual(all_dependencies("a", dependency_map=dm), []) |
michael@0 | 54 | self.assertEqual(all_dependencies("b", dependency_map=dm), ["a"]) |
michael@0 | 55 | self.assertEqual(all_dependencies("c", "a", "b", dependency_map=dm), ["b", "a"]) |
michael@0 | 56 | self.assertEqual(all_dependencies("d", dependency_map=dm), ["a"]) |
michael@0 | 57 | self.assertEqual(all_dependencies("d", "f", "c", dependency_map=dm), ["b", "a"]) |
michael@0 | 58 | self.assertEqual(all_dependencies("a", "b", dependency_map=dm), ["a"]) |
michael@0 | 59 | self.assertEqual(all_dependencies("b", "b", dependency_map=dm), ["a"]) |
michael@0 | 60 | |
michael@0 | 61 | def test_missing_entry(self): |
michael@0 | 62 | # a depends on b, which is missing |
michael@0 | 63 | dm = {"a": ["b"]} |
michael@0 | 64 | self.assertEqual(all_dependencies("a", dependency_map=dm), ["b"]) |
michael@0 | 65 | self.assertEqual(all_dependencies("a", "b", dependency_map=dm), ["b"]) |
michael@0 | 66 | self.assertEqual(all_dependencies("b", dependency_map=dm), []) |
michael@0 | 67 | |
michael@0 | 68 | def test_two_dependencies(self): |
michael@0 | 69 | dm = {"a": ["c"], "b": ["c"], "c": []} |
michael@0 | 70 | # suppose a and b both depend on c. Then we want to build a and b before c... |
michael@0 | 71 | self.assertEqual(all_dependencies("a", "b", dependency_map=dm), ["c"]) |
michael@0 | 72 | # ... but relative order is preserved. |
michael@0 | 73 | self.assertEqual(all_dependencies("b", "a", dependency_map=dm), ["c"]) |
michael@0 | 74 | |
michael@0 | 75 | def test_nested_dependencies(self): |
michael@0 | 76 | # a depends on b depends on c depends on d |
michael@0 | 77 | dm = {"a": ["b", "c", "d"], "b": ["c", "d"], "c": ["d"]} |
michael@0 | 78 | self.assertEqual(all_dependencies("b", "a", dependency_map=dm), ["b", "c", "d"]) |
michael@0 | 79 | self.assertEqual(all_dependencies("c", "a", dependency_map=dm), ["b", "c", "d"]) |
michael@0 | 80 | |
michael@0 | 81 | def test_add_extra_dependencies(self): |
michael@0 | 82 | # a depends on b depends on c depends on d |
michael@0 | 83 | dm = {"a": ["b", "c", "d"], "b": ["c", "d"], "c": ["d"]} |
michael@0 | 84 | # Edge cases. |
michael@0 | 85 | self.assertEqual(list(add_extra_dependencies([], dependency_map=dm)), |
michael@0 | 86 | []) |
michael@0 | 87 | self.assertEqual(list(add_extra_dependencies([(None, "package")], dependency_map=dm)), |
michael@0 | 88 | [(None, "package")]) |
michael@0 | 89 | # Easy expansion. |
michael@0 | 90 | self.assertEqual(list(add_extra_dependencies([("b", None)], dependency_map=dm)), |
michael@0 | 91 | [("b", None), ("c", None), ("d", None)]) |
michael@0 | 92 | # Expansion with two groups -- each group is handled independently. |
michael@0 | 93 | self.assertEqual(list(add_extra_dependencies([("b", None), |
michael@0 | 94 | (None, "package"), |
michael@0 | 95 | ("c", None)], dependency_map=dm)), |
michael@0 | 96 | [("b", None), (None, "package"), |
michael@0 | 97 | ("c", None), ("d", None)]) |
michael@0 | 98 | # Two groups, no duplicate dependencies in each group. |
michael@0 | 99 | self.assertEqual(list(add_extra_dependencies([("a", None), ("b", None), |
michael@0 | 100 | (None, "package"), (None, "install"), |
michael@0 | 101 | ("c", None), ("d", None)], dependency_map=dm)), |
michael@0 | 102 | [("a", None), ("b", None), (None, "package"), |
michael@0 | 103 | (None, "install"), ("c", None), ("d", None)]) |
michael@0 | 104 | |
michael@0 | 105 | if __name__ == '__main__': |
michael@0 | 106 | main() |