github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/internal/version/version_test.go (about)

     1  /* Copyright 2018 The Bazel Authors. All rights reserved.
     2  
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7     http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package version
    17  
    18  import "testing"
    19  
    20  func TestCompare(t *testing.T) {
    21  	for _, tc := range []struct {
    22  		x, y Version
    23  		want int
    24  	}{
    25  		{
    26  			x:    Version{1},
    27  			y:    Version{1},
    28  			want: 0,
    29  		}, {
    30  			x:    Version{1},
    31  			y:    Version{2},
    32  			want: -1,
    33  		}, {
    34  			x:    Version{2},
    35  			y:    Version{1},
    36  			want: 1,
    37  		}, {
    38  			x:    Version{1},
    39  			y:    Version{1, 1},
    40  			want: -1,
    41  		}, {
    42  			x:    Version{1, 1},
    43  			y:    Version{1},
    44  			want: 1,
    45  		},
    46  	} {
    47  		if got := tc.x.Compare(tc.y); got != tc.want {
    48  			t.Errorf("Compare(%s, %s): got %v, want %v", tc.x, tc.y, got, tc.want)
    49  		}
    50  	}
    51  }
    52  
    53  func TestParseVersion(t *testing.T) {
    54  	for _, tc := range []struct {
    55  		str     string
    56  		want    Version
    57  		wantErr bool
    58  	}{
    59  		{
    60  			str:     "",
    61  			wantErr: true,
    62  		}, {
    63  			str:  "1",
    64  			want: Version{1},
    65  		}, {
    66  			str:     "-1",
    67  			wantErr: true,
    68  		}, {
    69  			str:  "0.1.2",
    70  			want: Version{0, 1, 2},
    71  		}, {
    72  			str:  "0-suffix",
    73  			want: Version{0},
    74  		},
    75  	} {
    76  		if got, err := ParseVersion(tc.str); tc.wantErr && err == nil {
    77  			t.Errorf("ParseVersion(%q): got %s, want error", tc.str, got)
    78  		} else if !tc.wantErr && err != nil {
    79  			t.Errorf("ParseVersion(%q): got %v, want success", tc.str, err)
    80  		} else if got.Compare(tc.want) != 0 {
    81  			t.Errorf("ParseVersion(%q): got %s, want %s", tc.str, got, tc.want)
    82  		}
    83  	}
    84  }