From 361186aad0d06ab9874f4f3ffbbb9011dc1a06f0 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sat, 3 Nov 2018 21:12:11 +0000 Subject: [PATCH] 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. --- .../{roundup_test.go => examples_test.go} | 52 ++++++++----------- 1 file changed, 22 insertions(+), 30 deletions(-) rename hclwrite/{roundup_test.go => examples_test.go} (66%) diff --git a/hclwrite/roundup_test.go b/hclwrite/examples_test.go similarity index 66% rename from hclwrite/roundup_test.go rename to hclwrite/examples_test.go index 31543d1..170572f 100644 --- a/hclwrite/roundup_test.go +++ b/hclwrite/examples_test.go @@ -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 = [] + // } + // } }