From a81aa7b5dd2d665c32b7e737844ac6012158aed4 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Tue, 20 Mar 2018 12:09:03 +0100 Subject: [PATCH] printer: Add unit test of Format() producing unparsable output. --- hcl/printer/printer_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/hcl/printer/printer_test.go b/hcl/printer/printer_test.go index 5248259..f39edda 100644 --- a/hcl/printer/printer_test.go +++ b/hcl/printer/printer_test.go @@ -147,3 +147,25 @@ func lineAt(text []byte, offs int) []byte { } return text[offs:i] } + +// TestFormatParsable ensures that the output of Format() is can be parsed again. +func TestFormatValidOutput(t *testing.T) { + cases := []string{ + "#\x00", + "#\ue123t", + } + + for _, c := range cases { + f, err := Format([]byte(c)) + if err != nil { + // ignore these failures, not all inputs are valid HCL. + t.Logf("Format(%q) = %v", c, err) + continue + } + + if _, err := parser.Parse(f); err != nil { + t.Errorf("Format(%q) = %q; Parse(%q) = %v", c, f, f, err) + continue + } + } +}