Skip to content

Commit

Permalink
Adds MustParseRange
Browse files Browse the repository at this point in the history
  • Loading branch information
rosenhouse committed Jul 1, 2016
1 parent e64c75d commit 60ec348
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions range.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,12 @@ func parseComparator(s string) comparator {

return nil
}

// MustParseRange is like ParseRange but panics if the range cannot be parsed.
func MustParseRange(s string) Range {
r, err := ParseRange(s)
if err != nil {
panic(`semver: ParseRange(` + s + `): ` + err.Error())
}
return r
}
17 changes: 17 additions & 0 deletions range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,23 @@ func TestParseRange(t *testing.T) {
}
}

func TestMustParseRange(t *testing.T) {
testCase := ">1.2.2 <1.2.4 || >=2.0.0 <3.0.0"
r := MustParseRange(testCase)
if !r(MustParse("1.2.3")) {
t.Errorf("Unexpected range behavior on MustParseRange")
}
}

func TestMustParseRange_panic(t *testing.T) {
defer func() {
if recover() == nil {
t.Errorf("Should have panicked")
}
}()
_ = MustParseRange("invalid version")
}

func BenchmarkRangeParseSimple(b *testing.B) {
const VERSION = ">1.0.0"
b.ReportAllocs()
Expand Down

0 comments on commit 60ec348

Please sign in to comment.