...

Source file src/github.com/sigstore/rekor/pkg/api/index_test.go

Documentation: github.com/sigstore/rekor/pkg/api

     1  //
     2  // Copyright 2021 The Sigstore Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package api
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  	"github.com/google/go-cmp/cmp/cmpopts"
    23  )
    24  
    25  func Test_Collection(t *testing.T) {
    26  
    27  	vals := []string{"foo", "bar", "baz", "baz", "baz"}
    28  
    29  	t.Run("Unique", func(t *testing.T) {
    30  		unq := NewUniq()
    31  		unq.Add(vals...)
    32  
    33  		if len(unq.Values()) != 3 {
    34  			t.Errorf("expected 3 unique values, got %d", len(unq.Values()))
    35  		}
    36  		expected := []string{"foo", "bar", "baz"}
    37  		if !testEqualNoOrder(t, expected, unq.Values()) {
    38  			t.Errorf("expected %v, got %v", expected, unq.Values())
    39  		}
    40  	})
    41  
    42  	t.Run("Collection", func(t *testing.T) {
    43  
    44  		uniq1 := []string{"foo", "bar", "baz"}
    45  		uniq2 := []string{"foo", "bar", "baz"}
    46  		uniq3 := []string{"corge", "grault", "garply", "foo"}
    47  
    48  		tests := []struct {
    49  			name     string
    50  			operator string
    51  			expected []string
    52  		}{
    53  			{name: "with 'and' operator",
    54  				operator: "and",
    55  				expected: []string{"foo"},
    56  			},
    57  			{name: "with 'or' operator",
    58  				operator: "or",
    59  				expected: []string{"foo", "bar", "baz", "corge", "grault", "garply"},
    60  			},
    61  		}
    62  
    63  		for _, test := range tests {
    64  			t.Run(test.name, func(t *testing.T) {
    65  				c := NewCollection(test.operator)
    66  				c.Add(uniq1)
    67  				c.Add(uniq2)
    68  				c.Add(uniq3)
    69  
    70  				if !testEqualNoOrder(t, test.expected, c.Values()) {
    71  					t.Errorf("expected %v, got %v", test.expected, c.Values())
    72  				}
    73  			})
    74  		}
    75  
    76  	})
    77  
    78  }
    79  
    80  // testEqualNoOrder compares two slices of strings without considering order.
    81  func testEqualNoOrder(t *testing.T, expected, actual []string) bool {
    82  	t.Helper()
    83  	less := func(a, b string) bool { return a < b }
    84  	return cmp.Diff(actual, expected, cmpopts.SortSlices(less)) == ""
    85  }
    86  

View as plain text