terraform: ReferenceTransform test

This commit is contained in:
Mitchell Hashimoto 2016-09-15 18:41:09 -07:00
parent 21888b1227
commit ba51295267
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
1 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,97 @@
package terraform
import (
"strings"
"testing"
)
func TestReferenceTransformer_simple(t *testing.T) {
g := Graph{Path: RootModulePath}
g.Add(&graphNodeRefParentTest{
NameValue: "A",
Names: []string{"A"},
})
g.Add(&graphNodeRefChildTest{
NameValue: "B",
Refs: []string{"A"},
})
tf := &ReferenceTransformer{}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformRefBasicStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
func TestReferenceTransformer_path(t *testing.T) {
g := Graph{Path: RootModulePath}
g.Add(&graphNodeRefParentTest{
NameValue: "A",
Names: []string{"A"},
})
g.Add(&graphNodeRefChildTest{
NameValue: "B",
Refs: []string{"A"},
})
g.Add(&graphNodeRefParentTest{
NameValue: "child.A",
PathValue: []string{"root", "child"},
Names: []string{"A"},
})
g.Add(&graphNodeRefChildTest{
NameValue: "child.B",
PathValue: []string{"root", "child"},
Refs: []string{"A"},
})
tf := &ReferenceTransformer{}
if err := tf.Transform(&g); err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(g.String())
expected := strings.TrimSpace(testTransformRefPathStr)
if actual != expected {
t.Fatalf("bad:\n\n%s", actual)
}
}
type graphNodeRefParentTest struct {
NameValue string
PathValue []string
Names []string
}
func (n *graphNodeRefParentTest) Name() string { return n.NameValue }
func (n *graphNodeRefParentTest) ReferenceableName() []string { return n.Names }
func (n *graphNodeRefParentTest) Path() []string { return n.PathValue }
type graphNodeRefChildTest struct {
NameValue string
PathValue []string
Refs []string
}
func (n *graphNodeRefChildTest) Name() string { return n.NameValue }
func (n *graphNodeRefChildTest) References() []string { return n.Refs }
func (n *graphNodeRefChildTest) Path() []string { return n.PathValue }
const testTransformRefBasicStr = `
A
B
A
`
const testTransformRefPathStr = `
A
B
A
child.A
child.B
child.A
`