github.com/opentofu/opentofu@v1.7.1/internal/command/views/json/function_test.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package json
     7  
     8  import (
     9  	"encoding/json"
    10  	"testing"
    11  
    12  	"github.com/google/go-cmp/cmp"
    13  	"github.com/zclconf/go-cty/cty/function"
    14  	"github.com/zclconf/go-cty/cty/function/stdlib"
    15  )
    16  
    17  func TestDescribeFunction(t *testing.T) {
    18  	// NOTE: This test case is referring to some real functions in other
    19  	// packages. and so if those functions change signature later it will
    20  	// probably make some cases here fail. If that is the cause of the failure,
    21  	// it's fine to update the test here to match rather than to revert the
    22  	// change to the function signature, as long as the change to the
    23  	// function signature is otherwise within the bounds of our compatibility
    24  	// promises.
    25  
    26  	tests := map[string]struct {
    27  		Function function.Function
    28  		Want     *Function
    29  	}{
    30  		"upper": {
    31  			Function: stdlib.UpperFunc,
    32  			Want: &Function{
    33  				Name: "upper",
    34  				Params: []FunctionParam{
    35  					{
    36  						Name: "str",
    37  						Type: json.RawMessage(`"string"`),
    38  					},
    39  				},
    40  				ReturnType: json.RawMessage(`"string"`),
    41  			},
    42  		},
    43  		"coalesce": {
    44  			Function: stdlib.CoalesceFunc,
    45  			Want: &Function{
    46  				Name:   "coalesce",
    47  				Params: []FunctionParam{},
    48  				VariadicParam: &FunctionParam{
    49  					Name: "vals",
    50  					Type: json.RawMessage(`"dynamic"`),
    51  				},
    52  				ReturnType: json.RawMessage(`"dynamic"`),
    53  			},
    54  		},
    55  		"join": {
    56  			Function: stdlib.JoinFunc,
    57  			Want: &Function{
    58  				Name: "join",
    59  				Params: []FunctionParam{
    60  					{
    61  						Name: "separator",
    62  						Type: json.RawMessage(`"string"`),
    63  					},
    64  				},
    65  				VariadicParam: &FunctionParam{
    66  					Name: "lists",
    67  					Type: json.RawMessage(`["list","string"]`),
    68  				},
    69  				ReturnType: json.RawMessage(`"string"`),
    70  			},
    71  		},
    72  		"jsonencode": {
    73  			Function: stdlib.JSONEncodeFunc,
    74  			Want: &Function{
    75  				Name: "jsonencode",
    76  				Params: []FunctionParam{
    77  					{
    78  						Name: "val",
    79  						Type: json.RawMessage(`"dynamic"`),
    80  					},
    81  				},
    82  				ReturnType: json.RawMessage(`"string"`),
    83  			},
    84  		},
    85  	}
    86  
    87  	for name, test := range tests {
    88  		t.Run(name, func(t *testing.T) {
    89  			got := DescribeFunction(name, test.Function)
    90  			want := test.Want
    91  
    92  			if diff := cmp.Diff(want, got); diff != "" {
    93  				t.Errorf("wrong result\n%s", diff)
    94  			}
    95  		})
    96  	}
    97  }