Created StepValue type

This commit is contained in:
Brandon Dyck 2019-08-31 13:07:18 -06:00
parent b24ece723d
commit ee6d5468d1
2 changed files with 27 additions and 5 deletions

View File

@ -3,18 +3,25 @@ package attribute
import "strconv"
import "gitlab.codemonkeysoftware.net/b/hatmill"
type StepValue *float32
type StepValue float32
func StepAny() StepValue {
func (v *StepValue) String() string {
if v == nil {
return "any"
}
return strconv.FormatFloat(float64(*v), 'G', -1, 32)
}
func StepAny() *StepValue {
return nil
}
func StepFloat(value float32) StepValue {
return &value
func StepFloat(value float32) *StepValue {
return (*StepValue)(&(value))
}
// Step indicates the minimum allowed change to a number input.
func Step(value StepValue) hatmill.Attrib {
func Step(value *StepValue) hatmill.Attrib {
var attr = hatmill.Attrib{Key: "step"}
if value == nil {
attr.Value = String("any")

View File

@ -4,8 +4,23 @@ import (
"gitlab.codemonkeysoftware.net/b/hatmill/attribute"
"gitlab.codemonkeysoftware.net/b/hatmill/element"
"os"
"testing"
)
func TestStepValue(t *testing.T) {
t.Run(`String returns "any" for StepAny`, func(t *testing.T) {
actual := attribute.StepAny().String()
expected := "any"
expectEqualStrings(t, actual, expected)
})
t.Run("String returns valid float for StepFloat", func(t *testing.T) {
actual := attribute.StepFloat(5.5).String()
expected := "5.5"
expectEqualStrings(t, actual, expected)
})
}
func ExampleStep() {
s1 := attribute.Step(attribute.StepAny())
s2 := attribute.Step(attribute.StepFloat(0.5))