Created Int type

This commit is contained in:
Brandon Dyck 2019-08-31 10:20:58 -06:00
parent 261331daa4
commit 9f7f25a89e
5 changed files with 30 additions and 25 deletions

View File

@ -110,7 +110,7 @@ func Class(value ...string) hatmill.Attrib {
func Cols(value int) hatmill.Attrib { func Cols(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "cols", Key: "cols",
Value: String(strconv.FormatInt(int64(value), 10)), Value: String(Int(value).String()),
} }
} }
@ -118,7 +118,7 @@ func Cols(value int) hatmill.Attrib {
func Colspan(value int) hatmill.Attrib { func Colspan(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "colspan", Key: "colspan",
Value: String(strconv.FormatInt(int64(value), 10)), Value: String(Int(value).String()),
} }
} }
@ -282,7 +282,7 @@ func Headers(value ...string) hatmill.Attrib {
func Height(value int) hatmill.Attrib { func Height(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "height", Key: "height",
Value: String(strconv.FormatInt(int64(value), 10)), Value: String(Int(value).String()),
} }
} }
@ -407,7 +407,7 @@ func Max(value float32) hatmill.Attrib {
func Maxlength(value int) hatmill.Attrib { func Maxlength(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "maxlength", Key: "maxlength",
Value: String(strconv.FormatInt(int64(value), 10)), Value: String(Int(value).String()),
} }
} }
@ -439,7 +439,7 @@ func Min(value float32) hatmill.Attrib {
func Minlength(value int) hatmill.Attrib { func Minlength(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "minlength", Key: "minlength",
Value: String(strconv.FormatInt(int64(value), 10)), Value: String(Int(value).String()),
} }
} }
@ -568,7 +568,7 @@ func Reversed() hatmill.Attrib {
func Rows(value int) hatmill.Attrib { func Rows(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "rows", Key: "rows",
Value: String(strconv.FormatInt(int64(value), 10)), Value: String(Int(value).String()),
} }
} }
@ -576,7 +576,7 @@ func Rows(value int) hatmill.Attrib {
func Rowspan(value int) hatmill.Attrib { func Rowspan(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "rowspan", Key: "rowspan",
Value: String(strconv.FormatInt(int64(value), 10)), Value: String(Int(value).String()),
} }
} }
@ -615,7 +615,7 @@ func Shape(value string) hatmill.Attrib {
func Size(value int) hatmill.Attrib { func Size(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "size", Key: "size",
Value: String(strconv.FormatInt(int64(value), 10)), Value: String(Int(value).String()),
} }
} }
@ -631,7 +631,7 @@ func Sizes(value string) hatmill.Attrib {
func Span(value int) hatmill.Attrib { func Span(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "span", Key: "span",
Value: String(strconv.FormatInt(int64(value), 10)), Value: String(Int(value).String()),
} }
} }
@ -695,7 +695,7 @@ func Style(value string) hatmill.Attrib {
func Tabindex(value int) hatmill.Attrib { func Tabindex(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "tabindex", Key: "tabindex",
Value: String(strconv.FormatInt(int64(value), 10)), Value: String(Int(value).String()),
} }
} }
@ -743,7 +743,7 @@ func Value(value string) hatmill.Attrib {
func Width(value int) hatmill.Attrib { func Width(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "width", Key: "width",
Value: String(strconv.FormatInt(int64(value), 10)), Value: String(Int(value).String()),
} }
} }

View File

@ -31,15 +31,3 @@ func TestExplicitBool(t *testing.T) {
testAttribValue(t, attribute.Draggable(false), "false") testAttribValue(t, attribute.Draggable(false), "false")
}) })
} }
func TestInt(t *testing.T) {
t.Run("zero", func(t *testing.T) {
testAttribValue(t, attribute.Height(0), "0")
})
t.Run("positive", func(t *testing.T) {
testAttribValue(t, attribute.Height(45), "45")
})
t.Run("negative", func(t *testing.T) {
testAttribValue(t, attribute.Tabindex(-45), "-45")
})
}

View File

@ -20,3 +20,9 @@ type Float float32
func (f Float) String() string { func (f Float) String() string {
return strconv.FormatFloat(float64(f), 'G', -1, 32) return strconv.FormatFloat(float64(f), 'G', -1, 32)
} }
type Int int
func (n Int) String() string {
return strconv.FormatInt(int64(n), 10)
}

View File

@ -55,3 +55,15 @@ func TestFloat(t *testing.T) {
}) })
} }
} }
func TestInt(t *testing.T) {
t.Run("zero", func(t *testing.T) {
expectEqualStrings(t, attribute.Int(0).String(), "0")
})
t.Run("positive", func(t *testing.T) {
expectEqualStrings(t, attribute.Int(45).String(), "45")
})
t.Run("negative", func(t *testing.T) {
expectEqualStrings(t, attribute.Int(-45).String(), "-45")
})
}

View File

@ -60,8 +60,7 @@ var attribTypes = map[string]AttribTypeInfo{
Imports: []string{"strconv"}, Imports: []string{"strconv"},
}, },
"int": { "int": {
Template: simpleTemplate("int", "strconv.FormatInt(int64(%s), 10)"), Template: simpleTemplate("int", "Int(%s).String()"),
Imports: []string{"strconv"},
}, },
"float": { "float": {
Template: simpleTemplate("float32", "Float(%s).String()"), Template: simpleTemplate("float32", "Float(%s).String()"),