gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/errors/errors_test.go (about)

     1  package errors
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  )
     7  
     8  func TestErrors(t *testing.T) {
     9  	testData := []*Error{
    10  		{
    11  			Id:     "test",
    12  			Code:   500,
    13  			Detail: "Internal server error",
    14  			Status: http.StatusText(500),
    15  		},
    16  	}
    17  
    18  	for _, e := range testData {
    19  		ne := New(e.Id, e.Detail, e.Code)
    20  
    21  		if e.Error() != ne.Error() {
    22  			t.Fatalf("Expected %s got %s", e.Error(), ne.Error())
    23  		}
    24  
    25  		pe := Parse(ne.Error())
    26  
    27  		if pe == nil {
    28  			t.Fatalf("Expected error got nil %v", pe)
    29  		}
    30  
    31  		if pe.Id != e.Id {
    32  			t.Fatalf("Expected %s got %s", e.Id, pe.Id)
    33  		}
    34  
    35  		if pe.Detail != e.Detail {
    36  			t.Fatalf("Expected %s got %s", e.Detail, pe.Detail)
    37  		}
    38  
    39  		if pe.Code != e.Code {
    40  			t.Fatalf("Expected %d got %d", e.Code, pe.Code)
    41  		}
    42  
    43  		if pe.Status != e.Status {
    44  			t.Fatalf("Expected %s got %s", e.Status, pe.Status)
    45  		}
    46  	}
    47  }