github.com/GoogleCloudPlatform/testgrid@v0.0.174/util/gcs/sort_test.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes 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  
    17  package gcs
    18  
    19  import (
    20  	"net/url"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  // TestLeastRecentlyUpdated in util/gcs/fake/sort_test
    27  // TestTouch in util/gcs/fake/sort_test
    28  
    29  func TestSort(t *testing.T) {
    30  	cases := []struct {
    31  		name   string
    32  		builds []Build
    33  		want   []Build
    34  	}{
    35  		{
    36  			name: "basic",
    37  		},
    38  		{
    39  			name: "sorted",
    40  			builds: []Build{
    41  				{baseName: "c"},
    42  				{baseName: "b"},
    43  				{baseName: "a"},
    44  			},
    45  			want: []Build{
    46  				{baseName: "c"},
    47  				{baseName: "b"},
    48  				{baseName: "a"},
    49  			},
    50  		},
    51  		{
    52  			name: "stable", // already sorted elements do not move
    53  			builds: []Build{
    54  				{
    55  					Path:     Path{url: url.URL{Host: "foo"}},
    56  					baseName: "c",
    57  				},
    58  				{
    59  					Path:     Path{url: url.URL{Host: "bar"}},
    60  					baseName: "c",
    61  				},
    62  				{
    63  					Path:     Path{url: url.URL{Path: "other"}},
    64  					baseName: "c",
    65  				},
    66  				{baseName: "a"},
    67  				{baseName: "b"},
    68  			},
    69  			want: []Build{
    70  				{
    71  					Path:     Path{url: url.URL{Host: "foo"}},
    72  					baseName: "c",
    73  				},
    74  				{
    75  					Path:     Path{url: url.URL{Host: "bar"}},
    76  					baseName: "c",
    77  				},
    78  				{
    79  					Path:     Path{url: url.URL{Path: "other"}},
    80  					baseName: "c",
    81  				},
    82  				{baseName: "b"},
    83  				{baseName: "a"},
    84  			},
    85  		},
    86  		{
    87  			name: "resort",
    88  			builds: []Build{
    89  				{baseName: "b"},
    90  				{baseName: "c"},
    91  				{baseName: "a"},
    92  			},
    93  			want: []Build{
    94  				{baseName: "c"},
    95  				{baseName: "b"},
    96  				{baseName: "a"},
    97  			},
    98  		},
    99  		{
   100  			name: "numerics",
   101  			builds: []Build{
   102  				{baseName: "a1b"},
   103  				{baseName: "a10b"},
   104  				{baseName: "a2b"},
   105  				{baseName: "a3b"},
   106  			},
   107  			want: []Build{
   108  				{baseName: "a10b"},
   109  				{baseName: "a3b"},
   110  				{baseName: "a2b"},
   111  				{baseName: "a1b"},
   112  			},
   113  		},
   114  	}
   115  
   116  	for _, tc := range cases {
   117  		t.Run(tc.name, func(t *testing.T) {
   118  			Sort(tc.builds)
   119  			if diff := cmp.Diff(tc.want, tc.builds, cmp.AllowUnexported(Build{}, Path{})); diff != "" {
   120  				t.Errorf("Sort() got unexpected diff (-want +got):\n%s", diff)
   121  			}
   122  		})
   123  	}
   124  }