github.com/ssgreg/logf@v1.4.1/common_test.go (about)

     1  package logf
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  	"unsafe"
     7  )
     8  
     9  // testAppender
    10  type testAppender struct {
    11  	Entries          []Entry
    12  	FlushCallCounter int
    13  	SyncCallCounter  int
    14  
    15  	AppendError error
    16  	FlushError  error
    17  	SyncError   error
    18  }
    19  
    20  func (a *testAppender) Append(e Entry) error {
    21  	if a.AppendError != nil {
    22  		return a.AppendError
    23  	}
    24  	a.Entries = append(a.Entries, e)
    25  
    26  	return nil
    27  }
    28  
    29  func (a *testAppender) Flush() error {
    30  	if a.FlushError != nil {
    31  		return a.FlushError
    32  	}
    33  	a.FlushCallCounter++
    34  
    35  	return nil
    36  }
    37  
    38  func (a *testAppender) Sync() error {
    39  	if a.SyncError != nil {
    40  		return a.SyncError
    41  	}
    42  	a.SyncCallCounter++
    43  
    44  	return nil
    45  }
    46  
    47  // testEntryWriter implements EntryWriter storing the last Entry.
    48  type testEntryWriter struct {
    49  	Entry *Entry
    50  }
    51  
    52  func (w *testEntryWriter) WriteEntry(e Entry) {
    53  	w.Entry = &e
    54  }
    55  
    56  // testSnapshotter implements Snapshotter allowing to check whether
    57  // TakeSnapshot was called or not. TakeSnapshot returns new object of
    58  // this type.
    59  type testSnapshotter struct {
    60  	Called bool
    61  }
    62  
    63  func (s *testSnapshotter) TakeSnapshot() interface{} {
    64  	s.Called = true
    65  
    66  	return &testSnapshotter{}
    67  }
    68  
    69  // testLevelCheckerReturningFalse implements LevelCheckerGetter that always
    70  // returns false.
    71  type testLevelCheckerReturningFalse struct {
    72  }
    73  
    74  func (g testLevelCheckerReturningFalse) LevelChecker() LevelChecker {
    75  	return func(Level) bool {
    76  		return false
    77  	}
    78  }
    79  
    80  // testTypeEncoder implements TypeEncoder storing the last encoding value.
    81  type testTypeEncoder struct {
    82  	result interface{}
    83  }
    84  
    85  func (e *testTypeEncoder) EncodeTypeAny(v interface{}) {
    86  	e.result = v
    87  }
    88  
    89  func (e *testTypeEncoder) EncodeTypeBool(v bool) {
    90  	e.result = v
    91  }
    92  
    93  func (e *testTypeEncoder) EncodeTypeInt64(v int64) {
    94  	e.result = v
    95  }
    96  
    97  func (e *testTypeEncoder) EncodeTypeInt32(v int32) {
    98  	e.result = v
    99  }
   100  
   101  func (e *testTypeEncoder) EncodeTypeInt16(v int16) {
   102  	e.result = v
   103  }
   104  
   105  func (e *testTypeEncoder) EncodeTypeInt8(v int8) {
   106  	e.result = v
   107  }
   108  
   109  func (e *testTypeEncoder) EncodeTypeUint64(v uint64) {
   110  	e.result = v
   111  }
   112  
   113  func (e *testTypeEncoder) EncodeTypeUint32(v uint32) {
   114  	e.result = v
   115  }
   116  
   117  func (e *testTypeEncoder) EncodeTypeUint16(v uint16) {
   118  	e.result = v
   119  }
   120  
   121  func (e *testTypeEncoder) EncodeTypeUint8(v uint8) {
   122  	e.result = v
   123  }
   124  
   125  func (e *testTypeEncoder) EncodeTypeFloat64(v float64) {
   126  	e.result = v
   127  }
   128  
   129  func (e *testTypeEncoder) EncodeTypeFloat32(v float32) {
   130  	e.result = v
   131  }
   132  
   133  func (e *testTypeEncoder) EncodeTypeDuration(v time.Duration) {
   134  	e.result = v
   135  }
   136  
   137  func (e *testTypeEncoder) EncodeTypeTime(v time.Time) {
   138  	e.result = v
   139  }
   140  
   141  func (e *testTypeEncoder) EncodeTypeString(v string) {
   142  	e.result = v
   143  }
   144  
   145  func (e *testTypeEncoder) EncodeTypeStrings(v []string) {
   146  	e.result = v
   147  }
   148  
   149  func (e *testTypeEncoder) EncodeTypeBytes(v []byte) {
   150  	e.result = v
   151  }
   152  
   153  func (e *testTypeEncoder) EncodeTypeBools(v []bool) {
   154  	e.result = v
   155  }
   156  
   157  func (e *testTypeEncoder) EncodeTypeInts64(v []int64) {
   158  	e.result = v
   159  }
   160  
   161  func (e *testTypeEncoder) EncodeTypeInts32(v []int32) {
   162  	e.result = v
   163  }
   164  
   165  func (e *testTypeEncoder) EncodeTypeInts16(v []int16) {
   166  	e.result = v
   167  }
   168  
   169  func (e *testTypeEncoder) EncodeTypeInts8(v []int8) {
   170  	e.result = v
   171  }
   172  
   173  func (e *testTypeEncoder) EncodeTypeUints64(v []uint64) {
   174  	e.result = v
   175  }
   176  
   177  func (e *testTypeEncoder) EncodeTypeUints32(v []uint32) {
   178  	e.result = v
   179  }
   180  
   181  func (e *testTypeEncoder) EncodeTypeUints16(v []uint16) {
   182  	e.result = v
   183  }
   184  
   185  func (e *testTypeEncoder) EncodeTypeUints8(v []uint8) {
   186  	e.result = v
   187  }
   188  
   189  func (e *testTypeEncoder) EncodeTypeFloats64(v []float64) {
   190  	e.result = v
   191  }
   192  
   193  func (e *testTypeEncoder) EncodeTypeFloats32(v []float32) {
   194  	e.result = v
   195  }
   196  
   197  func (e *testTypeEncoder) EncodeTypeDurations(v []time.Duration) {
   198  	e.result = v
   199  }
   200  
   201  func (e *testTypeEncoder) EncodeTypeArray(v ArrayEncoder) {
   202  	e.result = v
   203  }
   204  
   205  func (e *testTypeEncoder) EncodeTypeObject(v ObjectEncoder) {
   206  	e.result = v
   207  }
   208  
   209  func (e *testTypeEncoder) EncodeTypeUnsafeBytes(v unsafe.Pointer) {
   210  	e.result = string(*(*[]byte)(v))
   211  }
   212  
   213  func newTestFieldEncoder() *testFieldEncoder {
   214  	return &testFieldEncoder{make(map[string]interface{})}
   215  }
   216  
   217  // testFieldEncoder implements FieldEncoder storing all fields to be encoded.
   218  type testFieldEncoder struct {
   219  	result map[string]interface{}
   220  }
   221  
   222  func (e *testFieldEncoder) EncodeFieldAny(k string, v interface{}) {
   223  	e.result[k] = v
   224  }
   225  
   226  func (e *testFieldEncoder) EncodeFieldBool(k string, v bool) {
   227  	e.result[k] = v
   228  }
   229  
   230  func (e *testFieldEncoder) EncodeFieldInt64(k string, v int64) {
   231  	e.result[k] = v
   232  }
   233  
   234  func (e *testFieldEncoder) EncodeFieldInt32(k string, v int32) {
   235  	e.result[k] = v
   236  }
   237  
   238  func (e *testFieldEncoder) EncodeFieldInt16(k string, v int16) {
   239  	e.result[k] = v
   240  }
   241  
   242  func (e *testFieldEncoder) EncodeFieldInt8(k string, v int8) {
   243  	e.result[k] = v
   244  }
   245  
   246  func (e *testFieldEncoder) EncodeFieldUint64(k string, v uint64) {
   247  	e.result[k] = v
   248  }
   249  
   250  func (e *testFieldEncoder) EncodeFieldUint32(k string, v uint32) {
   251  	e.result[k] = v
   252  }
   253  
   254  func (e *testFieldEncoder) EncodeFieldUint16(k string, v uint16) {
   255  	e.result[k] = v
   256  }
   257  
   258  func (e *testFieldEncoder) EncodeFieldUint8(k string, v uint8) {
   259  	e.result[k] = v
   260  }
   261  
   262  func (e *testFieldEncoder) EncodeFieldFloat64(k string, v float64) {
   263  	e.result[k] = v
   264  }
   265  
   266  func (e *testFieldEncoder) EncodeFieldFloat32(k string, v float32) {
   267  	e.result[k] = v
   268  }
   269  
   270  func (e *testFieldEncoder) EncodeFieldDuration(k string, v time.Duration) {
   271  	e.result[k] = v
   272  }
   273  
   274  func (e *testFieldEncoder) EncodeFieldError(k string, v error) {
   275  	e.result[k] = v
   276  }
   277  
   278  func (e *testFieldEncoder) EncodeFieldTime(k string, v time.Time) {
   279  	e.result[k] = v
   280  }
   281  
   282  func (e *testFieldEncoder) EncodeFieldString(k string, v string) {
   283  	e.result[k] = v
   284  }
   285  
   286  func (e *testFieldEncoder) EncodeFieldStrings(k string, v []string) {
   287  	e.result[k] = v
   288  }
   289  
   290  func (e *testFieldEncoder) EncodeFieldBytes(k string, v []byte) {
   291  	e.result[k] = v
   292  }
   293  
   294  func (e *testFieldEncoder) EncodeFieldBools(k string, v []bool) {
   295  	e.result[k] = v
   296  }
   297  
   298  func (e *testFieldEncoder) EncodeFieldInts64(k string, v []int64) {
   299  	e.result[k] = v
   300  }
   301  
   302  func (e *testFieldEncoder) EncodeFieldInts32(k string, v []int32) {
   303  	e.result[k] = v
   304  }
   305  
   306  func (e *testFieldEncoder) EncodeFieldInts16(k string, v []int16) {
   307  	e.result[k] = v
   308  }
   309  
   310  func (e *testFieldEncoder) EncodeFieldInts8(k string, v []int8) {
   311  	e.result[k] = v
   312  }
   313  
   314  func (e *testFieldEncoder) EncodeFieldUints64(k string, v []uint64) {
   315  	e.result[k] = v
   316  }
   317  
   318  func (e *testFieldEncoder) EncodeFieldUints32(k string, v []uint32) {
   319  	e.result[k] = v
   320  }
   321  
   322  func (e *testFieldEncoder) EncodeFieldUints16(k string, v []uint16) {
   323  	e.result[k] = v
   324  }
   325  
   326  func (e *testFieldEncoder) EncodeFieldUints8(k string, v []uint8) {
   327  	e.result[k] = v
   328  }
   329  
   330  func (e *testFieldEncoder) EncodeFieldFloats64(k string, v []float64) {
   331  	e.result[k] = v
   332  }
   333  
   334  func (e *testFieldEncoder) EncodeFieldFloats32(k string, v []float32) {
   335  	e.result[k] = v
   336  }
   337  
   338  func (e *testFieldEncoder) EncodeFieldDurations(k string, v []time.Duration) {
   339  	e.result[k] = v
   340  }
   341  
   342  func (e *testFieldEncoder) EncodeFieldArray(k string, v ArrayEncoder) {
   343  	e.result[k] = v
   344  }
   345  
   346  func (e *testFieldEncoder) EncodeFieldObject(k string, v ObjectEncoder) {
   347  	e.result[k] = v
   348  }
   349  
   350  type testObjectEncoder struct{}
   351  
   352  func (o testObjectEncoder) EncodeLogfObject(e FieldEncoder) error {
   353  	e.EncodeFieldString("username", "username")
   354  	e.EncodeFieldInt64("code", 42)
   355  
   356  	return nil
   357  }
   358  
   359  type testArrayEncoder struct{}
   360  
   361  func (o testArrayEncoder) EncodeLogfArray(e TypeEncoder) error {
   362  	e.EncodeTypeInt64(42)
   363  
   364  	return nil
   365  }
   366  
   367  type testStringer struct {
   368  	result string
   369  }
   370  
   371  func (s testStringer) String() string {
   372  	return s.result
   373  }
   374  
   375  type verboseError struct {
   376  	short string
   377  	full  string
   378  }
   379  
   380  func (e *verboseError) Error() string {
   381  	return e.short
   382  }
   383  
   384  func (e *verboseError) Format(f fmt.State, c rune) {
   385  	f.Write([]byte(e.full))
   386  }