github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/juju/errors/error_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the LGPLv3, see LICENCE file for details.
     3  
     4  package errors_test
     5  
     6  import (
     7  	"fmt"
     8  	"runtime"
     9  
    10  	jc "github.com/insionng/yougam/libraries/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/insionng/yougam/libraries/juju/errors"
    14  )
    15  
    16  type errorsSuite struct{}
    17  
    18  var _ = gc.Suite(&errorsSuite{})
    19  
    20  var someErr = errors.New("some error") //err varSomeErr
    21  
    22  func (*errorsSuite) TestErrorString(c *gc.C) {
    23  	for i, test := range []struct {
    24  		message   string
    25  		generator func() error
    26  		expected  string
    27  	}{
    28  		{
    29  			message: "uncomparable errors",
    30  			generator: func() error {
    31  				err := errors.Annotatef(newNonComparableError("uncomparable"), "annotation")
    32  				return errors.Annotatef(err, "another")
    33  			},
    34  			expected: "another: annotation: uncomparable",
    35  		}, {
    36  			message: "Errorf",
    37  			generator: func() error {
    38  				return errors.Errorf("first error")
    39  			},
    40  			expected: "first error",
    41  		}, {
    42  			message: "annotated error",
    43  			generator: func() error {
    44  				err := errors.Errorf("first error")
    45  				return errors.Annotatef(err, "annotation")
    46  			},
    47  			expected: "annotation: first error",
    48  		}, {
    49  			message: "test annotation format",
    50  			generator: func() error {
    51  				err := errors.Errorf("first %s", "error")
    52  				return errors.Annotatef(err, "%s", "annotation")
    53  			},
    54  			expected: "annotation: first error",
    55  		}, {
    56  			message: "wrapped error",
    57  			generator: func() error {
    58  				err := newError("first error")
    59  				return errors.Wrap(err, newError("detailed error"))
    60  			},
    61  			expected: "detailed error",
    62  		}, {
    63  			message: "wrapped annotated error",
    64  			generator: func() error {
    65  				err := errors.Errorf("first error")
    66  				err = errors.Annotatef(err, "annotated")
    67  				return errors.Wrap(err, fmt.Errorf("detailed error"))
    68  			},
    69  			expected: "detailed error",
    70  		}, {
    71  			message: "annotated wrapped error",
    72  			generator: func() error {
    73  				err := errors.Errorf("first error")
    74  				err = errors.Wrap(err, fmt.Errorf("detailed error"))
    75  				return errors.Annotatef(err, "annotated")
    76  			},
    77  			expected: "annotated: detailed error",
    78  		}, {
    79  			message: "traced, and annotated",
    80  			generator: func() error {
    81  				err := errors.New("first error")
    82  				err = errors.Trace(err)
    83  				err = errors.Annotate(err, "some context")
    84  				err = errors.Trace(err)
    85  				err = errors.Annotate(err, "more context")
    86  				return errors.Trace(err)
    87  			},
    88  			expected: "more context: some context: first error",
    89  		}, {
    90  			message: "traced, and annotated, masked and annotated",
    91  			generator: func() error {
    92  				err := errors.New("first error")
    93  				err = errors.Trace(err)
    94  				err = errors.Annotate(err, "some context")
    95  				err = errors.Maskf(err, "masked")
    96  				err = errors.Annotate(err, "more context")
    97  				return errors.Trace(err)
    98  			},
    99  			expected: "more context: masked: some context: first error",
   100  		},
   101  	} {
   102  		c.Logf("%v: %s", i, test.message)
   103  		err := test.generator()
   104  		ok := c.Check(err.Error(), gc.Equals, test.expected)
   105  		if !ok {
   106  			c.Logf("%#v", test.generator())
   107  		}
   108  	}
   109  }
   110  
   111  type embed struct {
   112  	errors.Err
   113  }
   114  
   115  func newEmbed(format string, args ...interface{}) *embed {
   116  	err := &embed{errors.NewErr(format, args...)}
   117  	err.SetLocation(1)
   118  	return err
   119  }
   120  
   121  func (*errorsSuite) TestNewErr(c *gc.C) {
   122  	if runtime.Compiler == "gccgo" {
   123  		c.Skip("gccgo can't determine the location")
   124  	}
   125  	err := newEmbed("testing %d", 42) //err embedErr
   126  	c.Assert(err.Error(), gc.Equals, "testing 42")
   127  	c.Assert(errors.Cause(err), gc.Equals, err)
   128  	c.Assert(errors.Details(err), jc.Contains, tagToLocation["embedErr"].String())
   129  }
   130  
   131  func newEmbedWithCause(other error, format string, args ...interface{}) *embed {
   132  	err := &embed{errors.NewErrWithCause(other, format, args...)}
   133  	err.SetLocation(1)
   134  	return err
   135  }
   136  
   137  func (*errorsSuite) TestNewErrWithCause(c *gc.C) {
   138  	if runtime.Compiler == "gccgo" {
   139  		c.Skip("gccgo can't determine the location")
   140  	}
   141  	causeErr := fmt.Errorf("external error")
   142  	err := newEmbedWithCause(causeErr, "testing %d", 43) //err embedCause
   143  	c.Assert(err.Error(), gc.Equals, "testing 43: external error")
   144  	c.Assert(errors.Cause(err), gc.Equals, causeErr)
   145  	c.Assert(errors.Details(err), jc.Contains, tagToLocation["embedCause"].String())
   146  }
   147  
   148  var _ error = (*embed)(nil)
   149  
   150  // This is an uncomparable error type, as it is a struct that supports the
   151  // error interface (as opposed to a pointer type).
   152  type error_ struct {
   153  	info  string
   154  	slice []string
   155  }
   156  
   157  // Create a non-comparable error
   158  func newNonComparableError(message string) error {
   159  	return error_{info: message}
   160  }
   161  
   162  func (e error_) Error() string {
   163  	return e.info
   164  }
   165  
   166  func newError(message string) error {
   167  	return testError{message}
   168  }
   169  
   170  // The testError is a value type error for ease of seeing results
   171  // when the test fails.
   172  type testError struct {
   173  	message string
   174  }
   175  
   176  func (e testError) Error() string {
   177  	return e.message
   178  }