...

Source file src/github.com/goph/emperror/handler_context_test.go

Documentation: github.com/goph/emperror

     1  package emperror
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestHandlerContext(t *testing.T) {
    10  	testHandler := NewTestHandler()
    11  
    12  	kvs := []interface{}{"a", 123}
    13  	handler := HandlerWith(testHandler, kvs...)
    14  
    15  	handler.Handle(errors.New("error"))
    16  
    17  	cerr := With(errors.New("error"), "a", 123)
    18  
    19  	if got, want := testHandler.LastError(), cerr; !reflect.DeepEqual(got, want) {
    20  		t.Errorf("error does not match the expected one\nactual:   %s\nexpected: %s", got, want)
    21  	}
    22  }
    23  
    24  func TestHandlerContext_Multi(t *testing.T) {
    25  	testHandler := NewTestHandler()
    26  
    27  	handler := HandlerWith(HandlerWith(testHandler, "a", 123), "b", 321)
    28  
    29  	handler.Handle(errors.New("error"))
    30  
    31  	cerr := With(errors.New("error"), "a", 123, "b", 321)
    32  
    33  	if got, want := testHandler.LastError(), cerr; !reflect.DeepEqual(got, want) {
    34  		t.Errorf("error does not match the expected one\nactual:   %s\nexpected: %s", got, want)
    35  	}
    36  }
    37  
    38  func TestHandlerContext_MultiPrefix(t *testing.T) {
    39  	testHandler := NewTestHandler()
    40  
    41  	handler := HandlerWithPrefix(HandlerWith(testHandler, "a", 123), "b", 321)
    42  
    43  	handler.Handle(errors.New("error"))
    44  
    45  	cerr := With(errors.New("error"), "b", 321, "a", 123)
    46  
    47  	if got, want := testHandler.LastError(), cerr; !reflect.DeepEqual(got, want) {
    48  		t.Errorf("error does not match the expected one\nactual:   %s\nexpected: %s", got, want)
    49  	}
    50  }
    51  
    52  func TestHandlerContext_MissingValue(t *testing.T) {
    53  	testHandler := NewTestHandler()
    54  
    55  	handler := HandlerWithPrefix(HandlerWith(testHandler, "k0"), "k1")
    56  
    57  	handler.Handle(errors.New("error"))
    58  
    59  	cerr := With(errors.New("error"), "k1", nil, "k0", nil)
    60  
    61  	if got, want := testHandler.LastError(), cerr; !reflect.DeepEqual(got, want) {
    62  		t.Errorf("error does not match the expected one\nactual:   %s\nexpected: %s", got, want)
    63  	}
    64  }
    65  

View as plain text