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

     1  package assert
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/nikandfor/assert/is"
     7  )
     8  
     9  type (
    10  	TestingT interface{}
    11  
    12  	Checker = is.Checker
    13  
    14  	helper interface {
    15  		Helper()
    16  	}
    17  
    18  	fail interface {
    19  		Fail()
    20  	}
    21  
    22  	wbuf []byte
    23  )
    24  
    25  func Eval(t TestingT, c Checker, args ...interface{}) (ok bool) {
    26  	if h, ok := t.(helper); ok {
    27  		h.Helper()
    28  	}
    29  
    30  	//	fargs := source.AssertionArgs(t)
    31  
    32  	var b wbuf
    33  
    34  	if c.Check(&b) {
    35  		return true
    36  	}
    37  
    38  	Fail(t, append([]interface{}{b}, args...)...)
    39  
    40  	return false
    41  }
    42  
    43  func Any(t TestingT, c []Checker, args ...interface{}) bool {
    44  	if h, ok := t.(helper); ok {
    45  		h.Helper()
    46  	}
    47  
    48  	bs := make([]wbuf, len(c))
    49  
    50  	for i, c := range c {
    51  		if c.Check(&bs[i]) {
    52  			return true
    53  		}
    54  	}
    55  
    56  	Fail(t, append([]interface{}{bs}, args...)...)
    57  
    58  	return false
    59  }
    60  
    61  func All(t TestingT, c []Checker, args ...interface{}) (ok bool) {
    62  	if h, ok := t.(helper); ok {
    63  		h.Helper()
    64  	}
    65  
    66  	bs := make([]wbuf, len(c))
    67  
    68  	ok = true
    69  
    70  	for i, c := range c {
    71  		if !c.Check(&bs[i]) {
    72  			ok = false
    73  			break
    74  		}
    75  	}
    76  
    77  	if !ok {
    78  		Fail(t, append([]interface{}{bs}, args...)...)
    79  	}
    80  
    81  	return ok
    82  }
    83  
    84  func Fail(t TestingT, args ...interface{}) {
    85  	if h, ok := t.(helper); ok {
    86  		h.Helper()
    87  	}
    88  
    89  	var b wbuf
    90  
    91  loop:
    92  	for i, a := range args {
    93  		switch a := a.(type) {
    94  		case wbuf:
    95  			b = append(b, a...)
    96  			b.Newline()
    97  		case []wbuf:
    98  			for _, a := range a {
    99  				b = append(b, a...)
   100  				b.Newline()
   101  			}
   102  		case string:
   103  			fmt.Fprintf(&b, a, args[i+1:]...)
   104  
   105  			b.Newline()
   106  
   107  			break loop
   108  		}
   109  	}
   110  
   111  	switch t := t.(type) {
   112  	case interface{ Logf(string, ...interface{}) }:
   113  		t.Logf("%s", b)
   114  	case interface{ Log(...interface{}) }:
   115  		t.Log(string(b))
   116  	default:
   117  		panic(fmt.Sprintf("unsupported testing.T: %T", t))
   118  	}
   119  
   120  	if t, ok := t.(fail); ok {
   121  		t.Fail()
   122  	}
   123  }
   124  
   125  func True(t TestingT, ok bool, args ...interface{}) bool {
   126  	if h, ok := t.(helper); ok {
   127  		h.Helper()
   128  	}
   129  
   130  	return Eval(t, is.True(ok), args...)
   131  }
   132  
   133  func False(t TestingT, ok bool, args ...interface{}) bool {
   134  	if h, ok := t.(helper); ok {
   135  		h.Helper()
   136  	}
   137  
   138  	return Eval(t, is.False(ok), args...)
   139  }
   140  
   141  func Nil(t TestingT, x interface{}, args ...interface{}) bool {
   142  	if h, ok := t.(helper); ok {
   143  		h.Helper()
   144  	}
   145  
   146  	return Eval(t, is.Nil(x), args...)
   147  }
   148  
   149  func NotNil(t TestingT, x interface{}, args ...interface{}) bool {
   150  	if h, ok := t.(helper); ok {
   151  		h.Helper()
   152  	}
   153  
   154  	return Eval(t, is.NotNil(x), args...)
   155  }
   156  
   157  func NoError(t TestingT, err error, args ...interface{}) bool {
   158  	if h, ok := t.(helper); ok {
   159  		h.Helper()
   160  	}
   161  
   162  	return Eval(t, is.NoError(err), args...)
   163  }
   164  
   165  func Error(t TestingT, err error, args ...interface{}) bool {
   166  	if h, ok := t.(helper); ok {
   167  		h.Helper()
   168  	}
   169  
   170  	return Eval(t, is.Error(err), args...)
   171  }
   172  
   173  func ErrorIs(t TestingT, err, target error, args ...interface{}) bool {
   174  	if h, ok := t.(helper); ok {
   175  		h.Helper()
   176  	}
   177  
   178  	return Eval(t, is.ErrorIs(err, target), args...)
   179  }
   180  
   181  func Equal(t TestingT, exp, act interface{}, args ...interface{}) bool {
   182  	if h, ok := t.(helper); ok {
   183  		h.Helper()
   184  	}
   185  
   186  	return Eval(t, is.Equal(exp, act), args...)
   187  }
   188  
   189  func NotEqual(t TestingT, exp, act interface{}, args ...interface{}) bool {
   190  	if h, ok := t.(helper); ok {
   191  		h.Helper()
   192  	}
   193  
   194  	return Eval(t, is.NotEqual(exp, act), args...)
   195  }
   196  
   197  func Zero(t TestingT, val interface{}, args ...interface{}) bool {
   198  	if h, ok := t.(helper); ok {
   199  		h.Helper()
   200  	}
   201  
   202  	return Eval(t, is.Zero(val), args...)
   203  }
   204  
   205  func NotZero(t TestingT, val interface{}, args ...interface{}) bool {
   206  	if h, ok := t.(helper); ok {
   207  		h.Helper()
   208  	}
   209  
   210  	return Eval(t, is.NotZero(val), args...)
   211  }
   212  
   213  func (w *wbuf) Write(p []byte) (int, error) {
   214  	*w = append(*w, p...)
   215  
   216  	return len(p), nil
   217  }
   218  
   219  func (w *wbuf) Newline() {
   220  	if l := len(*w); l == 0 || (*w)[l-1] == '\n' {
   221  		return
   222  	}
   223  
   224  	*w = append(*w, '\n')
   225  }