hclwrite: Recast TestRoundupCreate as an example
Since the goal of this is to be somewhat realistic of what a caller might do, we might as well also include it in the godoc examples.
This commit is contained in:
parent
17ff23300f
commit
361186aad0
@ -1,17 +1,13 @@
|
|||||||
package hclwrite_test
|
package hclwrite_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/hashicorp/hcl2/hclwrite"
|
"github.com/hashicorp/hcl2/hclwrite"
|
||||||
"github.com/zclconf/go-cty/cty"
|
"github.com/zclconf/go-cty/cty"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestRoundupCreate is a test that exercises a number of different codepaths
|
func Example_generateFromScratch() {
|
||||||
// to create a file from scratch, like a calling application might.
|
|
||||||
func TestRoundupCreate(t *testing.T) {
|
|
||||||
f := hclwrite.NewEmptyFile()
|
f := hclwrite.NewEmptyFile()
|
||||||
rootBody := f.Body()
|
rootBody := f.Body()
|
||||||
rootBody.SetAttributeValue("string", cty.StringVal("bar")) // this is overwritten later
|
rootBody.SetAttributeValue("string", cty.StringVal("bar")) // this is overwritten later
|
||||||
@ -39,28 +35,24 @@ func TestRoundupCreate(t *testing.T) {
|
|||||||
bazBody.SetAttributeValue("beep", cty.StringVal("boop"))
|
bazBody.SetAttributeValue("beep", cty.StringVal("boop"))
|
||||||
bazBody.SetAttributeValue("baz", cty.ListValEmpty(cty.String))
|
bazBody.SetAttributeValue("baz", cty.ListValEmpty(cty.String))
|
||||||
|
|
||||||
got := string(bytes.TrimSpace(f.Bytes()))
|
fmt.Printf("%s", f.Bytes())
|
||||||
want := strings.TrimSpace(`
|
// Output:
|
||||||
string = "foo"
|
// string = "foo"
|
||||||
|
//
|
||||||
object = {bar = 5, baz = true, foo = "foo"}
|
// object = {bar = 5, baz = true, foo = "foo"}
|
||||||
bool = false
|
// bool = false
|
||||||
|
//
|
||||||
foo {
|
// foo {
|
||||||
hello = "world"
|
// hello = "world"
|
||||||
}
|
// }
|
||||||
empty {
|
// empty {
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
bar "a" "b" {
|
// bar "a" "b" {
|
||||||
baz {
|
// baz {
|
||||||
foo = 10
|
// foo = 10
|
||||||
beep = "boop"
|
// beep = "boop"
|
||||||
baz = []
|
// baz = []
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
`)
|
|
||||||
if got != want {
|
|
||||||
t.Errorf("wrong result\ngot:\n%s\n\nwant:\n%s", got, want)
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user