Created Float type

This commit is contained in:
Brandon Dyck 2019-08-31 09:56:56 -06:00
parent 276c0f9c0e
commit 261331daa4
5 changed files with 34 additions and 28 deletions

View File

@ -297,7 +297,7 @@ func Hidden() hatmill.Attrib {
func High(value float32) hatmill.Attrib { func High(value float32) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "high", Key: "high",
Value: String(strconv.FormatFloat(float64(value), 'G', -1, 32)), Value: String(Float(value).String()),
} }
} }
@ -391,7 +391,7 @@ func Loop() hatmill.Attrib {
func Low(value float32) hatmill.Attrib { func Low(value float32) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "low", Key: "low",
Value: String(strconv.FormatFloat(float64(value), 'G', -1, 32)), Value: String(Float(value).String()),
} }
} }
@ -399,7 +399,7 @@ func Low(value float32) hatmill.Attrib {
func Max(value float32) hatmill.Attrib { func Max(value float32) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "max", Key: "max",
Value: String(strconv.FormatFloat(float64(value), 'G', -1, 32)), Value: String(Float(value).String()),
} }
} }
@ -431,7 +431,7 @@ func Method(value string) hatmill.Attrib {
func Min(value float32) hatmill.Attrib { func Min(value float32) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "min", Key: "min",
Value: String(strconv.FormatFloat(float64(value), 'G', -1, 32)), Value: String(Float(value).String()),
} }
} }
@ -483,7 +483,7 @@ func Open() hatmill.Attrib {
func Optimum(value float32) hatmill.Attrib { func Optimum(value float32) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "optimum", Key: "optimum",
Value: String(strconv.FormatFloat(float64(value), 'G', -1, 32)), Value: String(Float(value).String()),
} }
} }

View File

@ -43,24 +43,3 @@ func TestInt(t *testing.T) {
testAttribValue(t, attribute.Tabindex(-45), "-45") testAttribValue(t, attribute.Tabindex(-45), "-45")
}) })
} }
func TestFloat(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.Min(testcase.f), testcase.value)
})
}
}

View File

@ -1,6 +1,7 @@
package attribute package attribute
import "strings" import "strings"
import "strconv"
type SpaceList []string type SpaceList []string
@ -13,3 +14,9 @@ type CommaList []string
func (l CommaList) String() string { func (l CommaList) String() string {
return strings.Join(l, ",") return strings.Join(l, ",")
} }
type Float float32
func (f Float) String() string {
return strconv.FormatFloat(float64(f), 'G', -1, 32)
}

View File

@ -34,3 +34,24 @@ func TestCommaList(t *testing.T) {
expectEqualStrings(t, actual, expected) expectEqualStrings(t, actual, expected)
}) })
} }
func TestFloat(t *testing.T) {
for _, testcase := range []struct {
name string
f attribute.Float
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, testcase.f.String(), testcase.s)
})
}
}

View File

@ -64,8 +64,7 @@ var attribTypes = map[string]AttribTypeInfo{
Imports: []string{"strconv"}, Imports: []string{"strconv"},
}, },
"float": { "float": {
Template: simpleTemplate("float32", "strconv.FormatFloat(float64(%s), 'G', -1, 32)"), Template: simpleTemplate("float32", "Float(%s).String()"),
Imports: []string{"strconv"},
}, },
"space list": { "space list": {
Template: simpleTemplate("...string", "SpaceList(%s).String()"), Template: simpleTemplate("...string", "SpaceList(%s).String()"),