github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/dag/graph_test.go (about)

     1  package dag
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestGraph_empty(t *testing.T) {
    10  	var g Graph
    11  	g.Add(1)
    12  	g.Add(2)
    13  	g.Add(3)
    14  
    15  	actual := strings.TrimSpace(g.String())
    16  	expected := strings.TrimSpace(testGraphEmptyStr)
    17  	if actual != expected {
    18  		t.Fatalf("bad: %s", actual)
    19  	}
    20  }
    21  
    22  func TestGraph_basic(t *testing.T) {
    23  	var g Graph
    24  	g.Add(1)
    25  	g.Add(2)
    26  	g.Add(3)
    27  	g.Connect(BasicEdge(1, 3))
    28  
    29  	actual := strings.TrimSpace(g.String())
    30  	expected := strings.TrimSpace(testGraphBasicStr)
    31  	if actual != expected {
    32  		t.Fatalf("bad: %s", actual)
    33  	}
    34  }
    35  
    36  func TestGraph_remove(t *testing.T) {
    37  	var g Graph
    38  	g.Add(1)
    39  	g.Add(2)
    40  	g.Add(3)
    41  	g.Connect(BasicEdge(1, 3))
    42  	g.Remove(3)
    43  
    44  	actual := strings.TrimSpace(g.String())
    45  	expected := strings.TrimSpace(testGraphRemoveStr)
    46  	if actual != expected {
    47  		t.Fatalf("bad: %s", actual)
    48  	}
    49  }
    50  
    51  func TestGraph_replace(t *testing.T) {
    52  	var g Graph
    53  	g.Add(1)
    54  	g.Add(2)
    55  	g.Add(3)
    56  	g.Connect(BasicEdge(1, 2))
    57  	g.Connect(BasicEdge(2, 3))
    58  	g.Replace(2, 42)
    59  
    60  	actual := strings.TrimSpace(g.String())
    61  	expected := strings.TrimSpace(testGraphReplaceStr)
    62  	if actual != expected {
    63  		t.Fatalf("bad: %s", actual)
    64  	}
    65  }
    66  
    67  func TestGraph_replaceSelf(t *testing.T) {
    68  	var g Graph
    69  	g.Add(1)
    70  	g.Add(2)
    71  	g.Add(3)
    72  	g.Connect(BasicEdge(1, 2))
    73  	g.Connect(BasicEdge(2, 3))
    74  	g.Replace(2, 2)
    75  
    76  	actual := strings.TrimSpace(g.String())
    77  	expected := strings.TrimSpace(testGraphReplaceSelfStr)
    78  	if actual != expected {
    79  		t.Fatalf("bad: %s", actual)
    80  	}
    81  }
    82  
    83  // This tests that connecting edges works based on custom Hashcode
    84  // implementations for uniqueness.
    85  func TestGraph_hashcode(t *testing.T) {
    86  	var g Graph
    87  	g.Add(&hashVertex{code: 1})
    88  	g.Add(&hashVertex{code: 2})
    89  	g.Add(&hashVertex{code: 3})
    90  	g.Connect(BasicEdge(
    91  		&hashVertex{code: 1},
    92  		&hashVertex{code: 3}))
    93  
    94  	actual := strings.TrimSpace(g.String())
    95  	expected := strings.TrimSpace(testGraphBasicStr)
    96  	if actual != expected {
    97  		t.Fatalf("bad: %s", actual)
    98  	}
    99  }
   100  
   101  type hashVertex struct {
   102  	code interface{}
   103  }
   104  
   105  func (v *hashVertex) Hashcode() interface{} {
   106  	return v.code
   107  }
   108  
   109  func (v *hashVertex) Name() string {
   110  	return fmt.Sprintf("%#v", v.code)
   111  }
   112  
   113  const testGraphBasicStr = `
   114  1
   115    3
   116  2
   117  3
   118  `
   119  
   120  const testGraphEmptyStr = `
   121  1
   122  2
   123  3
   124  `
   125  
   126  const testGraphRemoveStr = `
   127  1
   128  2
   129  `
   130  
   131  const testGraphReplaceStr = `
   132  1
   133    42
   134  3
   135  42
   136    3
   137  `
   138  
   139  const testGraphReplaceSelfStr = `
   140  1
   141    2
   142  2
   143    3
   144  3
   145  `