github.com/sl1pm4t/terraform@v0.6.4-0.20170725213156-870617d22df3/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  			true,
    34  		},
    35  		{
    36  			"~>1.1.0",
    37  			"1.1.2-beta1",
    38  			true,
    39  		},
    40  		{
    41  			"~>1.1.0",
    42  			"1.2.0",
    43  			false,
    44  		},
    45  	}
    46  
    47  	for _, test := range tests {
    48  		t.Run(fmt.Sprintf("%s has %s", test.ConstraintStr, test.VersionStr), func(t *testing.T) {
    49  			accepted, err := ConstraintStr(test.ConstraintStr).Parse()
    50  			if err != nil {
    51  				t.Fatalf("unwanted error parsing constraints string %q: %s", test.ConstraintStr, err)
    52  			}
    53  
    54  			version, err := VersionStr(test.VersionStr).Parse()
    55  			if err != nil {
    56  				t.Fatalf("unwanted error parsing version string %q: %s", test.VersionStr, err)
    57  			}
    58  
    59  			if got, want := accepted.Allows(version), test.ShouldHave; got != want {
    60  				t.Errorf("Has returned %#v; want %#v", got, want)
    61  			}
    62  		})
    63  	}
    64  }