Custom helper for coords attribute
This commit is contained in:
19
attribute/coords.go
Normal file
19
attribute/coords.go
Normal file
@ -0,0 +1,19 @@
|
||||
package attribute
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitlab.codemonkeysoftware.net/b/hatmill"
|
||||
)
|
||||
|
||||
func Coords(values ...float32) hatmill.Attrib {
|
||||
s := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
s = append(s, strconv.FormatFloat(float64(value), 'G', -1, 32))
|
||||
}
|
||||
return hatmill.Attrib{
|
||||
Key: "coords",
|
||||
Value: strings.Join(s, ","),
|
||||
}
|
||||
}
|
40
attribute/coords_test.go
Normal file
40
attribute/coords_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
package attribute_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitlab.codemonkeysoftware.net/b/hatmill/attribute"
|
||||
)
|
||||
|
||||
func TestCoordsNumberFormat(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
name 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("zero", func(t *testing.T) {
|
||||
testAttribValue(t, attribute.Coords(), "")
|
||||
})
|
||||
t.Run("one", func(t *testing.T) {
|
||||
testAttribValue(t, attribute.Coords(5), "5")
|
||||
})
|
||||
t.Run("many", func(t *testing.T) {
|
||||
testAttribValue(t, attribute.Coords(5, 4, 3, 2, 1), "5,4,3,2,1")
|
||||
})
|
||||
}
|
@ -154,14 +154,6 @@ func Controls() hatmill.Attrib {
|
||||
}
|
||||
}
|
||||
|
||||
// Coords creates a "coords" attribute
|
||||
func Coords(value string) hatmill.Attrib {
|
||||
return hatmill.Attrib{
|
||||
Key: "coords",
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
// Crossorigin creates a "crossorigin" attribute
|
||||
func Crossorigin(value string) hatmill.Attrib {
|
||||
return hatmill.Attrib{
|
||||
|
Reference in New Issue
Block a user