github.com/camlistore/go4@v0.0.0-20200104003542-c7e774b10ea0/testing/functest/functest_test.go (about)

     1  package functest
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  // trec is a testing.TB which logs Errorf calls to buf
    10  type trec struct {
    11  	testing.TB // crash on unimplemented methods
    12  	buf        bytes.Buffer
    13  }
    14  
    15  func (t *trec) Errorf(format string, args ...interface{}) {
    16  	t.buf.WriteString("ERR: ")
    17  	fmt.Fprintf(&t.buf, format, args...)
    18  	t.buf.WriteByte('\n')
    19  }
    20  
    21  func (t *trec) Logf(format string, args ...interface{}) {
    22  	t.buf.WriteString("LOG: ")
    23  	fmt.Fprintf(&t.buf, format, args...)
    24  	t.buf.WriteByte('\n')
    25  }
    26  
    27  func (t *trec) String() string { return t.buf.String() }
    28  
    29  func add(a, b int) int { return a + b }
    30  
    31  func TestBasic(t *testing.T) {
    32  	f := New(add)
    33  	trec := new(trec)
    34  	f.Test(trec,
    35  		f.In(1, 2).Want(3),
    36  		f.In(5, 6).Want(100),
    37  		f.Case("also wrong").In(5, 6).Want(101),
    38  	)
    39  	want := `ERR: add(5, 6) = 11; want 100
    40  ERR: also wrong: add(5, 6) = 11; want 101
    41  `
    42  	if got := trec.String(); got != want {
    43  		t.Errorf("Output mismatch.\nGot:\n%v\nWant:\n%v\n", got, want)
    44  	}
    45  }
    46  
    47  func TestBasic_Strings(t *testing.T) {
    48  	concat := func(a, b string) string { return a + b }
    49  	f := New(concat)
    50  	f.Name = "concat"
    51  	trec := new(trec)
    52  	f.Test(trec,
    53  		f.In("a", "b").Want("ab"),
    54  		f.In("a", "b\x00").Want("ab"),
    55  	)
    56  	want := `ERR: concat("a", "b\x00") = "ab\x00"; want "ab"
    57  `
    58  	if got := trec.String(); got != want {
    59  		t.Errorf("Output mismatch.\nGot:\n%v\nWant:\n%v\n", got, want)
    60  	}
    61  }
    62  
    63  func TestVariadic(t *testing.T) {
    64  	sumVar := func(vals ...int) (sum int) {
    65  		for _, v := range vals {
    66  			sum += v
    67  		}
    68  		return
    69  	}
    70  
    71  	f := New(sumVar)
    72  	f.Name = "sumVar"
    73  	trec := new(trec)
    74  	f.Test(trec,
    75  		f.In().Want(0),
    76  		f.In().Want(100),
    77  		f.In(1).Want(1),
    78  		f.In(1).Want(100),
    79  		f.In(1, 2).Want(3),
    80  		f.In(1, 2, 3).Want(6),
    81  		f.In(1, 2, 3).Want(100),
    82  	)
    83  	want := `ERR: sumVar() = 0; want 100
    84  ERR: sumVar(1) = 1; want 100
    85  ERR: sumVar(1, 2, 3) = 6; want 100
    86  `
    87  	if got := trec.String(); got != want {
    88  		t.Errorf("Output mismatch.\nGot:\n%v\nWant:\n%v\n", got, want)
    89  	}
    90  }
    91  
    92  func condPanic(doPanic bool, panicValue interface{}) {
    93  	if doPanic {
    94  		panic(panicValue)
    95  	}
    96  }
    97  
    98  func TestPanic(t *testing.T) {
    99  	f := New(condPanic)
   100  	f.Name = "condPanic"
   101  	trec := new(trec)
   102  	f.Test(trec,
   103  		f.In(false, nil),
   104  		f.In(true, "boom").Check(func(res Result) error {
   105  			trec.Logf("Got res: %+v", res)
   106  			if res.Panic != "boom" {
   107  				return fmt.Errorf("panic = %v; want boom", res.Panic)
   108  			}
   109  			return nil
   110  		}),
   111  		f.Case("panic with nil").In(true, nil),
   112  	)
   113  	want := `LOG: Got res: {Result:[] Panic:boom Panicked:true}
   114  ERR: panic with nil: condPanic(true, <nil>): panicked with <nil>
   115  `
   116  	if got := trec.String(); got != want {
   117  		t.Errorf("Output mismatch.\nGot:\n%v\nWant:\n%v\n", got, want)
   118  	}
   119  }
   120  
   121  func TestName_AutoFunc(t *testing.T) {
   122  	testName(t, New(add), "add")
   123  }
   124  
   125  type SomeType struct{}
   126  
   127  func (t *SomeType) SomeMethod(int) int { return 123 }
   128  
   129  func TestName_AutoMethod(t *testing.T) {
   130  	testName(t, New((*SomeType).SomeMethod), "SomeType.SomeMethod")
   131  }
   132  
   133  func testName(t *testing.T, f *Func, want string) {
   134  	if f.Name != want {
   135  		t.Errorf("name = %q; want %q", f.Name, want)
   136  	}
   137  }