github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/slicefunc/func_test.go (about)

     1  // Copyright 2019 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package slicefunc
     6  
     7  import (
     8  	"context"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestFunc(t *testing.T) {
    14  	ctx := context.Background()
    15  	f, ok := Of(func(x, y int) int { return x + y })
    16  	if !ok {
    17  		t.Fatalf("unexpected bad func")
    18  	}
    19  	rv := f.Call(ctx, []reflect.Value{reflect.ValueOf(1), reflect.ValueOf(2)})
    20  	if got, want := len(rv), 1; got != want {
    21  		t.Fatalf("got %v, want %v", got, want)
    22  	}
    23  	if got, want := rv[0].Int(), int64(3); got != want {
    24  		t.Errorf("got %v, want %v", got, want)
    25  	}
    26  
    27  	f, ok = Of(func(pctx context.Context, x, y int) bool {
    28  		return x+y == 3 && pctx == ctx
    29  	})
    30  	if !ok {
    31  		t.Fatalf("unexpected bad func")
    32  	}
    33  	rv = f.Call(ctx, []reflect.Value{reflect.ValueOf(1), reflect.ValueOf(2)})
    34  	if got, want := len(rv), 1; got != want {
    35  		t.Fatalf("got %v, want %v", got, want)
    36  	}
    37  	if !rv[0].Bool() {
    38  		t.Error("!ok")
    39  	}
    40  }