github.com/anuaimi/terraform@v0.6.4-0.20150904235404-2bf9aec61da8/terraform/transform_vertex_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/dag"
     8  )
     9  
    10  func TestVertexTransformer_impl(t *testing.T) {
    11  	var _ GraphTransformer = new(VertexTransformer)
    12  }
    13  
    14  func TestVertexTransformer(t *testing.T) {
    15  	var g Graph
    16  	g.Add(1)
    17  	g.Add(2)
    18  	g.Add(3)
    19  	g.Connect(dag.BasicEdge(1, 2))
    20  	g.Connect(dag.BasicEdge(2, 3))
    21  
    22  	{
    23  		tf := &VertexTransformer{
    24  			Transforms: []GraphVertexTransformer{
    25  				&testVertexTransform{Source: 2, Target: 42},
    26  			},
    27  		}
    28  		if err := tf.Transform(&g); err != nil {
    29  			t.Fatalf("err: %s", err)
    30  		}
    31  	}
    32  
    33  	actual := strings.TrimSpace(g.String())
    34  	expected := strings.TrimSpace(testVertexTransformerStr)
    35  	if actual != expected {
    36  		t.Fatalf("bad: %s", actual)
    37  	}
    38  }
    39  
    40  type testVertexTransform struct {
    41  	Source, Target dag.Vertex
    42  }
    43  
    44  func (t *testVertexTransform) Transform(v dag.Vertex) (dag.Vertex, error) {
    45  	if t.Source == v {
    46  		v = t.Target
    47  	}
    48  
    49  	return v, nil
    50  }
    51  
    52  const testVertexTransformerStr = `
    53  1
    54    42
    55  3
    56  42
    57    3
    58  `