github.com/prebid/prebid-server/v2@v2.18.0/macros/macros_test.go (about)

     1  package macros
     2  
     3  import (
     4  	"testing"
     5  	"text/template"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  const validEndpointTemplate = "http://{{.Host}}/publisher/{{.PublisherID}}"
    11  
    12  func TestResolveMacros(t *testing.T) {
    13  	endpointTemplate := template.Must(template.New("endpointTemplate").Parse(validEndpointTemplate))
    14  
    15  	testCases := []struct {
    16  		givenTemplate  *template.Template
    17  		givenParams    interface{}
    18  		expectedResult string
    19  		expectedError  bool
    20  	}{
    21  		{
    22  			givenTemplate:  endpointTemplate,
    23  			givenParams:    EndpointTemplateParams{Host: "SomeHost", PublisherID: "1"},
    24  			expectedResult: "http://SomeHost/publisher/1",
    25  			expectedError:  false,
    26  		},
    27  		{
    28  			givenTemplate:  endpointTemplate,
    29  			givenParams:    UserSyncPrivacy{GDPR: "SomeGDPR", GDPRConsent: "SomeGDPRConsent"},
    30  			expectedResult: "",
    31  			expectedError:  true,
    32  		},
    33  	}
    34  
    35  	for _, test := range testCases {
    36  		result, err := ResolveMacros(test.givenTemplate, test.givenParams)
    37  
    38  		if test.expectedError {
    39  			assert.NotNil(t, err, "Error shouldn't be nil")
    40  			assert.Empty(t, result, "Result should be empty")
    41  		} else {
    42  			assert.Nil(t, err, "Err should be nil")
    43  			assert.Equal(t, result, test.expectedResult, "String after resolving macros should be %s", test.expectedResult)
    44  		}
    45  	}
    46  }