| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
import unittest |
| 18 |
|
| 19 |
from cylc.flow.c3mro import C3 |
| 20 |
|
| 21 |
|
| 22 |
class TestC3mro(unittest.TestCase): |
| 23 |
|
| 24 |
def test_tree_is_empty_by_default(self): |
| 25 |
c3 = C3() |
| 26 |
self.assertFalse(c3.tree) |
| 27 |
|
| 28 |
def test_tree_parameter(self): |
| 29 |
c3 = C3({'a': 1}) |
| 30 |
self.assertTrue(c3.tree) |
| 31 |
|
| 32 |
def test_simple_inheritance(self): |
| 33 |
parents = {} |
| 34 |
parents['object'] = [] |
| 35 |
parents['string'] = ['object'] |
| 36 |
c3 = C3(parents) |
| 37 |
string_hierarchy = c3.mro('string') |
| 38 |
self.assertEqual(['string', 'object'], string_hierarchy) |
| 39 |
|
| 40 |
def test_simple_inheritance_extra_nodes(self): |
| 41 |
parents = {} |
| 42 |
parents['object'] = [] |
| 43 |
parents['string'] = ['object'] |
| 44 |
# nodes not related to string |
| 45 |
parents['root'] = [] |
| 46 |
parents['diamond'] = ['root'] |
| 47 |
c3 = C3(parents) |
| 48 |
self.assertEqual(['string', 'object'], c3.mro('string')) |
| 49 |
self.assertEqual(['diamond', 'root'], c3.mro('diamond')) |
| 50 |
|
| 51 |
def test_empty_tree_key_error(self): |
| 52 |
parents = {} |
| 53 |
c3 = C3(parents) |
| 54 |
with self.assertRaises(KeyError): |
| 55 |
c3.mro('test') |
| 56 |
|
| 57 |
def test_multiple_inheritance_error_py23(self): |
| 58 |
parents = {} |
| 59 |
parents['object'] = [] |
| 60 |
parents['x'] = ['object'] |
| 61 |
parents['y'] = ['object'] |
| 62 |
parents['a'] = ['x', 'y'] |
| 63 |
parents['b'] = ['y', 'x'] |
| 64 |
parents['z'] = ['a', 'b'] |
| 65 |
c3 = C3(parents) |
| 66 |
# see class docstring, this is the case #2 |
| 67 |
with self.assertRaises(Exception) as cm: |
| 68 |
c3.mro('z') |
| 69 |
self.assertTrue("ERROR: z: bad runtime namespace inheritance hierarchy" |
| 70 |
in str(cm.exception)) |
| 71 |
|
| 72 |
def test_mro_of_none(self): |
| 73 |
with self.assertRaises(Exception) as cm: |
| 74 |
C3.merge([[], ['x', 'y', 'o'], ['y', 'x', 'o'], []], None) |
| 75 |
self.assertTrue("ERROR: bad runtime namespace inheritance hierarchy" |
| 76 |
in str(cm.exception)) |