[refactoring] version testing

This commit is contained in:
Vadym Slizov 2020-05-18 22:37:13 +03:00
parent 21f3d88b0f
commit 009d638ee2
2 changed files with 33 additions and 4 deletions

View File

@ -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
}

View 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)
}