github.com/nikandfor/assert@v0.0.0-20231112165957-bf2ce0a3555a/is/is.go (about)

     1  package is
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"reflect"
     8  )
     9  
    10  type (
    11  	Checker interface {
    12  		Check(io.Writer) bool
    13  	}
    14  
    15  	CheckerFunc func(w io.Writer) bool
    16  
    17  	equal struct {
    18  		a, b interface{}
    19  	}
    20  )
    21  
    22  func True(ok bool) Checker {
    23  	return CheckerFunc(func(w io.Writer) bool {
    24  		if ok {
    25  			return true
    26  		}
    27  
    28  		fmt.Fprintf(w, "Want true")
    29  
    30  		return false
    31  	})
    32  }
    33  
    34  func False(ok bool) Checker {
    35  	return CheckerFunc(func(w io.Writer) bool {
    36  		if !ok {
    37  			return true
    38  		}
    39  
    40  		fmt.Fprintf(w, "Want false")
    41  
    42  		return false
    43  	})
    44  }
    45  
    46  func Nil(x interface{}) Checker {
    47  	return CheckerFunc(func(w io.Writer) bool {
    48  		if x == nil {
    49  			return true
    50  		}
    51  
    52  		r := reflect.ValueOf(x)
    53  
    54  		if k := r.Kind(); k == reflect.Ptr || k == reflect.Interface || k == reflect.Slice || k == reflect.Map || k == reflect.Chan || k == reflect.Func {
    55  			return r.IsNil()
    56  		}
    57  
    58  		fmt.Fprintf(w, "Want nil, got: %v", x)
    59  
    60  		return false
    61  	})
    62  }
    63  
    64  func NotNil(x interface{}) Checker {
    65  	return CheckerFunc(func(w io.Writer) bool {
    66  		if x != nil {
    67  			return true
    68  		}
    69  
    70  		fmt.Fprintf(w, "Want not nil")
    71  
    72  		return false
    73  	})
    74  }
    75  
    76  func NoError(err error) Checker {
    77  	return CheckerFunc(func(w io.Writer) bool {
    78  		if err == nil {
    79  			return true
    80  		}
    81  
    82  		fmt.Fprintf(w, "Error: %+v (type: %[1]T)", err)
    83  
    84  		return false
    85  	})
    86  }
    87  
    88  func Error(err error) Checker {
    89  	return CheckerFunc(func(w io.Writer) bool {
    90  		if err != nil {
    91  			return true
    92  		}
    93  
    94  		fmt.Fprintf(w, "Want error")
    95  
    96  		return false
    97  	})
    98  }
    99  
   100  func ErrorIs(err, target error) Checker {
   101  	return CheckerFunc(func(w io.Writer) bool {
   102  		if errors.Is(err, target) {
   103  			return true
   104  		}
   105  
   106  		if err == nil {
   107  			fmt.Fprintf(w, "Want error: %q (type %T)", target.Error(), target)
   108  
   109  			return false
   110  		}
   111  
   112  		fmt.Fprintf(w, "Error chain\n")
   113  
   114  		for e := err; e != nil; e = errors.Unwrap(e) {
   115  			fmt.Fprintf(w, "%q (type %T)\n", e.Error(), e)
   116  		}
   117  
   118  		fmt.Fprintf(w, "is not %q (type %T)", target.Error(), target)
   119  
   120  		return false
   121  	})
   122  }
   123  
   124  func Zero(x interface{}) Checker {
   125  	return CheckerFunc(func(w io.Writer) bool {
   126  		if x == nil {
   127  			return true
   128  		}
   129  
   130  		r := reflect.ValueOf(x)
   131  
   132  		if r.IsZero() {
   133  			return true
   134  		}
   135  
   136  		fmt.Fprintf(w, "Want zero value, got: %v", x)
   137  
   138  		return false
   139  	})
   140  }
   141  
   142  func NotZero(x interface{}) Checker {
   143  	return CheckerFunc(func(w io.Writer) bool {
   144  		r := reflect.ValueOf(x)
   145  
   146  		if !r.IsZero() {
   147  			return true
   148  		}
   149  
   150  		fmt.Fprintf(w, "Want not zero value, got: %v", x)
   151  
   152  		return false
   153  	})
   154  }
   155  
   156  func (f CheckerFunc) Check(w io.Writer) bool { return f(w) }