func TestComparators(t *testing.T) { ver1 := semver.Build(1, 2, 4) ver2 := semver.Build(3, 2, 1) expected := false if response := gt(ver1, ver2); response != expected { t.Errorf("gt(%q, %q): => %t, want %t", ver1, ver2, response, expected) } expected = true if response := gte(ver1, ver1); response != expected { t.Errorf("gte(%q, %q): => %t, want %t", ver1, ver2, response, expected) } expected = true if response := lt(ver1, ver2); response != expected { t.Errorf("lt(%q, %q): => %t, want %t", ver1, ver2, response, expected) } expected = true if response := lte(ver2, ver2); response != expected { t.Errorf("lte(%q, %q): => %t, want %t", ver1, ver2, response, expected) } expected = false if response := eq(ver1, ver2); response != expected { t.Errorf("eq(%q, %q): => %t, want %t", ver1, ver2, response, expected) } }
func TestSetPrerelease(t *testing.T) { ver := semver.Build(1, 2, 3) err := ver.SetPrerelease("12", "1", "0", "43") if err != nil { t.Errorf(fmt.Sprint(err)) } }
func TestGetters(t *testing.T) { expectedMajor := uint64(1) expectedMinor := uint64(2) expectedPatch := uint64(3) expectedPrerelease := "alpha.1" expectedMetadata := "35.45" ver := semver.Build(expectedMajor, expectedMinor, expectedPatch) ver.SetPrerelease("alpha", "1") ver.SetMetadata("35", "45") if result := ver.Major(); result != expectedMajor { t.Errorf("%q.Major() => %q, wanted %q", ver, result, expectedMajor) } if result := ver.Minor(); result != expectedMinor { t.Errorf("%q.Minor() => %q, wanted %q", ver, result, expectedMinor) } if result := ver.Patch(); result != expectedPatch { t.Errorf("%q.Patch() => %q, wanted %q", ver, result, expectedPatch) } if result := ver.Prerelease(); result != expectedPrerelease { t.Errorf("%q.Prerelease() => %q, wanted %q", ver, result, expectedPrerelease) } if result := ver.Metadata(); result != expectedMetadata { t.Errorf("%q.Metadata() => %q, wanted %q", ver, result, expectedMetadata) } }
func TestComparatorFunc(t *testing.T) { v := satisfactionMap{ semver.Build(1, 2, 0): gte, semver.Build(3, 3, 1): lte, semver.Build(3, 2, 1): eq, } ver := semver.Build(3, 2, 1) expected := true for v, f := range v { response := f(ver, v) if !response { t.Errorf("%v(%q,%q): => %t, want %t", getFunctionName(f), ver, v, response, expected) } } }
func BenchmarkRunner(b *testing.B) { const VERSION = "1.2.7 || >=1.2.9 <2.0.0" p, _ := Parse(VERSION) v := semver.Build(2, 0, 0) b.ResetTimer() for n := 0; n < b.N; n++ { p.Run(v) } }
func TestDecrementers(t *testing.T) { ver := semver.Build(1, 2, 3) ver.DecrementMajor() ver.DecrementMinor() ver.DecrementPatch() expected := "0.1.2" result := fmt.Sprint(ver) if result != expected { t.Errorf("Stringer() => %q, wanted %q", result, expected) } }
func TestStringer(t *testing.T) { ver := semver.Build(1, 2, 3) ver.SetPrerelease("alpha", "1") ver.SetMetadata("35", "45") expected := "1.2.3-alpha.1+35.45" result := fmt.Sprint(ver) if result != expected { t.Errorf("String() => %q, wanted %q", result, expected) } }
func TestSetters(t *testing.T) { ver := semver.Build(1, 1, 1) ver.SetMajor(2) ver.SetMinor(3) ver.SetPatch(4) if err := ver.SetPrerelease("beta", "1"); err != nil { t.Errorf(fmt.Sprint(err)) } if err := ver.SetMetadata("22", "43"); err != nil { t.Errorf(fmt.Sprint(err)) } expected := "2.3.4-beta.1+22.43" result := fmt.Sprint(ver) if result != expected { t.Errorf("Stringer() => %q, wanted %q", result, expected) } }
func xr2op(i item) node { version := i.val s := strings.Split(version, dot) for len(s) < 3 { s = append(s, "*") } major, err1 := strconv.ParseUint(s[0], 10, 0) minor, err2 := strconv.ParseUint(s[1], 10, 0) patch, _ := strconv.ParseUint(s[2], 10, 0) v1 := semver.Build(major, minor, patch) v2 := *v1 if err1 != nil { v2.SetMinor(0) v2.SetPatch(0) return nodeSet{ nodeComparison{gte, &v2}, } } if err2 != nil { v2.IncrementMajor() v2.SetMinor(0) v2.SetPatch(0) return nodeSet{ nodeComparison{gte, v1}, nodeComparison{lt, &v2}, } } v2.IncrementMinor() return nodeSet{ nodeComparison{gte, v1}, nodeComparison{lt, &v2}, } }
"1.1.001", "1.1.1-01", "1.1.1-001", "1.1.1-beta.01", "1.1.1-beta.001", "0.0.0-!", "0.0.0+!", // empty prerelease "0.0.0-.alpha", // empty build metadata "0.0.0-alpha+", "0.0.0-alpha+test.", } var comparisons = []comparison{ {semver.Build(1, 0, 0), semver.Build(1, 0, 0), 0}, {semver.Build(2, 0, 0), semver.Build(1, 0, 0), 1}, {semver.Build(0, 1, 0), semver.Build(0, 1, 0), 0}, {semver.Build(0, 2, 0), semver.Build(0, 1, 0), 1}, {semver.Build(0, 0, 1), semver.Build(0, 0, 1), 0}, {semver.Build(0, 0, 2), semver.Build(0, 0, 1), 1}, {semver.Build(1, 2, 3), semver.Build(1, 2, 3), 0}, {semver.Build(2, 2, 4), semver.Build(1, 2, 4), 1}, {semver.Build(1, 3, 3), semver.Build(1, 2, 3), 1}, {semver.Build(1, 2, 4), semver.Build(1, 2, 3), 1}, // Spec Examples #11 {semver.Build(1, 0, 0), semver.Build(2, 0, 0), -1}, {semver.Build(2, 0, 0), semver.Build(2, 1, 0), -1}, {semver.Build(2, 1, 0), semver.Build(2, 1, 1), -1},
package parser import ( "testing" "github.com/hansrodtang/semver" ) type test struct { expected bool version *semver.Version } var parsables = map[string][]test{ "1.2.7 || >=1.2.9 <2.0.0": { {true, semver.Build(1, 2, 7)}, {true, semver.Build(1, 2, 9)}, {true, semver.Build(1, 4, 6)}, {false, semver.Build(1, 2, 8)}, {false, semver.Build(2, 0, 0)}, }, "1.2 <1.2.9 || >2.0.0": { {false, semver.Build(1, 2, 10)}, {false, semver.Build(1, 5, 1)}, {true, semver.Build(1, 2, 8)}, {true, semver.Build(1, 2, 7)}, }, "* || >2.0.0": { {true, semver.Build(1, 0, 10)}, {true, semver.Build(100, 5, 1)}, {true, semver.Build(1, 100, 8)},