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

     1  package jsonfunction
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/zclconf/go-cty-debug/ctydebug"
     9  	"github.com/zclconf/go-cty/cty"
    10  	"github.com/zclconf/go-cty/cty/function"
    11  )
    12  
    13  func TestMarshal(t *testing.T) {
    14  	tests := []struct {
    15  		Name    string
    16  		Input   map[string]function.Function
    17  		Want    string
    18  		WantErr string
    19  	}{
    20  		{
    21  			"minimal function",
    22  			map[string]function.Function{
    23  				"fun": function.New(&function.Spec{
    24  					Type: function.StaticReturnType(cty.Bool),
    25  				}),
    26  			},
    27  			`{"format_version":"1.0","function_signatures":{"fun":{"return_type":"bool"}}}`,
    28  			"",
    29  		},
    30  		{
    31  			"function with description",
    32  			map[string]function.Function{
    33  				"fun": function.New(&function.Spec{
    34  					Description: "`timestamp` returns a UTC timestamp string.",
    35  					Type:        function.StaticReturnType(cty.String),
    36  				}),
    37  			},
    38  			"{\"format_version\":\"1.0\",\"function_signatures\":{\"fun\":{\"description\":\"`timestamp` returns a UTC timestamp string.\",\"return_type\":\"string\"}}}",
    39  			"",
    40  		},
    41  		{
    42  			"function with parameters",
    43  			map[string]function.Function{
    44  				"fun": function.New(&function.Spec{
    45  					Params: []function.Parameter{
    46  						{
    47  							Name:        "timestamp",
    48  							Description: "timestamp text",
    49  							Type:        cty.String,
    50  						},
    51  						{
    52  							Name:        "duration",
    53  							Description: "duration text",
    54  							Type:        cty.String,
    55  						},
    56  					},
    57  					Type: function.StaticReturnType(cty.String),
    58  				}),
    59  			},
    60  			`{"format_version":"1.0","function_signatures":{"fun":{"return_type":"string","parameters":[{"name":"timestamp","description":"timestamp text","type":"string"},{"name":"duration","description":"duration text","type":"string"}]}}}`,
    61  			"",
    62  		},
    63  		{
    64  			"function with variadic parameter",
    65  			map[string]function.Function{
    66  				"fun": function.New(&function.Spec{
    67  					VarParam: &function.Parameter{
    68  						Name:             "default",
    69  						Description:      "default description",
    70  						Type:             cty.DynamicPseudoType,
    71  						AllowUnknown:     true,
    72  						AllowDynamicType: true,
    73  						AllowNull:        true,
    74  						AllowMarked:      true,
    75  					},
    76  					Type: function.StaticReturnType(cty.DynamicPseudoType),
    77  				}),
    78  			},
    79  			`{"format_version":"1.0","function_signatures":{"fun":{"return_type":"dynamic","variadic_parameter":{"name":"default","description":"default description","is_nullable":true,"type":"dynamic"}}}}`,
    80  			"",
    81  		},
    82  		{
    83  			"function with list types",
    84  			map[string]function.Function{
    85  				"fun": function.New(&function.Spec{
    86  					Params: []function.Parameter{
    87  						{
    88  							Name: "list",
    89  							Type: cty.List(cty.String),
    90  						},
    91  					},
    92  					Type: function.StaticReturnType(cty.List(cty.String)),
    93  				}),
    94  			},
    95  			`{"format_version":"1.0","function_signatures":{"fun":{"return_type":["list","string"],"parameters":[{"name":"list","type":["list","string"]}]}}}`,
    96  			"",
    97  		},
    98  		{
    99  			"returns diagnostics on failure",
   100  			map[string]function.Function{
   101  				"fun": function.New(&function.Spec{
   102  					Params: []function.Parameter{},
   103  					Type: func(args []cty.Value) (ret cty.Type, err error) {
   104  						return cty.DynamicPseudoType, fmt.Errorf("error")
   105  					},
   106  				}),
   107  			},
   108  			"",
   109  			"Failed to serialize function \"fun\": error",
   110  		},
   111  	}
   112  
   113  	for i, test := range tests {
   114  		t.Run(fmt.Sprintf("%d-%s", i, test.Name), func(t *testing.T) {
   115  			got, diags := Marshal(test.Input)
   116  			if test.WantErr != "" {
   117  				if !diags.HasErrors() {
   118  					t.Fatal("expected error, got none")
   119  				}
   120  				if diags.Err().Error() != test.WantErr {
   121  					t.Fatalf("expected error %q, got %q", test.WantErr, diags.Err())
   122  				}
   123  			} else {
   124  				if diags.HasErrors() {
   125  					t.Fatal(diags)
   126  				}
   127  
   128  				if diff := cmp.Diff(test.Want, string(got), ctydebug.CmpOptions); diff != "" {
   129  					t.Fatalf("mismatch of function signature: %s", diff)
   130  				}
   131  			}
   132  		})
   133  	}
   134  }