github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/views/json/function_test.go (about)

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