Defined CoordsList type
This commit is contained in:
parent
5412e5e108
commit
edb347c9ea
@ -1,19 +1,27 @@
|
|||||||
package attribute
|
package attribute
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"bytes"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"gitlab.codemonkeysoftware.net/b/hatmill"
|
"gitlab.codemonkeysoftware.net/b/hatmill"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Coords(values ...float32) hatmill.Attrib {
|
type CoordsList []float32
|
||||||
s := make([]string, 0, len(values))
|
|
||||||
for _, value := range values {
|
func (l CoordsList) String() string {
|
||||||
s = append(s, strconv.FormatFloat(float64(value), 'G', -1, 32))
|
var buf bytes.Buffer
|
||||||
|
for i, f := range l {
|
||||||
|
if i > 0 {
|
||||||
|
buf.WriteRune(',')
|
||||||
|
}
|
||||||
|
buf.WriteString(Float(f).String())
|
||||||
}
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Coords(values ...float32) hatmill.Attrib {
|
||||||
return hatmill.Attrib{
|
return hatmill.Attrib{
|
||||||
Key: "coords",
|
Key: "coords",
|
||||||
Value: String(strings.Join(s, ",")),
|
Value: CoordsList(values),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,35 +6,35 @@ import (
|
|||||||
"gitlab.codemonkeysoftware.net/b/hatmill/attribute"
|
"gitlab.codemonkeysoftware.net/b/hatmill/attribute"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCoordsNumberFormat(t *testing.T) {
|
func TestCoordsList(t *testing.T) {
|
||||||
for _, testcase := range []struct {
|
t.Run("String returns empty string for empty list", func(t *testing.T) {
|
||||||
name string
|
expectEqualStrings(t, attribute.CoordsList(nil).String(), "")
|
||||||
f float32
|
})
|
||||||
value string
|
|
||||||
}{
|
|
||||||
{name: "zero", f: 0, value: "0"},
|
|
||||||
{name: "integral", f: 45, value: "45"},
|
|
||||||
{name: "positive fractional, small positive exponent", f: 12.55, value: "12.55"},
|
|
||||||
{name: "positive fractional, large positive exponent", f: 1.55E+12, value: "1.55E+12"},
|
|
||||||
{name: "positive fractional, large negative exponent", f: 1.55E-12, value: "1.55E-12"},
|
|
||||||
{name: "negative fractional, small positive exponent", f: -12.55, value: "-12.55"},
|
|
||||||
{name: "negative fractional, large positive exponent", f: -1.55E+12, value: "-1.55E+12"},
|
|
||||||
{name: "negative fractional, large negative exponent", f: -1.55E-12, value: "-1.55E-12"},
|
|
||||||
} {
|
|
||||||
t.Run(testcase.name, func(t *testing.T) {
|
|
||||||
testAttribValue(t, attribute.Coords(testcase.f), testcase.value)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCoordsCardinality(t *testing.T) {
|
t.Run("String uses valid number format", func(t *testing.T) {
|
||||||
t.Run("zero", func(t *testing.T) {
|
for _, testcase := range []struct {
|
||||||
testAttribValue(t, attribute.Coords(), "")
|
name string
|
||||||
|
f float32
|
||||||
|
s string
|
||||||
|
}{
|
||||||
|
{name: "zero", f: 0, s: "0"},
|
||||||
|
{name: "integral", f: 45, s: "45"},
|
||||||
|
{name: "positive fractional, small positive exponent", f: 12.55, s: "12.55"},
|
||||||
|
{name: "positive fractional, large positive exponent", f: 1.55E+12, s: "1.55E+12"},
|
||||||
|
{name: "positive fractional, large negative exponent", f: 1.55E-12, s: "1.55E-12"},
|
||||||
|
{name: "negative fractional, small positive exponent", f: -12.55, s: "-12.55"},
|
||||||
|
{name: "negative fractional, large positive exponent", f: -1.55E+12, s: "-1.55E+12"},
|
||||||
|
{name: "negative fractional, large negative exponent", f: -1.55E-12, s: "-1.55E-12"},
|
||||||
|
} {
|
||||||
|
t.Run(testcase.name, func(t *testing.T) {
|
||||||
|
expectEqualStrings(t, attribute.CoordsList{testcase.f}.String(), testcase.s)
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
t.Run("one", func(t *testing.T) {
|
|
||||||
testAttribValue(t, attribute.Coords(5), "5")
|
t.Run("String inserts delimiter in lists with length > 1", func(t *testing.T) {
|
||||||
})
|
actual := attribute.CoordsList{0, 0}.String()
|
||||||
t.Run("many", func(t *testing.T) {
|
expected := "0,0"
|
||||||
testAttribValue(t, attribute.Coords(5, 4, 3, 2, 1), "5,4,3,2,1")
|
expectEqualStrings(t, actual, expected)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user