github.com/avenga/couper@v1.12.2/eval/lib/default_test.go (about)

     1  package lib_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/zclconf/go-cty/cty"
     7  
     8  	"github.com/avenga/couper/config/configload"
     9  	"github.com/avenga/couper/config/request"
    10  	"github.com/avenga/couper/eval"
    11  	"github.com/avenga/couper/internal/test"
    12  )
    13  
    14  func TestDefaultErrors(t *testing.T) {
    15  	helper := test.New(t)
    16  
    17  	cf, err := configload.LoadBytes([]byte(`server {}`), "couper.hcl")
    18  	helper.Must(err)
    19  
    20  	tests := []struct {
    21  		name    string
    22  		args    []cty.Value
    23  		wantErr string
    24  	}{
    25  		{
    26  			"mixed types",
    27  			[]cty.Value{
    28  				cty.TupleVal([]cty.Value{
    29  					cty.StringVal("1"),
    30  				}),
    31  				cty.ObjectVal(map[string]cty.Value{
    32  					"a": cty.NumberIntVal(1),
    33  				}),
    34  			},
    35  			"all defined arguments must have the same type",
    36  		},
    37  	}
    38  
    39  	hclContext := cf.Context.Value(request.ContextType).(*eval.Context).HCLContext()
    40  
    41  	for _, tt := range tests {
    42  		t.Run(tt.name, func(subT *testing.T) {
    43  			_, err := hclContext.Functions["default"].Call(tt.args)
    44  			if err == nil {
    45  				subT.Error("Error expected")
    46  			}
    47  			if err != nil && err.Error() != tt.wantErr {
    48  				subT.Errorf("Wrong error message; expected %#v, got: %#v", tt.wantErr, err.Error())
    49  			}
    50  		})
    51  	}
    52  }