github.com/blend/go-sdk@v1.20220411.3/semver/constraint_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package semver 9 10 import ( 11 "fmt" 12 "testing" 13 14 "github.com/blend/go-sdk/assert" 15 ) 16 17 func TestNewConstraint(t *testing.T) { 18 assert := assert.New(t) 19 20 cases := []struct { 21 input string 22 count int 23 err bool 24 }{ 25 {">= 1.2", 1, false}, 26 {"1.0", 1, false}, 27 {">= 1.x", 0, true}, 28 {">= 1.2, < 1.0", 2, false}, 29 30 // Out of bounds 31 {"11387778780781445675529500000000000000000", 0, true}, 32 } 33 34 for _, tc := range cases { 35 v, err := NewConstraint(tc.input) 36 assert.False(!tc.err && err != nil, fmt.Sprintf("error for input %s: %s", tc.input, err)) 37 assert.False(tc.err && err == nil, fmt.Sprintf("expected error for input: %s", tc.input)) 38 assert.Len(v, tc.count, fmt.Sprintf("input: %s\nexpected len: %d\nactual: %d", tc.input, tc.count, len(v))) 39 } 40 } 41 42 func TestConstraintCheck(t *testing.T) { 43 assert := assert.New(t) 44 45 cases := []struct { 46 constraint string 47 version string 48 check bool 49 }{ 50 {">= 1.0, < 1.2", "1.1.5", true}, 51 {"< 1.0, < 1.2", "1.1.5", false}, 52 {"= 1.0", "1.1.5", false}, 53 {"= 1.0", "1.0.0", true}, 54 {"1.0", "1.0.0", true}, 55 {"~> 1.0", "2.0", false}, 56 {"~> 1.0", "1.1", true}, 57 {"~> 1.0", "1.2.3", true}, 58 {"~> 1.0.0", "1.2.3", false}, 59 {"~> 1.0.0", "1.0.7", true}, 60 {"~> 1.0.0", "1.1.0", false}, 61 {"~> 1.0.7", "1.0.4", false}, 62 {"~> 1.0.7", "1.0.7", true}, 63 {"~> 1.0.7", "1.0.8", true}, 64 {"~> 1.0.7", "1.0.7.5", true}, 65 {"~> 1.0.7", "1.0.6.99", false}, 66 {"~> 1.0.7", "1.0.8.0", true}, 67 {"~> 1.0.9.5", "1.0.9.5", true}, 68 {"~> 1.0.9.5", "1.0.9.4", false}, 69 {"~> 1.0.9.5", "1.0.9.6", true}, 70 {"~> 1.0.9.5", "1.0.9.5.0", true}, 71 {"~> 1.0.9.5", "1.0.9.5.1", true}, 72 {"~> 2.0", "2.1.0-beta", false}, 73 {"~> 2.1.0-a", "2.2.0", false}, 74 {"~> 2.1.0-a", "2.1.0", false}, 75 {"~> 2.1.0-a", "2.1.0-beta", true}, 76 {"~> 2.1.0-a", "2.2.0-alpha", false}, 77 {"> 2.0", "2.1.0-beta", false}, 78 {">= 2.1.0-a", "2.1.0-beta", true}, 79 {">= 2.1.0-a", "2.1.1-beta", false}, 80 {">= 2.0.0", "2.1.0-beta", false}, 81 {">= 2.1.0-a", "2.1.1", true}, 82 {">= 2.1.0-a", "2.1.1-beta", false}, 83 {">= 2.1.0-a", "2.1.0", true}, 84 {"<= 2.1.0-a", "2.0.0", true}, 85 } 86 87 for _, tc := range cases { 88 c, err := NewConstraint(tc.constraint) 89 assert.Nil(err) 90 91 v, err := NewVersion(tc.version) 92 assert.Nil(err) 93 94 actual := c.Check(v) 95 expected := tc.check 96 assert.Equal(expected, actual) 97 } 98 } 99 100 func TestConstraintsString(t *testing.T) { 101 assert := assert.New(t) 102 103 cases := []struct { 104 constraint string 105 result string 106 }{ 107 {">= 1.0, < 1.2", ""}, 108 {"~> 1.0.7", ""}, 109 } 110 111 for _, tc := range cases { 112 c, err := NewConstraint(tc.constraint) 113 assert.Nil(err) 114 115 actual := c.String() 116 expected := tc.result 117 if expected == "" { 118 expected = tc.constraint 119 } 120 121 assert.Equal(expected, actual) 122 } 123 }