github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/views/json/function_test.go (about)

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