github.com/beauknowssoftware/makehcl@v0.0.0-20200322000747-1b9bb1e1c008/internal/functions/exists_test.go (about)

     1  package functions_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/beauknowssoftware/makehcl/internal/functions"
     7  	"github.com/pkg/errors"
     8  	"github.com/zclconf/go-cty/cty"
     9  )
    10  
    11  func exists(t *testing.T, args []cty.Value, retType cty.Type) cty.Value {
    12  	v, err := functions.Exists(args, retType)
    13  	if err != nil {
    14  		err = errors.Wrap(err, "failed to exists")
    15  		t.Fatal(err)
    16  	}
    17  
    18  	return v
    19  }
    20  
    21  func TestExists(t *testing.T) {
    22  	tests := map[string]struct {
    23  		args     []cty.Value
    24  		retType  cty.Type
    25  		expected cty.Value
    26  	}{
    27  		"object exists": {
    28  			args: []cty.Value{
    29  				cty.ObjectVal(map[string]cty.Value{
    30  					"test": cty.StringVal("value"),
    31  				}),
    32  				cty.StringVal("test"),
    33  			},
    34  			expected: cty.BoolVal(true),
    35  		},
    36  		"object not exists": {
    37  			args: []cty.Value{
    38  				cty.ObjectVal(map[string]cty.Value{
    39  					"test": cty.StringVal("value"),
    40  				}),
    41  				cty.StringVal("another"),
    42  			},
    43  			expected: cty.BoolVal(false),
    44  		},
    45  		"map exists": {
    46  			args: []cty.Value{
    47  				cty.MapVal(map[string]cty.Value{
    48  					"test": cty.StringVal("value"),
    49  				}),
    50  				cty.StringVal("test"),
    51  			},
    52  			expected: cty.BoolVal(true),
    53  		},
    54  		"map not exists": {
    55  			args: []cty.Value{
    56  				cty.MapVal(map[string]cty.Value{
    57  					"test": cty.StringVal("value"),
    58  				}),
    59  				cty.StringVal("another"),
    60  			},
    61  			expected: cty.BoolVal(false),
    62  		},
    63  	}
    64  
    65  	for name, test := range tests {
    66  		t.Run(name, func(t *testing.T) {
    67  			actual := exists(t, test.args, test.retType)
    68  
    69  			if !test.expected.RawEquals(actual) {
    70  				t.Fatalf("\nexpected %v\ngot %v", test.expected.GoString(), actual.GoString())
    71  			}
    72  		})
    73  	}
    74  }