summaryrefslogtreecommitdiffstats
path: root/test/util/graph.rb
blob: 875fd0ec305a6f4f5c9ea868ba872447705f93a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env ruby
#
#  Created by Luke Kanies on 2006-11-16.
#  Copyright (c) 2006. All rights reserved.

$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/

require 'puppettest'
require 'puppettest/graph'
require 'puppet/util/graph'

class TestUtilGraph < Test::Unit::TestCase
	include PuppetTest
	include PuppetTest::Graph
    
	def test_to_graph
	    children = %w{a b c d}
        list = Container.new("yay", children)
        
        graph = nil
        assert_nothing_raised do
            graph = list.to_graph
        end
        
        assert(graph.vertices.include?(list), "wtf?")
        
        ([list] + children).each do |thing|
            assert(graph.vertex?(thing), "%s is not a vertex" % thing)
        end
        children.each do |child|
            assert(graph.edge?(list, child),
                "%s/%s was not added as an edge" % ["yay", child])
        end
    end
    
    def test_recursive_to_graph
        one, two, three, middle, top = build_tree
        
        graph = nil
        assert_nothing_raised do
            graph = top.to_graph
        end
        
        (%w{a b c d e f g h} + [one, two, middle, top]).each do |v|
            assert(graph.vertex?(v), "%s is not a vertex" % v)
        end
        
        [one, two, middle, top].each do |con|
            con.each do |child|
                assert(graph.edge?(con, child), "%s/%s is not an edge" % [con, child])
            end
        end
        
        # Now make sure we correctly retrieve the leaves from each container
        {top => %w{a b c d e f g h i j},
         one => %w{a b},
         two => %w{c d},
         three => %w{i j},
         middle => %w{c d e f}}.each do |cont, list|
            leaves = nil
            assert_nothing_raised do
                leaves = graph.leaves(cont)
            end
            leaves = leaves.sort
            assert_equal(list.sort, leaves.sort,
                "Got incorrect leaf list for %s" % cont.name)
            %w{a b c d e f g h}.each do |letter|
                unless list.include?(letter)
                    assert(!leaves.include?(letter),
                        "incorrectly got %s as a leaf of %s" %
                            [letter, cont.to_s])
                end
            end
        end
    end
    
    def test_to_graph_with_block
        middle = Container.new "middle", ["c", "d", 3, 4]
        top = Container.new "top", ["a", "b", middle, 1, 2]
        
        graph = nil
        assert_nothing_raised() { 
            graph = top.to_graph { |c| c.is_a?(String) or c.is_a?(Container) }
        }
        
        %w{a b c d}.each do |child|
            assert(graph.vertex?(child), "%s was not added as a vertex" % child)
        end
        
        [1, 2, 3, 4].each do |child|
            assert(! graph.vertex?(child), "%s is a vertex" % child)
        end
    end

    def test_cyclic_graphs
        one = Container.new "one", %w{a b}
        two = Container.new "two", %w{c d}
        
        one.push(two)
        two.push(one)
        
        assert_raise(Puppet::Error, "did not fail on cyclic graph") do
            one.to_graph
        end
    end
end

# $Id$