diff --git a/attribute/step.go b/attribute/step.go index 9109510..5d1ea0f 100644 --- a/attribute/step.go +++ b/attribute/step.go @@ -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") diff --git a/attribute/step_test.go b/attribute/step_test.go index f380301..a4c4a19 100644 --- a/attribute/step_test.go +++ b/attribute/step_test.go @@ -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))