opentofu/internal/configs/hcl2shim/util_test.go
Oleksandr Levchenkov 5a161c8bcc
add automated copyright header check (#1696)
Signed-off-by: ollevche <ollevche@gmail.com>
Signed-off-by: Oleksandr Levchenkov <ollevche@gmail.com>
2024-06-03 16:49:36 +03:00

61 lines
1.3 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package hcl2shim
import (
"reflect"
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
hclJSON "github.com/hashicorp/hcl/v2/json"
)
func TestConvertJSONExpressionToHCL(t *testing.T) {
tests := []struct {
Input string
}{
{
Input: "hello",
},
{
Input: "resource.test_resource[0]",
},
}
for _, test := range tests {
JSONExpr, diags := hclJSON.ParseExpression([]byte(`"`+test.Input+`"`), "")
if diags.HasErrors() {
t.Errorf("got %d diagnostics; want 0", len(diags))
for _, d := range diags {
t.Logf(" - %s", d.Error())
}
}
want, diags := hclsyntax.ParseExpression([]byte(test.Input), "", hcl.Pos{Line: 1, Column: 1})
if diags.HasErrors() {
t.Errorf("got %d diagnostics; want 0", len(diags))
for _, d := range diags {
t.Logf(" - %s", d.Error())
}
}
t.Run(test.Input, func(t *testing.T) {
resultExpr, diags := ConvertJSONExpressionToHCL(JSONExpr)
if diags.HasErrors() {
t.Errorf("got %d diagnostics; want 0", len(diags))
for _, d := range diags {
t.Logf(" - %s", d.Error())
}
}
if !reflect.DeepEqual(resultExpr, want) {
t.Errorf("got %s, but want %s", resultExpr, want)
}
})
}
}