2015-02-08 01:02:45 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExpandTransform_impl(t *testing.T) {
|
|
|
|
var _ GraphVertexTransformer = new(ExpandTransform)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpandTransform(t *testing.T) {
|
|
|
|
var g Graph
|
|
|
|
g.Add(1)
|
|
|
|
g.Add(2)
|
|
|
|
g.Connect(dag.BasicEdge(1, 2))
|
|
|
|
|
|
|
|
tf := &ExpandTransform{}
|
|
|
|
out, err := tf.Transform(&testExpandable{
|
|
|
|
Result: &g,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sn, ok := out.(GraphNodeSubgraph)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("not subgraph: %#v", out)
|
|
|
|
}
|
|
|
|
|
2016-11-09 22:52:22 +01:00
|
|
|
actual := strings.TrimSpace(sn.Subgraph().(*Graph).String())
|
2015-02-08 01:02:45 +01:00
|
|
|
expected := strings.TrimSpace(testExpandTransformStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpandTransform_nonExpandable(t *testing.T) {
|
|
|
|
tf := &ExpandTransform{}
|
|
|
|
out, err := tf.Transform(42)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if out != 42 {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testExpandable struct {
|
|
|
|
// Inputs
|
|
|
|
Result *Graph
|
|
|
|
ResultError error
|
|
|
|
|
|
|
|
// Outputs
|
|
|
|
Builder GraphBuilder
|
|
|
|
}
|
|
|
|
|
2015-02-12 02:07:38 +01:00
|
|
|
func (n *testExpandable) Expand(b GraphBuilder) (GraphNodeSubgraph, error) {
|
2015-02-08 01:02:45 +01:00
|
|
|
n.Builder = b
|
2015-02-12 02:07:38 +01:00
|
|
|
return &testSubgraph{n.Result}, n.ResultError
|
|
|
|
}
|
|
|
|
|
|
|
|
type testSubgraph struct {
|
|
|
|
Graph *Graph
|
|
|
|
}
|
|
|
|
|
2016-11-09 22:52:22 +01:00
|
|
|
func (n *testSubgraph) Subgraph() dag.Grapher {
|
2015-02-12 02:07:38 +01:00
|
|
|
return n.Graph
|
2015-02-08 01:02:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const testExpandTransformStr = `
|
|
|
|
1
|
|
|
|
2
|
|
|
|
2
|
|
|
|
`
|