mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-03-05 11:01:25 -05:00
Use a slightly modified value renderer from terraform-provider-testing to display values in the console REPL, as well as outputs from the apply and outputs subcommands. Derived from code in this repository, MIT licensed: https://github.com/apparentlymart/terraform-provider-testing Note that this is technically a breaking change for the console subcommand, which would previously error if the user attempted to render an unknown value (such as an unset variable). This was marked as an unintentional side effect, with the goal being the new behaviour of rendering "(unknown)", which is why I changed the behaviour in this commit.
150 lines
2.2 KiB
Go
150 lines
2.2 KiB
Go
package repl
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
func TestFormatValue(t *testing.T) {
|
|
tests := []struct {
|
|
Val cty.Value
|
|
Want string
|
|
}{
|
|
{
|
|
cty.NullVal(cty.DynamicPseudoType),
|
|
`null`,
|
|
},
|
|
{
|
|
cty.NullVal(cty.String),
|
|
`tostring(null)`,
|
|
},
|
|
{
|
|
cty.NullVal(cty.Number),
|
|
`tonumber(null)`,
|
|
},
|
|
{
|
|
cty.NullVal(cty.Bool),
|
|
`tobool(null)`,
|
|
},
|
|
{
|
|
cty.NullVal(cty.List(cty.String)),
|
|
`tolist(null) /* of string */`,
|
|
},
|
|
{
|
|
cty.NullVal(cty.Set(cty.Number)),
|
|
`toset(null) /* of number */`,
|
|
},
|
|
{
|
|
cty.NullVal(cty.Map(cty.Bool)),
|
|
`tomap(null) /* of bool */`,
|
|
},
|
|
{
|
|
cty.NullVal(cty.Object(map[string]cty.Type{"a": cty.Bool})),
|
|
`null /* object */`, // Ideally this would display the full object type, including its attributes
|
|
},
|
|
{
|
|
cty.UnknownVal(cty.DynamicPseudoType),
|
|
`(known after apply)`,
|
|
},
|
|
{
|
|
cty.StringVal(""),
|
|
`""`,
|
|
},
|
|
{
|
|
cty.StringVal("hello"),
|
|
`"hello"`,
|
|
},
|
|
{
|
|
cty.StringVal("hello\nworld"),
|
|
`"hello\nworld"`, // Ideally we'd use heredoc syntax here for better readability, but we don't yet
|
|
},
|
|
{
|
|
cty.Zero,
|
|
`0`,
|
|
},
|
|
{
|
|
cty.NumberIntVal(5),
|
|
`5`,
|
|
},
|
|
{
|
|
cty.NumberFloatVal(5.2),
|
|
`5.2`,
|
|
},
|
|
{
|
|
cty.False,
|
|
`false`,
|
|
},
|
|
{
|
|
cty.True,
|
|
`true`,
|
|
},
|
|
{
|
|
cty.EmptyObjectVal,
|
|
`{}`,
|
|
},
|
|
{
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
"a": cty.StringVal("b"),
|
|
}),
|
|
`{
|
|
"a" = "b"
|
|
}`,
|
|
},
|
|
{
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
"a": cty.StringVal("b"),
|
|
"c": cty.StringVal("d"),
|
|
}),
|
|
`{
|
|
"a" = "b"
|
|
"c" = "d"
|
|
}`,
|
|
},
|
|
{
|
|
cty.MapValEmpty(cty.String),
|
|
`tomap({})`,
|
|
},
|
|
{
|
|
cty.EmptyTupleVal,
|
|
`[]`,
|
|
},
|
|
{
|
|
cty.TupleVal([]cty.Value{
|
|
cty.StringVal("b"),
|
|
}),
|
|
`[
|
|
"b",
|
|
]`,
|
|
},
|
|
{
|
|
cty.TupleVal([]cty.Value{
|
|
cty.StringVal("b"),
|
|
cty.StringVal("d"),
|
|
}),
|
|
`[
|
|
"b",
|
|
"d",
|
|
]`,
|
|
},
|
|
{
|
|
cty.ListValEmpty(cty.String),
|
|
`tolist([])`,
|
|
},
|
|
{
|
|
cty.SetValEmpty(cty.String),
|
|
`toset([])`,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(fmt.Sprintf("%#v", test.Val), func(t *testing.T) {
|
|
got := FormatValue(test.Val, 0)
|
|
if got != test.Want {
|
|
t.Errorf("wrong result\nvalue: %#v\ngot: %s\nwant: %s", test.Val, got, test.Want)
|
|
}
|
|
})
|
|
}
|
|
}
|