Created Float type
This commit is contained in:
parent
276c0f9c0e
commit
261331daa4
@ -297,7 +297,7 @@ func Hidden() hatmill.Attrib {
|
||||
func High(value float32) hatmill.Attrib {
|
||||
return hatmill.Attrib{
|
||||
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 {
|
||||
return hatmill.Attrib{
|
||||
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 {
|
||||
return hatmill.Attrib{
|
||||
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 {
|
||||
return hatmill.Attrib{
|
||||
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 {
|
||||
return hatmill.Attrib{
|
||||
Key: "optimum",
|
||||
Value: String(strconv.FormatFloat(float64(value), 'G', -1, 32)),
|
||||
Value: String(Float(value).String()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,24 +43,3 @@ func TestInt(t *testing.T) {
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package attribute
|
||||
|
||||
import "strings"
|
||||
import "strconv"
|
||||
|
||||
type SpaceList []string
|
||||
|
||||
@ -13,3 +14,9 @@ type CommaList []string
|
||||
func (l CommaList) String() string {
|
||||
return strings.Join(l, ",")
|
||||
}
|
||||
|
||||
type Float float32
|
||||
|
||||
func (f Float) String() string {
|
||||
return strconv.FormatFloat(float64(f), 'G', -1, 32)
|
||||
}
|
||||
|
@ -34,3 +34,24 @@ func TestCommaList(t *testing.T) {
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -64,8 +64,7 @@ var attribTypes = map[string]AttribTypeInfo{
|
||||
Imports: []string{"strconv"},
|
||||
},
|
||||
"float": {
|
||||
Template: simpleTemplate("float32", "strconv.FormatFloat(float64(%s), 'G', -1, 32)"),
|
||||
Imports: []string{"strconv"},
|
||||
Template: simpleTemplate("float32", "Float(%s).String()"),
|
||||
},
|
||||
"space list": {
|
||||
Template: simpleTemplate("...string", "SpaceList(%s).String()"),
|
||||
|
Loading…
Reference in New Issue
Block a user