github.com/alloyzeus/go-azfl@v0.0.0-20231220071816-9740126a2d07/errors/context_test.go (about)

     1  package errors
     2  
     3  import "testing"
     4  
     5  func TestContextEmpty(t *testing.T) {
     6  	var err error = Context(nil)
     7  	if err.Error() != "context invalid" {
     8  		t.Errorf(`err.Error() != "context invalid" -- %q`, err.Error())
     9  	}
    10  	inner := Unwrap(err)
    11  	if inner != nil {
    12  		t.Error("inner != nil")
    13  	}
    14  }
    15  
    16  func TestContextConstantDescriptor(t *testing.T) {
    17  	var err error = Context(ErrValueUnspecified, nil)
    18  	if err.Error() != "context unspecified" {
    19  		t.Errorf(`err.Error() != "context unspecified" -- %q`, err.Error())
    20  	}
    21  	if d, ok := err.(hasDescriptor); !ok {
    22  		t.Error("err.(hasDescriptor)")
    23  	} else {
    24  		desc := d.Descriptor()
    25  		if desc == nil {
    26  			t.Error("desc == nil")
    27  		}
    28  		if desc != ErrValueUnspecified {
    29  			t.Error("desc != ErrValueUnspecified")
    30  		}
    31  	}
    32  	if !IsContextUnspecifiedError(err) {
    33  		t.Error("!IsContextUnspecified(err)")
    34  	}
    35  }
    36  
    37  func TestContextWrappedConstantDescriptor(t *testing.T) {
    38  	var err error = Context(DescWrap(ErrValueUnspecified, nil))
    39  	if err.Error() != "context unspecified" {
    40  		t.Errorf(`err.Error() != "context unspecified" -- %q`, err.Error())
    41  	}
    42  	if d, ok := err.(hasDescriptor); !ok {
    43  		t.Error("err.(hasDescriptor)")
    44  	} else {
    45  		desc := d.Descriptor()
    46  		if desc == nil {
    47  			t.Error("desc == nil")
    48  		}
    49  		if desc != ErrValueUnspecified {
    50  			t.Error("desc != ErrValueUnspecified")
    51  		}
    52  	}
    53  	if !IsContextUnspecifiedError(err) {
    54  		t.Error("!IsContextUnspecified(err)")
    55  	}
    56  }
    57  
    58  func TestContextUnspecified(t *testing.T) {
    59  	var err error = ContextUnspecified()
    60  	if err.Error() != "context unspecified" {
    61  		t.Errorf(`err.Error() != "context unspecified" -- %q`, err.Error())
    62  	}
    63  	if ctxErr, ok := err.(ContextError); !ok {
    64  		t.Error("err.(ContextError)")
    65  	} else {
    66  		if ctxErr == nil {
    67  			t.Error("argErr == nil")
    68  		}
    69  	}
    70  	inner := Unwrap(err)
    71  	if inner == nil {
    72  		t.Error("inner == nil")
    73  	}
    74  	if inner != ErrValueUnspecified {
    75  		t.Error("inner != DataErrUnspecified")
    76  	}
    77  	if d, ok := err.(hasDescriptor); !ok {
    78  		t.Error("err.(hasDescriptor)")
    79  	} else {
    80  		desc := d.Descriptor()
    81  		if desc == nil {
    82  			t.Error("desc == nil")
    83  		}
    84  		if desc != ErrValueUnspecified {
    85  			t.Error("desc != ErrValueUnspecified")
    86  		}
    87  	}
    88  }
    89  
    90  func TestContextUnspecifiedCustomBare(t *testing.T) {
    91  	var err error = Context(ErrValueUnspecified)
    92  	if !IsContextUnspecifiedError(err) {
    93  		t.Errorf("!IsContextUnspecified(err)")
    94  	}
    95  }
    96  
    97  // Ensure that the descriptor is for the context, not for others.
    98  func TestContextUnspecifiedCustomWrap(t *testing.T) {
    99  	var err error = Context(Wrap("", ErrValueUnspecified))
   100  	if IsContextUnspecifiedError(err) {
   101  		t.Errorf("IsContextUnspecified(err)")
   102  	}
   103  }
   104  
   105  func TestContextFelds(t *testing.T) {
   106  	var err error = ContextFields(Ent("authorization", ErrValueUnspecified))
   107  	if err.Error() != "context invalid: authorization: unspecified" {
   108  		t.Errorf(`err.Error() != "context invalid: authorization: unspecified" -- %q`, err.Error())
   109  	}
   110  }
   111  
   112  func TestIsContextErrorNil(t *testing.T) {
   113  	var err error
   114  	if IsContextError(err) {
   115  		t.Error("IsContextError(err)")
   116  	}
   117  }
   118  
   119  func TestIsContextErrorNegative(t *testing.T) {
   120  	var err error = ErrValueUnspecified
   121  	if IsContextError(err) {
   122  		t.Error("IsContextError(err)")
   123  	}
   124  }
   125  
   126  func TestIsContextUnspecifiedErrorNil(t *testing.T) {
   127  	var err error
   128  	if IsContextUnspecifiedError(err) {
   129  		t.Error("IsContextUnspecifiedError(err)")
   130  	}
   131  }
   132  
   133  func TestIsContextUnspecifiedErrorNegative(t *testing.T) {
   134  	var err error = ErrValueUnspecified
   135  	if IsContextUnspecifiedError(err) {
   136  		t.Error("IsContextUnspecifiedError(err)")
   137  	}
   138  }