Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions diagnostic_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ func (w *diagnosticTextWriter) valueStr(val cty.Value) string {
// Should never happen here because we should filter before we get
// in here, but we'll do something reasonable rather than panic.
return "(not yet known)"
case val.IsMarked():
return "(marked value)"
case ty == cty.Bool:
if val.True() {
return "true"
Expand Down
77 changes: 77 additions & 0 deletions diagnostic_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package hcl
import (
"bytes"
"fmt"
"reflect"
"testing"

"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -236,3 +237,79 @@ type diagnosticTestExpr struct {
func (e *diagnosticTestExpr) Variables() []Traversal {
return e.vars
}

func TestValuestr(t *testing.T) {
var i = 0

tests := []struct {
Input cty.Value
Want string
}{
{
Input: cty.NullVal(cty.String),
Want: "null",
},
{
Input: cty.UnknownVal(cty.String),
Want: "(not yet known)",
},
{
Input: cty.StringVal("marked").Mark("x"),
Want: "(marked value)",
},
{
Input: cty.True,
Want: "true",
},
{
Input: cty.False,
Want: "false",
},
{
Input: cty.NumberIntVal(1),
Want: "1",
},
{
Input: cty.StringVal("foo"),
Want: `"foo"`,
},
{
Input: cty.EmptyTupleVal,
Want: "empty tuple",
},
{
Input: cty.TupleVal([]cty.Value{cty.StringVal("foo")}),
Want: "tuple with 1 element",
},
{
Input: cty.TupleVal([]cty.Value{cty.StringVal("foo"), cty.StringVal("bar")}),
Want: "tuple with 2 elements",
},
{
Input: cty.EmptyObjectVal,
Want: "object with no attributes",
},
{
Input: cty.ObjectVal(map[string]cty.Value{"foo": cty.StringVal("bar")}),
Want: `object with 1 attribute "foo"`,
},
{
Input: cty.ObjectVal(map[string]cty.Value{"foo": cty.NumberIntVal(1), "bar": cty.NumberIntVal(2)}),
Want: "object with 2 attributes",
},
{
Input: cty.CapsuleVal(cty.Capsule("capsule", reflect.TypeOf(0)), &i),
Want: "capsule",
},
}

for _, test := range tests {
t.Run(test.Input.GoString(), func(t *testing.T) {
dwr := diagnosticTextWriter{}
got := dwr.valueStr(test.Input)
if got != test.Want {
t.Errorf("wrong result\n\ngot: %s\nwant: %s", got, test.Want)
}
})
}
}
Loading