github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/plugin/discovery/version_set_test.go (about) 1 package discovery 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 func TestVersionSet(t *testing.T) { 9 tests := []struct { 10 ConstraintStr string 11 VersionStr string 12 ShouldHave bool 13 }{ 14 // These test cases are not exhaustive since the underlying go-version 15 // library is well-tested. This is mainly here just to exercise our 16 // wrapper code, but also used as an opportunity to cover some basic 17 // but important cases such as the ~> constraint so that we'll be more 18 // likely to catch any accidental breaking behavior changes in the 19 // underlying library. 20 { 21 ">=1.0.0", 22 "1.0.0", 23 true, 24 }, 25 { 26 ">=1.0.0", 27 "0.0.0", 28 false, 29 }, 30 { 31 ">=1.0.0", 32 "1.1.0-beta1", 33 false, 34 }, 35 { 36 ">=1.0.0", 37 "1.1.0", 38 true, 39 }, 40 { 41 "~>1.1.0-a", 42 "1.1.0-beta1", 43 true, 44 }, 45 { 46 "~>1.1.0", 47 "1.1.2", 48 true, 49 }, 50 { 51 "~>1.1.0", 52 "1.2.0", 53 false, 54 }, 55 } 56 57 for _, test := range tests { 58 t.Run(fmt.Sprintf("%s has %s", test.ConstraintStr, test.VersionStr), func(t *testing.T) { 59 accepted, err := ConstraintStr(test.ConstraintStr).Parse() 60 if err != nil { 61 t.Fatalf("unwanted error parsing constraints string %q: %s", test.ConstraintStr, err) 62 } 63 64 version, err := VersionStr(test.VersionStr).Parse() 65 if err != nil { 66 t.Fatalf("unwanted error parsing version string %q: %s", test.VersionStr, err) 67 } 68 69 if got, want := accepted.Allows(version), test.ShouldHave; got != want { 70 t.Errorf("Has returned %#v; want %#v", got, want) 71 } 72 }) 73 } 74 }