summaryrefslogtreecommitdiff
path: root/diagnostic_text_test.go
diff options
context:
space:
mode:
authorMartin Atkins <mart@degeneration.co.uk>2019-09-09 16:08:19 -0700
committerMartin Atkins <mart@degeneration.co.uk>2019-09-09 16:08:19 -0700
commit6c4344623b6ac528a57f9b80e4622acfab2fde40 (patch)
tree83031084d3ab54abbe40e3fd749e439c8b28e9d1 /diagnostic_text_test.go
parent0f5ab3bd563c111077917020c0cbc8c211e1bff3 (diff)
Unfold the "hcl" directory up into the root
The main HCL package is more visible this way, and so it's easier than having to pick it out from dozens of other package directories.
Diffstat (limited to 'diagnostic_text_test.go')
-rw-r--r--diagnostic_text_test.go235
1 files changed, 235 insertions, 0 deletions
diff --git a/diagnostic_text_test.go b/diagnostic_text_test.go
new file mode 100644
index 0000000..8757fbe
--- /dev/null
+++ b/diagnostic_text_test.go
@@ -0,0 +1,235 @@
+package hcl
+
+import (
+ "bytes"
+ "fmt"
+ "testing"
+
+ "github.com/zclconf/go-cty/cty"
+)
+
+func TestDiagnosticTextWriter(t *testing.T) {
+ tests := []struct {
+ Input *Diagnostic
+ Want string
+ }{
+ {
+ &Diagnostic{
+ Severity: DiagError,
+ Summary: "Splines not reticulated",
+ Detail: "All splines must be pre-reticulated.",
+ Subject: &Range{
+ Start: Pos{
+ Byte: 0,
+ Column: 1,
+ Line: 1,
+ },
+ End: Pos{
+ Byte: 3,
+ Column: 4,
+ Line: 1,
+ },
+ },
+ },
+ `Error: Splines not reticulated
+
+ on line 1, in hardcoded-context:
+ 1: foo = 1
+
+All splines must be pre-reticulated.
+
+`,
+ },
+ {
+ &Diagnostic{
+ Severity: DiagError,
+ Summary: "Unsupported attribute",
+ Detail: `"baz" is not a supported top-level attribute. Did you mean "bam"?`,
+ Subject: &Range{
+ Start: Pos{
+ Byte: 16,
+ Column: 1,
+ Line: 3,
+ },
+ End: Pos{
+ Byte: 19,
+ Column: 4,
+ Line: 3,
+ },
+ },
+ },
+ `Error: Unsupported attribute
+
+ on line 3, in hardcoded-context:
+ 3: baz = 3
+
+"baz" is not a supported top-level
+attribute. Did you mean "bam"?
+
+`,
+ },
+ {
+ &Diagnostic{
+ Severity: DiagError,
+ Summary: "Unsupported attribute",
+ Detail: `"pizza" is not a supported attribute. Did you mean "pizzetta"?`,
+ Subject: &Range{
+ Start: Pos{
+ Byte: 42,
+ Column: 3,
+ Line: 5,
+ },
+ End: Pos{
+ Byte: 47,
+ Column: 8,
+ Line: 5,
+ },
+ },
+ // This is actually not a great example of a context, but is here to test
+ // whether we're able to show a multi-line context when needed.
+ Context: &Range{
+ Start: Pos{
+ Byte: 24,
+ Column: 1,
+ Line: 4,
+ },
+ End: Pos{
+ Byte: 60,
+ Column: 2,
+ Line: 6,
+ },
+ },
+ },
+ `Error: Unsupported attribute
+
+ on line 5, in hardcoded-context:
+ 4: block "party" {
+ 5: pizza = "cheese"
+ 6: }
+
+"pizza" is not a supported attribute.
+Did you mean "pizzetta"?
+
+`,
+ },
+ {
+ &Diagnostic{
+ Severity: DiagError,
+ Summary: "Test of including relevant variable values",
+ Detail: `This diagnostic includes an expression and an evalcontext.`,
+ Subject: &Range{
+ Start: Pos{
+ Byte: 42,
+ Column: 3,
+ Line: 5,
+ },
+ End: Pos{
+ Byte: 47,
+ Column: 8,
+ Line: 5,
+ },
+ },
+ Expression: &diagnosticTestExpr{
+ vars: []Traversal{
+ {
+ TraverseRoot{
+ Name: "foo",
+ },
+ },
+ {
+ TraverseRoot{
+ Name: "bar",
+ },
+ TraverseAttr{
+ Name: "baz",
+ },
+ },
+ {
+ TraverseRoot{
+ Name: "missing",
+ },
+ },
+ {
+ TraverseRoot{
+ Name: "boz",
+ },
+ },
+ },
+ },
+ EvalContext: &EvalContext{
+ parent: &EvalContext{
+ Variables: map[string]cty.Value{
+ "foo": cty.StringVal("foo value"),
+ },
+ },
+ Variables: map[string]cty.Value{
+ "bar": cty.ObjectVal(map[string]cty.Value{
+ "baz": cty.ListValEmpty(cty.String),
+ }),
+ "boz": cty.NumberIntVal(5),
+ "unused": cty.True,
+ },
+ },
+ },
+ `Error: Test of including relevant variable values
+
+ on line 5, in hardcoded-context:
+ 5: pizza = "cheese"
+
+with bar.baz as empty list of string,
+ boz as 5,
+ foo as "foo value".
+
+This diagnostic includes an expression
+and an evalcontext.
+
+`,
+ },
+ }
+
+ files := map[string]*File{
+ "": &File{
+ Bytes: []byte(testDiagnosticTextWriterSource),
+ Nav: &diagnosticTestNav{},
+ },
+ }
+
+ for i, test := range tests {
+ t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
+ bwr := &bytes.Buffer{}
+ dwr := NewDiagnosticTextWriter(bwr, files, 40, false)
+ err := dwr.WriteDiagnostic(test.Input)
+ if err != nil {
+ t.Fatalf("unexpected error: %s", err)
+ }
+ got := bwr.String()
+ if got != test.Want {
+ t.Errorf("wrong result\n\ngot:\n%swant:\n%s", got, test.Want)
+ }
+ })
+ }
+}
+
+const testDiagnosticTextWriterSource = `foo = 1
+bar = 2
+baz = 3
+block "party" {
+ pizza = "cheese"
+}
+`
+
+type diagnosticTestNav struct {
+}
+
+func (tn *diagnosticTestNav) ContextString(offset int) string {
+ return "hardcoded-context"
+}
+
+type diagnosticTestExpr struct {
+ vars []Traversal
+ staticExpr
+}
+
+func (e *diagnosticTestExpr) Variables() []Traversal {
+ return e.vars
+}