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:
Martin Atkins 2018-11-03 21:12:11 +00:00
parent 17ff23300f
commit 361186aad0

View File

@ -1,17 +1,13 @@
package hclwrite_test
import (
"bytes"
"strings"
"testing"
"fmt"
"github.com/hashicorp/hcl2/hclwrite"
"github.com/zclconf/go-cty/cty"
)
// TestRoundupCreate is a test that exercises a number of different codepaths
// to create a file from scratch, like a calling application might.
func TestRoundupCreate(t *testing.T) {
func Example_generateFromScratch() {
f := hclwrite.NewEmptyFile()
rootBody := f.Body()
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("baz", cty.ListValEmpty(cty.String))
got := string(bytes.TrimSpace(f.Bytes()))
want := strings.TrimSpace(`
string = "foo"
object = {bar = 5, baz = true, foo = "foo"}
bool = false
foo {
hello = "world"
}
empty {
}
bar "a" "b" {
baz {
foo = 10
beep = "boop"
baz = []
}
}
`)
if got != want {
t.Errorf("wrong result\ngot:\n%s\n\nwant:\n%s", got, want)
}
fmt.Printf("%s", f.Bytes())
// Output:
// string = "foo"
//
// object = {bar = 5, baz = true, foo = "foo"}
// bool = false
//
// foo {
// hello = "world"
// }
// empty {
// }
//
// bar "a" "b" {
// baz {
// foo = 10
// beep = "boop"
// baz = []
// }
// }
}