github.com/hashicorp/packer@v1.14.3/hcl2template/function/strcontains_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package function
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/zclconf/go-cty/cty"
    11  )
    12  
    13  func TestStrContains(t *testing.T) {
    14  	tests := []struct {
    15  		String    cty.Value
    16  		Substr    cty.Value
    17  		Want      cty.Value
    18  		ExpectErr bool
    19  	}{
    20  		{
    21  			cty.StringVal("hello"),
    22  			cty.StringVal("hel"),
    23  			cty.BoolVal(true),
    24  			false,
    25  		},
    26  		{
    27  			cty.StringVal("hello"),
    28  			cty.StringVal("lo"),
    29  			cty.BoolVal(true),
    30  			false,
    31  		},
    32  		{
    33  			cty.StringVal("hello1"),
    34  			cty.StringVal("1"),
    35  			cty.BoolVal(true),
    36  			false,
    37  		},
    38  		{
    39  			cty.StringVal("hello1"),
    40  			cty.StringVal("heo"),
    41  			cty.BoolVal(false),
    42  			false,
    43  		},
    44  		{
    45  			cty.StringVal("hello1"),
    46  			cty.NumberIntVal(1),
    47  			cty.UnknownVal(cty.Bool),
    48  			true,
    49  		},
    50  	}
    51  
    52  	for _, test := range tests {
    53  		t.Run(fmt.Sprintf("includes(%#v, %#v)", test.String, test.Substr), func(t *testing.T) {
    54  			got, err := StrContains.Call([]cty.Value{
    55  				test.String,
    56  				test.Substr,
    57  			})
    58  
    59  			if test.ExpectErr && err == nil {
    60  				t.Fatal("succeeded; want error")
    61  			}
    62  
    63  			if test.ExpectErr && err != nil {
    64  				return
    65  			}
    66  
    67  			if !test.ExpectErr && err != nil {
    68  				t.Fatalf("unexpected error: %s", err)
    69  			}
    70  
    71  			if !got.RawEquals(test.Want) {
    72  				t.Errorf("wrong result\ngot:  %#v\nwant: %#v", got, test.Want)
    73  			}
    74  		})
    75  	}
    76  }