def test_does_not_fire_exception_if_no_cycles_present (self): graph_with_no_cycles={} graph_with_no_cycles['foo']=['bar'] graph_with_no_cycles['a']=['b'] graph_with_no_cycles['baz']=['bar'] graph_with_no_cycles['hello']=['foo'] graphTest = TokenCycleChecking(graph_with_no_cycles) graphTest.assert_no_cycles_present()
def test_should_not_raise_exception_if_no_cycles_present(self): graph_with_no_cycles = {'foo': ['bar'], 'a': ['b'], 'baz': ['bar'], 'hello': ['foo']} actual_graph = TokenCycleChecking(graph_with_no_cycles) actual_graph.assert_no_cycles_present()
def _replace_tokens_in_token_values(self, token_values): tokens_without_sub_tokens = dict( (key, value) for (key, value) in token_values.iteritems() if not TokenReplacer.TOKEN_PATTERN.search(value)) tokens_with_sub_tokens = dict( (key, value) for (key, value) in token_values.iteritems() if TokenReplacer.TOKEN_PATTERN.search(value)) while tokens_with_sub_tokens: tokens_with_sub_tokens_after_replace = {} replace_count = 0 for (key, value) in tokens_with_sub_tokens.iteritems(): token_names = TokenReplacer.TOKEN_PATTERN.findall(value) for token_name in token_names: if token_name in tokens_without_sub_tokens: value = value.replace( "@@@%s@@@" % token_name, tokens_without_sub_tokens[token_name]) replace_count += 1 if TokenReplacer.TOKEN_PATTERN.search(value): tokens_with_sub_tokens_after_replace[key] = value else: tokens_without_sub_tokens[key] = value # there are still invalid tokens and we could not replace any of them in the last loop cycle, so let's throw an error if tokens_with_sub_tokens_after_replace and not replace_count: # maybe there is a cycle? dependency_digraph = {} for (variable, variable_contents ) in tokens_with_sub_tokens_after_replace.iteritems(): edge_source = variable edge_target = TokenReplacer.TOKEN_PATTERN.findall( variable_contents) dependency_digraph[edge_source] = edge_target token_graph = TokenCycleChecking(dependency_digraph) token_graph.assert_no_cycles_present() # no cycle => variable undefined unreplaced_variables = [] for (variable, variable_contents ) in tokens_with_sub_tokens_after_replace.iteritems(): unreplaced = TokenReplacer.TOKEN_PATTERN.findall( variable_contents) unreplaced_variables.append(unreplaced) raise MissingOrRedundantTokenException( "Unresolved variables :\n" + str(unreplaced_variables)) tokens_with_sub_tokens = tokens_with_sub_tokens_after_replace return tokens_without_sub_tokens
def test_should_recognize_simple_cycle(self): graph_with_cycle = {'foo': ['bar'], 'bar': ['foo']} actual_graph = TokenCycleChecking(graph_with_cycle) self.assertRaises(ContainsCyclesException, actual_graph.assert_no_cycles_present)
def _replace_tokens_in_token_values(self, token_values): tokens_without_sub_tokens = dict((key, value) for (key, value) in token_values.iteritems() if not TokenReplacer.TOKEN_PATTERN.search(value)) tokens_with_sub_tokens = dict((key, value) for (key, value) in token_values.iteritems() if TokenReplacer.TOKEN_PATTERN.search(value)) while tokens_with_sub_tokens: tokens_with_sub_tokens_after_replace = {} replace_count = 0 for (key, value) in tokens_with_sub_tokens.iteritems(): token_names = TokenReplacer.TOKEN_PATTERN.findall(value) for token_name in token_names: if token_name in tokens_without_sub_tokens: value = value.replace("@@@%s@@@" % token_name, tokens_without_sub_tokens[token_name]) replace_count += 1 if TokenReplacer.TOKEN_PATTERN.search(value): tokens_with_sub_tokens_after_replace[key] = value else: tokens_without_sub_tokens[key] = value # there are still invalid tokens and we could not replace any of them in the last loop cycle, so let's throw an error if tokens_with_sub_tokens_after_replace and not replace_count: # maybe there is a cycle? dependency_digraph = {} for (variable, variable_contents) in tokens_with_sub_tokens_after_replace.iteritems(): edge_source = variable edge_target = TokenReplacer.TOKEN_PATTERN.findall(variable_contents) dependency_digraph[edge_source] = edge_target token_graph = TokenCycleChecking(dependency_digraph) token_graph.assert_no_cycles_present() # no cycle => variable undefined unreplaced_variables = [] for(variable, variable_contents) in tokens_with_sub_tokens_after_replace.iteritems(): unreplaced = TokenReplacer.TOKEN_PATTERN.findall(variable_contents) unreplaced_variables.append(unreplaced) raise MissingOrRedundantTokenException("Unresolved variables :\n" + str(unreplaced_variables)) tokens_with_sub_tokens = tokens_with_sub_tokens_after_replace return tokens_without_sub_tokens
def test_should_not_raise_exception_if_graph_empty(self): empty_graph = {} actual_graph = TokenCycleChecking(empty_graph) actual_graph.assert_no_cycles_present()
def test_does_not_fire_exception_if_graph_empty (self): empty_graph={} graphTest = TokenCycleChecking(empty_graph) graphTest.assert_no_cycles_present()