From 07079cbe73f71a1ea764331c99d67419b411e797 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Sun, 4 Aug 2019 10:05:14 +0200 Subject: [PATCH] Add simple tests for DependencySolver --- .../test_dependency_solver.py | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tracetools_analysis/test/tracetools_analysis/test_dependency_solver.py diff --git a/tracetools_analysis/test/tracetools_analysis/test_dependency_solver.py b/tracetools_analysis/test/tracetools_analysis/test_dependency_solver.py new file mode 100644 index 0000000..d5fff84 --- /dev/null +++ b/tracetools_analysis/test/tracetools_analysis/test_dependency_solver.py @@ -0,0 +1,107 @@ +# Copyright 2019 Open Source Robotics Foundation, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +from tracetools_analysis.processor import Dependant +from tracetools_analysis.processor import DependencySolver + + +class DepEmtpy(Dependant): + pass + + +class DepOne(Dependant): + + @staticmethod + def dependencies(): + return [DepEmtpy] + + +class DepOne2(Dependant): + + @staticmethod + def dependencies(): + return [DepEmtpy] + +class DepTwo(Dependant): + + @staticmethod + def dependencies(): + return [DepOne, DepOne2] + + +class TestDependencySolver(unittest.TestCase): + + def __init__(self, *args) -> None: + super().__init__( + *args, + ) + + def test_single_dep(self) -> None: + depone_instance = DepOne() + + # DepEmtpy should be added before + solution = DependencySolver([depone_instance]).solve() + self.assertEqual(len(solution), 2, 'solution length invalid') + self.assertIsInstance(solution[0], DepEmtpy) + self.assertIs(solution[1], depone_instance) + + def test_single_dep_existing(self) -> None: + depempty_instance = DepEmtpy() + depone_instance = DepOne() + + # Already in order + solution = DependencySolver([depempty_instance, depone_instance]).solve() + self.assertEqual(len(solution), 2, 'solution length invalid') + self.assertIs(solution[0], depempty_instance, 'wrong solution order') + self.assertIs(solution[1], depone_instance, 'wrong solution order') + + # Out of order + solution = DependencySolver([depone_instance, depempty_instance]).solve() + self.assertEqual(len(solution), 2, 'solution length invalid') + self.assertIs(solution[0], depempty_instance, 'solution does not use existing instance') + self.assertIs(solution[1], depone_instance, 'solution does not use existing instance') + + def test_duplicate_dependency(self) -> None: + deptwo_instance = DepTwo() + + # DepOne and DepOne2 both depend on DepEmpty + solution = DependencySolver([deptwo_instance]).solve() + self.assertEqual(len(solution), 4, 'solution length invalid') + self.assertIsInstance(solution[0], DepEmtpy) + self.assertIsInstance(solution[1], DepOne) + self.assertIsInstance(solution[2], DepOne2) + self.assertIs(solution[3], deptwo_instance) + + # Existing instance of DepEmpty, in order + depempty_instance = DepEmtpy() + solution = DependencySolver([depempty_instance, deptwo_instance]).solve() + self.assertEqual(len(solution), 4, 'solution length invalid') + self.assertIsInstance(solution[0], DepEmtpy) + self.assertIsInstance(solution[1], DepOne) + self.assertIsInstance(solution[2], DepOne2) + self.assertIs(solution[3], deptwo_instance) + + # Existing instance of DepEmpty, not in order + solution = DependencySolver([deptwo_instance, depempty_instance]).solve() + self.assertEqual(len(solution), 4, 'solution length invalid') + self.assertIsInstance(solution[0], DepEmtpy) + self.assertIsInstance(solution[1], DepOne) + self.assertIsInstance(solution[2], DepOne2) + self.assertIs(solution[3], deptwo_instance) + + +if __name__ == '__main__': + unittest.main()