Skip to content

Instantly share code, notes, and snippets.

@crallen
Last active October 26, 2021 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crallen/607f0dc4c82a5b27d0f30e8da7edca9e to your computer and use it in GitHub Desktop.
Save crallen/607f0dc4c82a5b27d0f30e8da7edca9e to your computer and use it in GitHub Desktop.
Testing semver parsing
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)
}
}
}
@crallen
Copy link
Author

crallen commented Oct 26, 2021

Results in the following output:

❯ go test
--- FAIL: TestConstraint_SpecificPatchVersion (0.00s)
    --- FAIL: TestConstraint_SpecificPatchVersion/VersionWithExplicitPatchNonZero (0.00s)
        semver_test.go:65: version 1.0.9-SNAPSHOT does not satisfy constraint 1.0.0-SNAPSHOT
    --- FAIL: TestConstraint_SpecificPatchVersion/VersionWithFeatureTag (0.00s)
        semver_test.go:65: version 1.0-feature-SNAPSHOT does not satisfy constraint 1.0.0-SNAPSHOT
    --- FAIL: TestConstraint_SpecificPatchVersion/VersionWithPatchAndFeatureTag (0.00s)
        semver_test.go:65: version 1.0.0-feature-SNAPSHOT does not satisfy constraint 1.0.0-SNAPSHOT
FAIL
exit status 1
FAIL	github.com/crallen/semver-testing	0.101s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment