github.com/karrick/gorill@v1.10.3/ensure_test.go (about)

     1  package gorill
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"sort"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func ensureBuffer(tb testing.TB, buf []byte, n int, want string) {
    12  	tb.Helper()
    13  	if got, want := n, len(want); got != want {
    14  		tb.Fatalf("GOT: %v; WANT: %v", got, want)
    15  	}
    16  	if got, want := string(buf[:n]), want; got != want {
    17  		tb.Errorf("GOT: %v; WANT: %v", got, want)
    18  	}
    19  }
    20  
    21  func ensureError(tb testing.TB, err error, contains ...string) {
    22  	tb.Helper()
    23  	if len(contains) == 0 || (len(contains) == 1 && contains[0] == "") {
    24  		if err != nil {
    25  			tb.Fatalf("GOT: %v; WANT: %v", err, contains)
    26  		}
    27  	} else if err == nil {
    28  		tb.Errorf("GOT: %v; WANT: %v", err, contains)
    29  	} else {
    30  		for _, stub := range contains {
    31  			if stub != "" && !strings.Contains(err.Error(), stub) {
    32  				tb.Errorf("GOT: %v; WANT: %q", err, stub)
    33  			}
    34  		}
    35  	}
    36  }
    37  
    38  func ensurePanic(tb testing.TB, want string, callback func()) {
    39  	tb.Helper()
    40  	defer func() {
    41  		r := recover()
    42  		if r == nil {
    43  			tb.Fatalf("GOT: %v; WANT: %v", r, want)
    44  			return
    45  		}
    46  		if got := fmt.Sprintf("%v", r); got != want {
    47  			tb.Fatalf("GOT: %v; WANT: %v", got, want)
    48  		}
    49  	}()
    50  	callback()
    51  }
    52  
    53  // ensureNoPanic prettifies the output so one knows which test case caused a
    54  // panic.
    55  func ensureNoPanic(tb testing.TB, label string, callback func()) {
    56  	tb.Helper()
    57  	defer func() {
    58  		if r := recover(); r != nil {
    59  			tb.Fatalf("TEST: %s: GOT: %v", label, r)
    60  		}
    61  	}()
    62  	callback()
    63  }
    64  
    65  func ensureStringSlicesMatch(tb testing.TB, actual, expected []string) {
    66  	tb.Helper()
    67  
    68  	results := make(map[string]int)
    69  
    70  	for _, s := range actual {
    71  		results[s] = -1
    72  	}
    73  	for _, s := range expected {
    74  		results[s] += 1
    75  	}
    76  
    77  	keys := make([]string, 0, len(results))
    78  	for k := range results {
    79  		keys = append(keys, k)
    80  	}
    81  	sort.Strings(keys)
    82  
    83  	for _, s := range keys {
    84  		v, ok := results[s]
    85  		if !ok {
    86  			panic(fmt.Errorf("cannot find key: %s", s)) // panic because this function is broken
    87  		}
    88  		switch v {
    89  		case -1:
    90  			tb.Errorf("GOT: %q (extra)", s)
    91  		case 0:
    92  			// both slices have this key
    93  		case 1:
    94  			tb.Errorf("WANT: %q (missing)", s)
    95  		default:
    96  			panic(fmt.Errorf("key has invalid value: %s: %d", s, v)) // panic because this function is broken
    97  		}
    98  	}
    99  }
   100  
   101  func testErrorType(tb testing.TB, got, want error) {
   102  	tb.Helper()
   103  	if typeGot, typeWant := reflect.TypeOf(got), reflect.TypeOf(want); typeGot != typeWant {
   104  		tb.Errorf("GOT: %T; WANT: %T", got, want)
   105  	}
   106  }