github.com/leanovate/gopter@v0.2.9/prop/check_condition_func_test.go (about)

     1  package prop
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestCheckCondition(t *testing.T) {
     9  	call, err := checkConditionFunc(0, 0)
    10  	if err == nil || call != nil {
    11  		t.Error("Should not work for integers")
    12  	}
    13  
    14  	call, err = checkConditionFunc(func(a, b int) bool {
    15  		return false
    16  	}, 1)
    17  	if err == nil || call != nil {
    18  		t.Error("Should not work with wrong number of arguments")
    19  	}
    20  
    21  	call, err = checkConditionFunc(func(a, b int) {
    22  	}, 2)
    23  	if err == nil || call != nil {
    24  		t.Error("Should not work witout return")
    25  	}
    26  
    27  	call, err = checkConditionFunc(func(a, b int) (int, int, int) {
    28  		return 0, 0, 0
    29  	}, 2)
    30  	if err == nil || call != nil {
    31  		t.Error("Should not work with too many return")
    32  	}
    33  
    34  	call, err = checkConditionFunc(func(a, b int) (int, int) {
    35  		return 0, 0
    36  	}, 2)
    37  	if err == nil || call != nil {
    38  		t.Error("Should not work if second return is not an error")
    39  	}
    40  
    41  	var calledA, calledB int
    42  	call, err = checkConditionFunc(func(a, b int) bool {
    43  		calledA = a
    44  		calledB = b
    45  		return true
    46  	}, 2)
    47  	if err != nil || call == nil {
    48  		t.Error("Should work")
    49  	}
    50  	result := call([]reflect.Value{
    51  		reflect.ValueOf(123),
    52  		reflect.ValueOf(456),
    53  	})
    54  	if calledA != 123 || calledB != 456 {
    55  		t.Errorf("Invalid parameters: %d, %d", calledA, calledB)
    56  	}
    57  	if !result.Success() {
    58  		t.Errorf("Invalid result: %#v", result)
    59  	}
    60  }