crossplane
96 строк · 2.2 Кб
1/*
2Copyright 2020 The Crossplane Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package version18
19import (20"errors"21"testing"22
23"github.com/google/go-cmp/cmp"24
25"github.com/crossplane/crossplane-runtime/pkg/test"26)
27
28func TestInRange(t *testing.T) {29type args struct {30version string31r string32}33type want struct {34is bool35err error36}37cases := map[string]struct {38reason string39args args
40want want
41}{42"ValidInRange": {43reason: "Should return true when a valid semantic version is in a valid range.",44args: args{45version: "v0.13.0",46r: ">0.12.0",47},48want: want{49is: true,50},51},52"ValidNotInRange": {53reason: "Should return false when a valid semantic version is not in a valid range.",54args: args{55version: "v0.13.0",56r: ">0.13.0",57},58want: want{59is: false,60},61},62"InvalidVersion": {63reason: "Should return error when version is invalid.",64args: args{65version: "v0a.13.0",66},67want: want{68err: errors.New("Invalid Semantic Version"),69},70},71"InvalidRange": {72reason: "Should return error when range is invalid.",73args: args{74version: "v0.13.0",75r: ">a2",76},77want: want{78err: errors.New("improper constraint: >a2"),79},80},81}82
83for name, tc := range cases {84t.Run(name, func(t *testing.T) {85version = tc.args.version86is, err := New().InConstraints(tc.args.r)87if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {88t.Errorf("\n%s\nInRange(...): -want err, +got err:\n%s", tc.reason, diff)89}90
91if diff := cmp.Diff(tc.want.is, is, test.EquateErrors()); diff != "" {92t.Errorf("\n%s\nInRange(...): -want, +got:\n%s", tc.reason, diff)93}94})95}96}
97