github.com/alimy/mir/v4@v4.1.0/error_test.go (about)

     1  package mir
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  )
     9  
    10  func TestNewError(t *testing.T) {
    11  	for _, data := range []struct {
    12  		code int
    13  		err  error
    14  		msg  string
    15  	}{
    16  		{
    17  			code: http.StatusInternalServerError,
    18  			err:  errors.New(http.StatusText(http.StatusInternalServerError)),
    19  			msg:  http.StatusText(http.StatusInternalServerError),
    20  		},
    21  		{
    22  			code: http.StatusMethodNotAllowed,
    23  			err:  errors.New(http.StatusText(http.StatusMethodNotAllowed)),
    24  			msg:  http.StatusText(http.StatusMethodNotAllowed),
    25  		},
    26  	} {
    27  		err := NewError(data.code, data.err)
    28  		code, msg := err.StatusCode(), err.Error()
    29  		if code != data.code {
    30  			t.Errorf("expect error code: %d but got: %d", data.code, code)
    31  		}
    32  		if msg != data.msg {
    33  			t.Errorf("expect error msg: %s but got: %s", data.msg, msg)
    34  		}
    35  	}
    36  }
    37  
    38  func TestErrorf(t *testing.T) {
    39  	for _, data := range []struct {
    40  		code   int
    41  		format string
    42  		a      []any
    43  	}{
    44  		{
    45  			code:   http.StatusInternalServerError,
    46  			format: `internal server error: %s:%s`,
    47  			a:      []any{"host", "localhost"},
    48  		},
    49  		{
    50  			code:   http.StatusMethodNotAllowed,
    51  			format: `method not allowed: %s:%s`,
    52  			a:      []any{"method", "POST"},
    53  		},
    54  	} {
    55  		err := Errorf(data.code, data.format, data.a...)
    56  		fmtErrMsg := fmt.Errorf(data.format, data.a...).Error()
    57  		code, msg := err.StatusCode(), err.Error()
    58  		if code != data.code {
    59  			t.Errorf("expect error code: %d but got: %d", data.code, code)
    60  		}
    61  		if msg != fmtErrMsg {
    62  			t.Errorf("expect error msg: %s but got: %s", fmtErrMsg, msg)
    63  		}
    64  	}
    65  }
    66  
    67  func TestErrorln(t *testing.T) {
    68  	for _, data := range []struct {
    69  		code int
    70  		text string
    71  		msg  string
    72  	}{
    73  		{
    74  			code: http.StatusInternalServerError,
    75  			text: http.StatusText(http.StatusInternalServerError),
    76  			msg:  http.StatusText(http.StatusInternalServerError),
    77  		},
    78  		{
    79  			code: http.StatusMethodNotAllowed,
    80  			text: http.StatusText(http.StatusMethodNotAllowed),
    81  			msg:  http.StatusText(http.StatusMethodNotAllowed),
    82  		},
    83  	} {
    84  		err := Errorln(data.code, data.text)
    85  		code, msg := err.StatusCode(), err.Error()
    86  		if code != data.code {
    87  			t.Errorf("expect error code: %d but got: %d", data.code, code)
    88  		}
    89  		if msg != data.msg {
    90  			t.Errorf("expect error msg: %s but got: %s", data.msg, msg)
    91  		}
    92  	}
    93  }