github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/controller/registry/resolver/cache/predicates_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  type OperatorPredicateTestFunc func(*Entry) bool
    10  
    11  func (opf OperatorPredicateTestFunc) Test(o *Entry) bool {
    12  	return opf(o)
    13  }
    14  
    15  func (opf OperatorPredicateTestFunc) String() string {
    16  	return ""
    17  }
    18  
    19  func TestCountingPredicate(t *testing.T) {
    20  	for _, tc := range []struct {
    21  		Name        string
    22  		TestResults []bool
    23  		Expected    int
    24  	}{
    25  		{
    26  			Name:        "no increment on failure",
    27  			TestResults: []bool{false},
    28  			Expected:    0,
    29  		},
    30  		{
    31  			Name:        "increment on success",
    32  			TestResults: []bool{true},
    33  			Expected:    1,
    34  		},
    35  		{
    36  			Name:        "multiple increments",
    37  			TestResults: []bool{true, true},
    38  			Expected:    2,
    39  		},
    40  		{
    41  			Name:        "no increment without test",
    42  			TestResults: nil,
    43  			Expected:    0,
    44  		},
    45  	} {
    46  		t.Run(tc.Name, func(t *testing.T) {
    47  			var (
    48  				n      int
    49  				result bool
    50  			)
    51  
    52  			p := CountingPredicate(OperatorPredicateTestFunc(func(*Entry) bool {
    53  				return result
    54  			}), &n)
    55  
    56  			for _, result = range tc.TestResults {
    57  				p.Test(nil)
    58  			}
    59  
    60  			assert.Equal(t, tc.Expected, n)
    61  		})
    62  	}
    63  }