Skip to content

Commit d819399

Browse files
committed
add ToString method, testcode
1 parent dec9180 commit d819399

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

vector2/vector2.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package vector2
22

3-
import "math"
3+
import (
4+
"fmt"
5+
"math"
6+
)
47

58
const Epsilon = 0.00001
69

@@ -137,3 +140,7 @@ func (v *Vector2) Reflect(other *Vector2) *Vector2 {
137140
func (v *Vector2) Equals(other *Vector2) bool {
138141
return v.X == other.X && v.Y == other.Y
139142
}
143+
144+
func (v *Vector2) ToString() string {
145+
return fmt.Sprintf("Vector2(%f, %f)", v.X, v.Y)
146+
}

vector2/vector2_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package vector2
22

33
import (
4+
"fmt"
45
"github.com/stretchr/testify/assert"
56
"testing"
67
)
@@ -189,6 +190,11 @@ func TestVector2_Equals(t *testing.T) {
189190
assert.False(t, a.Equals(b))
190191
}
191192

193+
func TestVector2_ToString(t *testing.T) {
194+
a := New(3, 6)
195+
assert.Equal(t, a.ToString(), fmt.Sprintf("Vector2(%f, %f)", a.X, a.Y))
196+
}
197+
192198
func TestDistance(t *testing.T) {
193199
a := New(1, 2)
194200
b := New(4, 3)

vector3/vector3.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package vector3
22

3-
import "math"
3+
import (
4+
"fmt"
5+
"math"
6+
)
47

58
const Epsilon = 0.00001
69

@@ -160,3 +163,7 @@ func (v *Vector3) Reflect(other *Vector3) *Vector3 {
160163
func (v *Vector3) Equals(other *Vector3) bool {
161164
return v.X == other.X && v.Y == other.Y && v.Z == other.Z
162165
}
166+
167+
func (v *Vector3) ToString() string {
168+
return fmt.Sprintf("Vector3(%f, %f, %f)", v.X, v.Y, v.Z)
169+
}

vector3/vector3_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package vector3
22

33
import (
4+
"fmt"
45
"github.com/stretchr/testify/assert"
56
"testing"
67
)
@@ -217,6 +218,11 @@ func TestVector3_Equals(t *testing.T) {
217218
assert.False(t, a.Equals(b))
218219
}
219220

221+
func TestVector2_ToString(t *testing.T) {
222+
a := New(3, 6, 2)
223+
assert.Equal(t, a.ToString(), fmt.Sprintf("Vector3(%f, %f, %f)", a.X, a.Y, a.Z))
224+
}
225+
220226
func TestDistance(t *testing.T) {
221227
a := New(1, 2, 3)
222228
b := New(4, 3, 1)

0 commit comments

Comments
 (0)