...

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

Documentation: github.com/goph/emperror

     1  package emperror
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  func TestWith(t *testing.T) {
    11  	err := errors.New("error")
    12  
    13  	kvs := []interface{}{"a", 123}
    14  	err = With(err, kvs...)
    15  	kvs[1] = 0 // With should copy its key values
    16  
    17  	ctx := Context(err)
    18  
    19  	if got, want := ctx[0], "a"; got != want {
    20  		t.Errorf("context value does not match the expected one\nactual:   %s\nexpected: %s", got, want)
    21  	}
    22  
    23  	if got, want := ctx[1], 123; got != want {
    24  		t.Errorf("context value does not match the expected one\nactual:   %s\nexpected: %d", got, want)
    25  	}
    26  
    27  	if got, want := err.Error(), "error"; got != want {
    28  		t.Errorf("error does not match the expected one\nactual:   %s\nexpected: %s", got, want)
    29  	}
    30  }
    31  
    32  func TestWith_Multiple(t *testing.T) {
    33  	err := errors.New("")
    34  
    35  	err = With(With(err, "a", 123), "b", 321)
    36  
    37  	ctx := Context(err)
    38  
    39  	if got, want := ctx[0], "a"; got != want {
    40  		t.Errorf("context value does not match the expected one\nactual:   %s\nexpected: %s", got, want)
    41  	}
    42  
    43  	if got, want := ctx[1], 123; got != want {
    44  		t.Errorf("context value does not match the expected one\nactual:   %s\nexpected: %d", got, want)
    45  	}
    46  
    47  	if got, want := ctx[2], "b"; got != want {
    48  		t.Errorf("context value does not match the expected one\nactual:   %s\nexpected: %s", got, want)
    49  	}
    50  
    51  	if got, want := ctx[3], 321; got != want {
    52  		t.Errorf("context value does not match the expected one\nactual:   %s\nexpected: %d", got, want)
    53  	}
    54  }
    55  
    56  func TestContextor_MissingValue(t *testing.T) {
    57  	err := errors.New("")
    58  
    59  	err = With(With(err, "k0"), "k1")
    60  
    61  	ctx := Context(err)
    62  
    63  	if got, want := len(ctx), 4; got != want {
    64  		t.Fatalf("context does not have the required length\nactual:   %d\nexpected: %d", got, want)
    65  	}
    66  
    67  	for i := 1; i < 4; i += 2 {
    68  		if ctx[i] != nil {
    69  			t.Errorf("context value %d is expected to be nil\nactual: %v", i, ctx[i])
    70  		}
    71  	}
    72  }
    73  
    74  func TestContext(t *testing.T) {
    75  	err := With(
    76  		errors.WithMessage(
    77  			With(
    78  				errors.Wrap(
    79  					With(
    80  						errors.New("error"),
    81  						"key", "value",
    82  					),
    83  					"wrapped error",
    84  				),
    85  				"key2", "value2",
    86  			),
    87  			"another wrapped error",
    88  		),
    89  		"key3", "value3",
    90  	)
    91  
    92  	expected := []interface{}{
    93  		"key", "value",
    94  		"key2", "value2",
    95  		"key3", "value3",
    96  	}
    97  
    98  	actual := Context(err)
    99  
   100  	if got, want := actual, expected; !reflect.DeepEqual(got, want) {
   101  		t.Errorf("context does not match the expected one\nactual:   %v\nexpected: %v", got, want)
   102  	}
   103  }
   104  
   105  func TestWith_NilError(t *testing.T) {
   106  	err := With(nil)
   107  
   108  	if err != nil {
   109  		t.Errorf("error is expected to be nil\nactual: %v", err)
   110  	}
   111  }
   112  

View as plain text