depstree_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2009 The Closure Library Authors. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS-IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Unit test for depstree."""
  17. __author__ = 'nnaze@google.com (Nathan Naze)'
  18. import unittest
  19. import depstree
  20. def _GetProvides(sources):
  21. """Get all namespaces provided by a collection of sources."""
  22. provides = set()
  23. for source in sources:
  24. provides.update(source.provides)
  25. return provides
  26. class MockSource(object):
  27. """Mock Source file."""
  28. def __init__(self, provides, requires):
  29. self.provides = set(provides)
  30. self.requires = set(requires)
  31. def __repr__(self):
  32. return 'MockSource %s' % self.provides
  33. class DepsTreeTestCase(unittest.TestCase):
  34. """Unit test for DepsTree. Tests several common situations and errors."""
  35. def AssertValidDependencies(self, deps_list):
  36. """Validates a dependency list.
  37. Asserts that a dependency list is valid: For every source in the list,
  38. ensure that every require is provided by a source earlier in the list.
  39. Args:
  40. deps_list: A list of sources that should be in dependency order.
  41. """
  42. for i in range(len(deps_list)):
  43. source = deps_list[i]
  44. previous_provides = _GetProvides(deps_list[:i])
  45. for require in source.requires:
  46. self.assertTrue(
  47. require in previous_provides,
  48. 'Namespace "%s" not provided before required by %s' % (
  49. require, source))
  50. def testSimpleDepsTree(self):
  51. a = MockSource(['A'], ['B', 'C'])
  52. b = MockSource(['B'], [])
  53. c = MockSource(['C'], ['D'])
  54. d = MockSource(['D'], ['E'])
  55. e = MockSource(['E'], [])
  56. tree = depstree.DepsTree([a, b, c, d, e])
  57. self.AssertValidDependencies(tree.GetDependencies('A'))
  58. self.AssertValidDependencies(tree.GetDependencies('B'))
  59. self.AssertValidDependencies(tree.GetDependencies('C'))
  60. self.AssertValidDependencies(tree.GetDependencies('D'))
  61. self.AssertValidDependencies(tree.GetDependencies('E'))
  62. def testCircularDependency(self):
  63. # Circular deps
  64. a = MockSource(['A'], ['B'])
  65. b = MockSource(['B'], ['C'])
  66. c = MockSource(['C'], ['A'])
  67. tree = depstree.DepsTree([a, b, c])
  68. self.assertRaises(depstree.CircularDependencyError,
  69. tree.GetDependencies, 'A')
  70. def testRequiresUndefinedNamespace(self):
  71. a = MockSource(['A'], ['B'])
  72. b = MockSource(['B'], ['C'])
  73. c = MockSource(['C'], ['D']) # But there is no D.
  74. def MakeDepsTree():
  75. return depstree.DepsTree([a, b, c])
  76. self.assertRaises(depstree.NamespaceNotFoundError, MakeDepsTree)
  77. def testDepsForMissingNamespace(self):
  78. a = MockSource(['A'], ['B'])
  79. b = MockSource(['B'], [])
  80. tree = depstree.DepsTree([a, b])
  81. # There is no C.
  82. self.assertRaises(depstree.NamespaceNotFoundError,
  83. tree.GetDependencies, 'C')
  84. def testMultipleRequires(self):
  85. a = MockSource(['A'], ['B'])
  86. b = MockSource(['B'], ['C'])
  87. c = MockSource(['C'], [])
  88. d = MockSource(['D'], ['B'])
  89. tree = depstree.DepsTree([a, b, c, d])
  90. self.AssertValidDependencies(tree.GetDependencies(['D', 'A']))
  91. if __name__ == '__main__':
  92. unittest.main()