diff --git a/attribute/coords.go b/attribute/coords.go index 6acfff1..5c425e8 100644 --- a/attribute/coords.go +++ b/attribute/coords.go @@ -1,19 +1,27 @@ package attribute import ( - "strconv" - "strings" + "bytes" "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)) +type CoordsList []float32 + +func (l CoordsList) String() string { + 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{ Key: "coords", - Value: String(strings.Join(s, ",")), + Value: CoordsList(values), } } diff --git a/attribute/coords_test.go b/attribute/coords_test.go index d38b7cf..80f96fb 100644 --- a/attribute/coords_test.go +++ b/attribute/coords_test.go @@ -6,35 +6,35 @@ import ( "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 TestCoordsList(t *testing.T) { + t.Run("String returns empty string for empty list", func(t *testing.T) { + expectEqualStrings(t, attribute.CoordsList(nil).String(), "") + }) -func TestCoordsCardinality(t *testing.T) { - t.Run("zero", func(t *testing.T) { - testAttribValue(t, attribute.Coords(), "") + t.Run("String uses valid number format", func(t *testing.T) { + for _, testcase := range []struct { + 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("many", func(t *testing.T) { - testAttribValue(t, attribute.Coords(5, 4, 3, 2, 1), "5,4,3,2,1") + + t.Run("String inserts delimiter in lists with length > 1", func(t *testing.T) { + actual := attribute.CoordsList{0, 0}.String() + expected := "0,0" + expectEqualStrings(t, actual, expected) }) }