Last active
October 26, 2021 15:26
-
-
Save crallen/607f0dc4c82a5b27d0f30e8da7edca9e to your computer and use it in GitHub Desktop.
Testing semver parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
"github.com/Masterminds/semver/v3" | |
) | |
type testCase struct { | |
Name string | |
Version string | |
} | |
var testCases = []testCase{ | |
{ | |
Name: "VersionWithoutExplicitPatch", | |
Version: "1.0-SNAPSHOT", | |
}, | |
{ | |
Name: "VersionWithExplicitPatchZero", | |
Version: "1.0.0-SNAPSHOT", | |
}, | |
{ | |
Name: "VersionWithExplicitPatchNonZero", | |
Version: "1.0.9-SNAPSHOT", | |
}, | |
{ | |
Name: "VersionWithFeatureTag", | |
Version: "1.0-feature-SNAPSHOT", | |
}, | |
{ | |
Name: "VersionWithPatchAndFeatureTag", | |
Version: "1.0.0-feature-SNAPSHOT", | |
}, | |
} | |
func TestConstraint_NoPatchVersion(t *testing.T) { | |
c, err := semver.NewConstraint("1.0-SNAPSHOT") | |
if err != nil { | |
t.Fatalf("failed to parse constraint: %v", err) | |
} | |
for _, tc := range testCases { | |
t.Run(tc.Name, testConstraint(c, &tc)) | |
} | |
} | |
func TestConstraint_SpecificPatchVersion(t *testing.T) { | |
c, err := semver.NewConstraint("1.0.0-SNAPSHOT") | |
if err != nil { | |
t.Fatalf("failed to parse constraint: %v", err) | |
} | |
for _, tc := range testCases { | |
t.Run(tc.Name, testConstraint(c, &tc)) | |
} | |
} | |
func testConstraint(c *semver.Constraints, tc *testCase) func(*testing.T) { | |
return func(t *testing.T) { | |
v, err := semver.NewVersion(tc.Version) | |
if err != nil { | |
t.Errorf("failed to parse version: %v", err) | |
return | |
} | |
if !c.Check(v) { | |
t.Errorf("version %v does not satisfy constraint %v", tc.Version, c) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results in the following output: