From 24f8fe6ba9334f07eb0ebebbef09f804905ea2e1 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sat, 31 Aug 2019 13:34:34 -0600 Subject: [PATCH] Removed ValueString method --- hatmill.go | 15 ++++++--------- hatmill_test.go | 35 ++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/hatmill.go b/hatmill.go index 9b4e396..6502b54 100644 --- a/hatmill.go +++ b/hatmill.go @@ -36,13 +36,6 @@ type Attrib struct { Value fmt.Stringer } -func (a Attrib) ValueString() string { - if a.Value == nil { - return "" - } - return html.EscapeString(a.Value.String()) -} - // WriteTo writes a to w as an HTML attribute in the form key="value", or // simply key if value is empty. Special characters in value are replaced with // HTML entities. It returns the number of bytes written and any @@ -52,8 +45,12 @@ func (a Attrib) WriteTo(w io.Writer) (n int64, err error) { if err != nil { return } - if a.ValueString() != "" { - err = writeStringsTo(w, &n, "='", a.ValueString(), "'") + var value string + if a.Value != nil { + value = html.EscapeString(a.Value.String()) + } + if value != "" { + err = writeStringsTo(w, &n, "='", value, "'") } return } diff --git a/hatmill_test.go b/hatmill_test.go index d394c1f..0931ebe 100644 --- a/hatmill_test.go +++ b/hatmill_test.go @@ -70,25 +70,27 @@ func stringNotEmpty(v interface{}) bool { var nonEmptyAlphaString = gen.AlphaString().SuchThat(stringNotEmpty) -type valueString string +type stringStringer string -func (s valueString) String() string { +func (s stringStringer) String() string { return string(s) } func attribGen(value gopter.Gen) gopter.Gen { - return gen.Struct(reflect.TypeOf(hatmill.Attrib{}), map[string]gopter.Gen{ - "Key": nonEmptyAlphaString, - "Value": value.Map(func(v string) fmt.Stringer { - return valueString(v) - }), - }) + return gen.Struct(reflect.TypeOf(hatmill.Attrib{}), + map[string]gopter.Gen{ + "Key": nonEmptyAlphaString, + "Value": value.Map(func(s string) fmt.Stringer { + return stringStringer(s) + }), + }, + ) } func TestAttrib(t *testing.T) { properties := gopter.NewProperties(nil) - properties.Property("WriteTo only writes key when value is empty", prop.ForAll( + properties.Property("WriteTo only writes key when value is nil", prop.ForAll( func(key string) bool { attrib := hatmill.Attrib{ Key: key, @@ -98,9 +100,20 @@ func TestAttrib(t *testing.T) { nonEmptyAlphaString, )) - properties.Property("writes key=value with ValueString() when value is not empty", prop.ForAll( + properties.Property("WriteTo only writes key when value.String() is empty", prop.ForAll( + func(key string) bool { + attrib := hatmill.Attrib{ + Key: key, + Value: stringStringer(""), + } + return checkWrite(attrib, key) + }, + nonEmptyAlphaString, + )) + + properties.Property("writes key=value with with escaped value.String() when value is not nil and value.String() is not empty", prop.ForAll( func(attrib hatmill.Attrib) bool { - expected := fmt.Sprintf("%s='%s'", attrib.Key, attrib.ValueString()) + expected := fmt.Sprintf("%s='%s'", attrib.Key, html.EscapeString(attrib.Value.String())) return checkWrite(attrib, expected) }, attribGen(dangerousASCII.SuchThat(stringNotEmpty)),