Removed ValueString method

This commit is contained in:
Brandon Dyck 2019-08-31 13:34:34 -06:00
parent e3cedf5de7
commit 24f8fe6ba9
2 changed files with 30 additions and 20 deletions

View File

@ -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
}

View File

@ -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{
return gen.Struct(reflect.TypeOf(hatmill.Attrib{}),
map[string]gopter.Gen{
"Key": nonEmptyAlphaString,
"Value": value.Map(func(v string) fmt.Stringer {
return valueString(v)
"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)),