From 361186aad0d06ab9874f4f3ffbbb9011dc1a06f0 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sat, 3 Nov 2018 21:12:11 +0000 Subject: 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. --- hclwrite/examples_test.go | 58 +++++++++++++++++++++++++++++++++++++++++ hclwrite/roundup_test.go | 66 ----------------------------------------------- 2 files changed, 58 insertions(+), 66 deletions(-) create mode 100644 hclwrite/examples_test.go delete mode 100644 hclwrite/roundup_test.go (limited to 'hclwrite') diff --git a/hclwrite/examples_test.go b/hclwrite/examples_test.go new file mode 100644 index 0000000..170572f --- /dev/null +++ b/hclwrite/examples_test.go @@ -0,0 +1,58 @@ +package hclwrite_test + +import ( + "fmt" + + "github.com/hashicorp/hcl2/hclwrite" + "github.com/zclconf/go-cty/cty" +) + +func Example_generateFromScratch() { + f := hclwrite.NewEmptyFile() + rootBody := f.Body() + rootBody.SetAttributeValue("string", cty.StringVal("bar")) // this is overwritten later + rootBody.AppendNewline() + rootBody.SetAttributeValue("object", cty.ObjectVal(map[string]cty.Value{ + "foo": cty.StringVal("foo"), + "bar": cty.NumberIntVal(5), + "baz": cty.True, + })) + rootBody.SetAttributeValue("string", cty.StringVal("foo")) + rootBody.SetAttributeValue("bool", cty.False) + rootBody.AppendNewline() + fooBlock := rootBody.AppendNewBlock("foo", nil) + fooBody := fooBlock.Body() + rootBody.AppendNewBlock("empty", nil) + rootBody.AppendNewline() + barBlock := rootBody.AppendNewBlock("bar", []string{"a", "b"}) + barBody := barBlock.Body() + + fooBody.SetAttributeValue("hello", cty.StringVal("world")) + + bazBlock := barBody.AppendNewBlock("baz", nil) + bazBody := bazBlock.Body() + bazBody.SetAttributeValue("foo", cty.NumberIntVal(10)) + bazBody.SetAttributeValue("beep", cty.StringVal("boop")) + bazBody.SetAttributeValue("baz", cty.ListValEmpty(cty.String)) + + 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 = [] + // } + // } +} diff --git a/hclwrite/roundup_test.go b/hclwrite/roundup_test.go deleted file mode 100644 index 31543d1..0000000 --- a/hclwrite/roundup_test.go +++ /dev/null @@ -1,66 +0,0 @@ -package hclwrite_test - -import ( - "bytes" - "strings" - "testing" - - "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) { - f := hclwrite.NewEmptyFile() - rootBody := f.Body() - rootBody.SetAttributeValue("string", cty.StringVal("bar")) // this is overwritten later - rootBody.AppendNewline() - rootBody.SetAttributeValue("object", cty.ObjectVal(map[string]cty.Value{ - "foo": cty.StringVal("foo"), - "bar": cty.NumberIntVal(5), - "baz": cty.True, - })) - rootBody.SetAttributeValue("string", cty.StringVal("foo")) - rootBody.SetAttributeValue("bool", cty.False) - rootBody.AppendNewline() - fooBlock := rootBody.AppendNewBlock("foo", nil) - fooBody := fooBlock.Body() - rootBody.AppendNewBlock("empty", nil) - rootBody.AppendNewline() - barBlock := rootBody.AppendNewBlock("bar", []string{"a", "b"}) - barBody := barBlock.Body() - - fooBody.SetAttributeValue("hello", cty.StringVal("world")) - - bazBlock := barBody.AppendNewBlock("baz", nil) - bazBody := bazBlock.Body() - bazBody.SetAttributeValue("foo", cty.NumberIntVal(10)) - 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) - } -} -- cgit v1.2.3