...

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

Documentation: github.com/goph/emperror

     1  package emperror
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  func TestWrapWith_Format(t *testing.T) {
    12  	testError := WrapWith(io.EOF, "error1", "key", "value")
    13  
    14  	tests := []struct {
    15  		error
    16  		format string
    17  		want   string
    18  	}{{
    19  		WrapWith(errors.New("error"), "error2", "key", "value"),
    20  		"%s",
    21  		"error2: error",
    22  	}, {
    23  		WrapWith(errors.New("error"), "error2", "key", "value"),
    24  		"%v",
    25  		"error2: error",
    26  	}, {
    27  		WrapWith(errors.New("error"), "error2", "key", "value"),
    28  		"%+v",
    29  		"error\n" +
    30  			"github.com/goph/emperror.TestWrapWith_Format\n" +
    31  			"\t.+/wrap_context_test.go:27",
    32  	}, {
    33  		WrapWith(io.EOF, "error", "key", "value"),
    34  		"%s",
    35  		"error: EOF",
    36  	}, {
    37  		WrapWith(io.EOF, "error", "key", "value"),
    38  		"%v",
    39  		"error: EOF",
    40  	}, {
    41  		WrapWith(io.EOF, "error", "key", "value"),
    42  		"%+v",
    43  		"EOF\n" +
    44  			"error\n" +
    45  			"github.com/goph/emperror.TestWrapWith_Format\n" +
    46  			"\t.+/wrap_context_test.go:41",
    47  	}, {
    48  		WrapWith(WrapWith(io.EOF, "error1"), "error2", "key", "value"),
    49  		"%+v",
    50  		"EOF\n" +
    51  			"error1\n" +
    52  			"github.com/goph/emperror.TestWrapWith_Format\n" +
    53  			"\t.+/wrap_context_test.go:48\n",
    54  	}, {
    55  		WrapWith(fmt.Errorf("error with space"), "context", "key", "value"),
    56  		"%q",
    57  		`"context: error with space"`,
    58  	}, {
    59  		WrapWith(testError, "error2", "key", "value"),
    60  		"%+v",
    61  		"EOF\n" +
    62  			"error1\n" +
    63  			"github.com/goph/emperror.TestWrapWith_Format\n" +
    64  			"\t.+/wrap_context_test.go:12\n",
    65  	}}
    66  
    67  	for i, tt := range tests {
    68  		testFormatRegexp(t, i, tt.error, tt.format, tt.want)
    69  	}
    70  }
    71  
    72  func TestWrapWith_Context(t *testing.T) {
    73  	err := errors.New("error")
    74  
    75  	kvs := []interface{}{"a", 123}
    76  	err = WrapWith(err, "error2", kvs...)
    77  	kvs[1] = 0 // WrapWith should copy its key values
    78  
    79  	ctx := Context(err)
    80  
    81  	if got, want := ctx[0], "a"; got != want {
    82  		t.Errorf("context value does not match the expected one\nactual:   %s\nexpected: %s", got, want)
    83  	}
    84  
    85  	if got, want := ctx[1], 123; got != want {
    86  		t.Errorf("context value does not match the expected one\nactual:   %s\nexpected: %d", got, want)
    87  	}
    88  
    89  	if got, want := err.Error(), "error2: error"; got != want {
    90  		t.Errorf("error does not match the expected one\nactual:   %s\nexpected: %s", got, want)
    91  	}
    92  }
    93  

View as plain text