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

     1  package errors
     2  
     3  import "testing"
     4  
     5  func TestArgEmpty(t *testing.T) {
     6  	var err error = Arg("")
     7  	assert(t, "arg error", err.Error())
     8  
     9  	inner := Unwrap(err)
    10  	assert(t, nil, inner)
    11  	assert(t, false, IsArgumentUnspecifiedError(err))
    12  }
    13  
    14  func TestArgFields(t *testing.T) {
    15  	var err error = Arg("foo").Fieldset(
    16  		N("name").Desc(ErrValueEmpty), N("bar").Desc(ErrValueUnspecified))
    17  	assert(t, "arg foo: name: empty, bar: unspecified", err.Error())
    18  }
    19  
    20  func TestArgFieldsNoName(t *testing.T) {
    21  	var err error = Arg("").Fieldset(
    22  		N("name").Desc(ErrValueEmpty), N("bar").Desc(ErrValueUnspecified))
    23  	assert(t, "arg: name: empty, bar: unspecified", err.Error())
    24  }
    25  
    26  func TestArg1(t *testing.T) {
    27  	var err error = Arg1().Desc(ErrValueUnspecified)
    28  	assert(t, "arg unspecified", err.Error())
    29  }
    30  
    31  func TestArgDescWrap(t *testing.T) {
    32  	inner := Msg("inner")
    33  	var err error = Arg("foo").Desc(ErrValueMalformed).Wrap(inner)
    34  	assert(t, "arg foo: malformed: inner", err.Error())
    35  	assert(t, true, ArgumentErrorCheck(err).IsTrue())
    36  	assert(t, true, ArgumentErrorCheck(err).HasName("foo").IsTrue())
    37  	assert(t, false, ArgumentErrorCheck(err).HasName("bar").IsTrue())
    38  	assert(t, true, ArgumentErrorCheck(err).HasName("foo").HasDesc(ErrValueMalformed).IsTrue())
    39  	assert(t, false, ArgumentErrorCheck(err).HasName("foo").HasDesc(ErrValueEmpty).IsTrue())
    40  	assert(t, false, ArgumentErrorCheck(err).HasName("bar").HasDesc(ErrValueMalformed).IsTrue())
    41  	assert(t, false, ArgumentErrorCheck(err).HasName("bar").HasDesc(ErrValueEmpty).IsTrue())
    42  	assert(t, true, ArgumentErrorCheck(err).HasName("foo").HasDesc(ErrValueMalformed).HasWrapped(inner).IsTrue())
    43  	assert(t, false, ArgumentErrorCheck(err).HasWrapped(nil).IsTrue())
    44  	assert(t, false, ArgumentErrorCheck(inner).IsTrue())
    45  
    46  	wrapped := Unwrap(err)
    47  	assertNotEqual(t, nil, wrapped)
    48  	assert(t, inner, wrapped)
    49  
    50  	assert(t, true, HasDescriptor(err, ErrValueMalformed))
    51  
    52  	desc := UnwrapDescriptor(err)
    53  	assertNotEqual(t, nil, desc)
    54  	assert(t, ErrValueMalformed, desc)
    55  }
    56  
    57  func TestArgDescMsg(t *testing.T) {
    58  	const customDesc = "custom descriptor"
    59  	var err error = Arg("saturn").DescMsg(customDesc)
    60  	assert(t, "arg saturn: custom descriptor", err.Error())
    61  	assert(t, true, HasDescriptorText(err, customDesc))
    62  
    63  	desc := UnwrapDescriptor(err)
    64  	assertNotEqual(t, nil, desc)
    65  	assert(t, customDesc, desc.Error())
    66  }
    67  
    68  func TestAsArgumentError(t *testing.T) {
    69  	var err error = Arg1()
    70  	assert(t, err, AsArgumentError(err))
    71  }
    72  
    73  func TestAsArgumentErrorNil(t *testing.T) {
    74  	var err error
    75  	assert(t, nil, AsArgumentError(err))
    76  }
    77  
    78  func TestAsArgumentErrorNegative(t *testing.T) {
    79  	var err error = N("")
    80  	assert(t, nil, AsArgumentError(err))
    81  }
    82  
    83  func TestArgUnspecifiedEmpty(t *testing.T) {
    84  	var err error = ArgUnspecified("")
    85  
    86  	assert(t, "arg unspecified", err.Error())
    87  	assert(t, true, IsArgumentUnspecifiedError(err))
    88  	assert(t, ErrValueUnspecified, UnwrapDescriptor(err))
    89  
    90  	argErr, ok := err.(ArgumentError)
    91  	assert(t, true, ok)
    92  	assertNotEqual(t, nil, argErr)
    93  	assert(t, argErr, argErr.CallError())
    94  	assert(t, "", argErr.ArgumentName())
    95  
    96  	argErrB := AsArgumentError(err)
    97  	assertNotEqual(t, nil, argErrB)
    98  	assert(t, true, argErr == argErrB)
    99  	assert(t, argErr, argErrB)
   100  
   101  	wrapped := Unwrap(err)
   102  	assert(t, nil, wrapped)
   103  
   104  	desc := UnwrapDescriptor(err)
   105  	assertNotEqual(t, nil, desc)
   106  	assert(t, ErrValueUnspecified, desc)
   107  }
   108  
   109  func TestArgUnspecifiedFoo(t *testing.T) {
   110  	var err error = ArgUnspecified("foo")
   111  	assert(t, "arg foo: unspecified", err.Error())
   112  	assert(t, true, IsArgumentUnspecifiedError(err))
   113  	assert(t, true, IsArgumentUnspecified(err, "foo"))
   114  	assert(t, false, IsArgumentUnspecified(err, "bar"))
   115  
   116  	argErr, ok := err.(ArgumentError)
   117  	assert(t, true, ok)
   118  	assertNotEqual(t, nil, argErr)
   119  	assert(t, "foo", argErr.ArgumentName())
   120  	assert(t, true, IsArgumentUnspecifiedError(err))
   121  	assert(t, ErrValueUnspecified, UnwrapDescriptor(err))
   122  
   123  	wrapped := Unwrap(err)
   124  	assert(t, nil, wrapped)
   125  
   126  	desc := UnwrapDescriptor(err)
   127  	assertNotEqual(t, nil, desc)
   128  	assert(t, ErrValueUnspecified, desc)
   129  }
   130  
   131  func TestIsArgUnspecifiedErrorNil(t *testing.T) {
   132  	var err error
   133  	assert(t, false, IsArgumentUnspecifiedError(err))
   134  	assert(t, false, IsArgumentUnspecified(err, "foo"))
   135  }
   136  
   137  func TestIsArgUnspecifiedNegative(t *testing.T) {
   138  	var err error = ErrValueInvalid
   139  	assert(t, false, IsArgumentUnspecified(err, "foo"))
   140  }
   141  
   142  func TestIsArgUnspecifiedWrongArgName(t *testing.T) {
   143  	var err error = ArgUnspecified("foo")
   144  	assert(t, false, IsArgumentUnspecified(err, "bar"))
   145  }
   146  
   147  func TestIsArgUnspecifiedCustomStruct(t *testing.T) {
   148  	var err error = &customArgError{argName: "foo"}
   149  	assert(t, false, IsArgumentUnspecified(err, "foo"))
   150  }
   151  
   152  func TestArgValueUnsupportedNoName(t *testing.T) {
   153  	var err error = ArgValueUnsupported("")
   154  
   155  	//TODO: should be "arg value unsupported"
   156  	assert(t, "arg unsupported", err.Error())
   157  	assert(t, nil, Unwrap(err))
   158  	assert(t, ErrValueUnsupported, UnwrapDescriptor(err))
   159  
   160  	argErr, ok := err.(ArgumentError)
   161  	assert(t, true, ok)
   162  	assertNotEqual(t, nil, argErr)
   163  	assert(t, "", argErr.ArgumentName())
   164  }
   165  
   166  func TestArgValueUnsupportedFoo(t *testing.T) {
   167  	var err error = ArgValueUnsupported("foo")
   168  	assert(t, "arg foo: unsupported", err.Error())
   169  	// assert(t, true, IsArgumentUnspecifiedError(err))
   170  	// assert(t, true, IsArgumentUnspecified(err, "foo"))
   171  	// assert(t, false, IsArgumentUnspecified(err, "bar"))
   172  
   173  	argErr, ok := err.(ArgumentError)
   174  	assert(t, true, ok)
   175  	assertNotEqual(t, nil, argErr)
   176  	assert(t, "foo", argErr.ArgumentName())
   177  	// assert(t, true, IsArgumentUnspecifiedError(err))
   178  	assert(t, ErrValueUnsupported, UnwrapDescriptor(err))
   179  
   180  	wrapped := Unwrap(err)
   181  	assert(t, nil, wrapped)
   182  
   183  	desc := UnwrapDescriptor(err)
   184  	assertNotEqual(t, nil, desc)
   185  	assert(t, ErrValueUnsupported, desc)
   186  }
   187  
   188  func TestArgRewrapNil(t *testing.T) {
   189  	var err error = Arg("foo").Rewrap(nil)
   190  	assert(t, "arg foo", err.Error())
   191  	assert(t, true, IsArgumentError(err))
   192  	assert(t, false, IsEntityError(err))
   193  	assert(t, nil, UnwrapDescriptor(err))
   194  	assert(t, nil, Unwrap(err))
   195  }
   196  
   197  func TestArgRewrapDesc(t *testing.T) {
   198  	var err error = Arg("foo").Rewrap(ErrValueMalformed)
   199  	assert(t, "arg foo: malformed", err.Error())
   200  	assert(t, true, IsArgumentError(err))
   201  	assert(t, false, IsEntityError(err))
   202  	assertNotEqual(t, nil, UnwrapDescriptor(err))
   203  	assert(t, ErrValueMalformed, UnwrapDescriptor(err))
   204  	assert(t, nil, Unwrap(err))
   205  }
   206  
   207  func TestArgRewrapDescWrapped(t *testing.T) {
   208  	var err error = Arg("foo").Rewrap(Arg1().Desc(ErrValueMalformed).Wrap(Msg("bar")))
   209  	assert(t, "arg foo: malformed: bar", err.Error())
   210  	assert(t, true, IsArgumentError(err))
   211  	assert(t, false, IsEntityError(err))
   212  	assertNotEqual(t, nil, UnwrapDescriptor(err))
   213  	assert(t, ErrValueMalformed, UnwrapDescriptor(err))
   214  	assertNotEqual(t, nil, Unwrap(err))
   215  	assert(t, "bar", Unwrap(err).Error())
   216  }
   217  
   218  func TestArgRewrapRandom(t *testing.T) {
   219  	var err error = Arg("foo").Rewrap(Msg("bar"))
   220  	assert(t, "arg foo", err.Error())
   221  	assert(t, true, IsArgumentError(err))
   222  	assert(t, false, IsEntityError(err))
   223  	assert(t, nil, UnwrapDescriptor(err))
   224  	assert(t, nil, Unwrap(err))
   225  }
   226  
   227  func TestArgRewrapWrappedNoDesc(t *testing.T) {
   228  	var err error = Arg("foo").Rewrap(Arg1().Wrap(Msg("bar")))
   229  	assert(t, "arg foo: bar", err.Error())
   230  	assert(t, true, IsArgumentError(err))
   231  	assert(t, false, IsEntityError(err))
   232  	assert(t, nil, UnwrapDescriptor(err))
   233  	assertNotEqual(t, nil, Unwrap(err))
   234  	assert(t, "bar", Unwrap(err).Error())
   235  }
   236  
   237  func TestArgRewrapFields(t *testing.T) {
   238  	var err error = Arg("simple").Rewrap(Arg1().Fieldset(
   239  		NamedValueUnsupported("foo"),
   240  		N("bar").Desc(ErrValueMalformed),
   241  	))
   242  	assert(t, "arg simple: foo: unsupported, bar: malformed", err.Error())
   243  	assert(t, true, IsArgumentError(err))
   244  	assert(t, false, IsEntityError(err))
   245  	assert(t, nil, UnwrapDescriptor(err))
   246  	assert(t, nil, Unwrap(err))
   247  	assert(t, 2, len(UnwrapFieldErrors(err)))
   248  	assert(t, true, ArgumentErrorCheck(err).IsTrue())
   249  	assert(t, true, ArgumentErrorCheck(err).HasName("simple").IsTrue())
   250  	assert(t, true, ArgumentErrorCheck(err).HasName("simple").HasDesc(nil).IsTrue())
   251  	assert(t, true, ArgumentErrorCheck(err).HasName("simple").HasDesc(nil).HasWrapped(nil).IsTrue())
   252  }
   253  
   254  func TestArgHint(t *testing.T) {
   255  	var err error = Arg("color").Hint("Check the color of the sky right now")
   256  
   257  	assert(t, "arg color. Check the color of the sky right now", err.Error())
   258  }
   259  
   260  func TestArgHintDesc(t *testing.T) {
   261  	var err error = ArgUnspecified("color").Hint("Check the color of the ocean right now")
   262  
   263  	assert(t, "arg color: unspecified. Check the color of the ocean right now", err.Error())
   264  }
   265  
   266  func TestArgHintWrap(t *testing.T) {
   267  	var err error = Arg("color").Wrap(Msg("hex invalid")).Hint("Check the color of your right iris")
   268  
   269  	assert(t, "arg color: hex invalid. Check the color of your right iris", err.Error())
   270  }
   271  
   272  // ----
   273  
   274  type customArgError struct {
   275  	argName string
   276  }
   277  
   278  var (
   279  	_ ArgumentError = &customArgError{}
   280  )
   281  
   282  func (e *customArgError) ArgumentName() string     { return e.argName }
   283  func (e *customArgError) Error() string            { return "custom arg error" }
   284  func (e *customArgError) CallError() CallError     { return e }
   285  func (e *customArgError) Unwrap() error            { return nil }
   286  func (customArgError) Descriptor() ErrorDescriptor { return nil }
   287  func (e customArgError) FieldErrors() []NamedError { return nil }