-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8353bd7
commit 2dcb953
Showing
6 changed files
with
97 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
mt "github.com/minetest-go/types" | ||
) | ||
|
||
// parses a comma separated string to a pos object | ||
func ParsePos(pos string) (*mt.Pos, error) { | ||
parts := strings.Split(pos, ",") | ||
if len(parts) != 3 { | ||
return nil, fmt.Errorf("invalid part-count: %d, should be 3", len(parts)) | ||
} | ||
|
||
x, err := strconv.ParseInt(strings.TrimSpace(parts[0]), 10, 32) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing x part: %v", err) | ||
} | ||
|
||
y, err := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 32) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing y part: %v", err) | ||
} | ||
|
||
z, err := strconv.ParseInt(strings.TrimSpace(parts[2]), 10, 32) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing z part: %v", err) | ||
} | ||
|
||
return mt.NewPos(int(x), int(y), int(z)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package types_test | ||
|
||
import ( | ||
"blockexchange/types" | ||
"testing" | ||
|
||
mt "github.com/minetest-go/types" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParsePos(t *testing.T) { | ||
pos, err := types.ParsePos("1,2,3") | ||
assert.NoError(t, err) | ||
assert.Equal(t, mt.NewPos(1, 2, 3), pos) | ||
|
||
pos, err = types.ParsePos("-1,-2,-3") | ||
assert.NoError(t, err) | ||
assert.Equal(t, mt.NewPos(-1, -2, -3), pos) | ||
|
||
pos, err = types.ParsePos("0,0,0") | ||
assert.NoError(t, err) | ||
assert.Equal(t, mt.NewPos(0, 0, 0), pos) | ||
|
||
pos, err = types.ParsePos("1 , 2 , 03") | ||
assert.NoError(t, err) | ||
assert.Equal(t, mt.NewPos(1, 2, 3), pos) | ||
|
||
pos, err = types.ParsePos("1,2,3,4") | ||
assert.Error(t, err) | ||
assert.Nil(t, pos) | ||
|
||
pos, err = types.ParsePos("") | ||
assert.Error(t, err) | ||
assert.Nil(t, pos) | ||
|
||
pos, err = types.ParsePos("1,2,") | ||
assert.Error(t, err) | ||
assert.Nil(t, pos) | ||
|
||
pos, err = types.ParsePos("garbage") | ||
assert.Error(t, err) | ||
assert.Nil(t, pos) | ||
|
||
pos, err = types.ParsePos("garbage,x,y") | ||
assert.Error(t, err) | ||
assert.Nil(t, pos) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters