[refactoring] version testing
This commit is contained in:
parent
21f3d88b0f
commit
009d638ee2
@ -42,17 +42,17 @@ func Compare(a string, b string) (int, error) {
|
||||
}
|
||||
|
||||
func parse(v string) (version, error) {
|
||||
parts := strings.Split(v, ".")
|
||||
if len(parts) != 2 {
|
||||
i := strings.Index(v, ".")
|
||||
if i == -1 {
|
||||
return version{}, errors.New("version must contain major and minor parts")
|
||||
}
|
||||
|
||||
major, err := strconv.Atoi(parts[0])
|
||||
major, err := strconv.Atoi(v[:i])
|
||||
if err != nil {
|
||||
return version{}, err
|
||||
}
|
||||
|
||||
minor, err := strconv.Atoi(parts[1])
|
||||
minor, err := strconv.Atoi(v[i+1:])
|
||||
if err != nil {
|
||||
return version{}, err
|
||||
}
|
||||
|
29
internal/version/version_test.go
Normal file
29
internal/version/version_test.go
Normal file
@ -0,0 +1,29 @@
|
||||
package version_test
|
||||
|
||||
import (
|
||||
"gotest.tools/assert"
|
||||
"testing"
|
||||
|
||||
"github.com/z7zmey/php-parser/internal/version"
|
||||
)
|
||||
|
||||
func TestSmaller(t *testing.T) {
|
||||
r, err := version.Compare("7.3", "5.6")
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, 1, r)
|
||||
}
|
||||
|
||||
func TestGreater(t *testing.T) {
|
||||
r, err := version.Compare("5.6", "7.3")
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, -1, r)
|
||||
}
|
||||
|
||||
func TestEqual(t *testing.T) {
|
||||
r, err := version.Compare("7.3", "7.3")
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.Equal(t, 0, r)
|
||||
}
|
Loading…
Reference in New Issue
Block a user