github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/platform/api/api_test.go (about)

     1  package api_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ActiveState/cli/pkg/platform/api"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestErrorCode_WithoutPayload(t *testing.T) {
    11  	assert.Equal(t, 100, api.ErrorCode(&struct{ Code int }{
    12  		Code: 100,
    13  	}))
    14  }
    15  
    16  func TestErrorCode_WithoutPayload_NoCodeValue(t *testing.T) {
    17  	assert.Equal(t, -1, api.ErrorCode(&struct{ OtherCode int }{
    18  		OtherCode: 100,
    19  	}))
    20  }
    21  
    22  func TestErrorCode_WithPayload(t *testing.T) {
    23  	providedCode := 200
    24  	codeValue := struct{ Code *int }{Code: &providedCode}
    25  	payload := struct{ Payload struct{ Code *int } }{
    26  		Payload: codeValue,
    27  	}
    28  
    29  	assert.Equal(t, 200, api.ErrorCode(&payload))
    30  }
    31  
    32  func TestErrorCode_WithPayload_CodeNotPointer(t *testing.T) {
    33  	providedCode := 300
    34  	codeValue := struct{ Code int }{Code: providedCode}
    35  	payload := struct{ Payload struct{ Code int } }{
    36  		Payload: codeValue,
    37  	}
    38  
    39  	assert.Equal(t, 300, api.ErrorCode(&payload))
    40  }
    41  
    42  func TestErrorCode_WithPayload_NoCodeField(t *testing.T) {
    43  	providedCode := 400
    44  	codeValue := struct{ OtherCode int }{OtherCode: providedCode}
    45  	payload := struct{ Payload struct{ OtherCode int } }{
    46  		Payload: codeValue,
    47  	}
    48  
    49  	assert.Equal(t, -1, api.ErrorCode(&payload))
    50  }