add CommentConverterFunc to simplify injection of custom converters

This commit is contained in:
Umputun
2018-08-18 23:09:17 -05:00
parent 031a25ad92
commit a6277d764a
2 changed files with 14 additions and 0 deletions
+8
View File
@@ -19,6 +19,14 @@ type CommentConverter interface {
Convert(text string) string
}
// CommentConverterFunc functional struct implementing CommentConverter
type CommentConverterFunc func(text string) string
// Convert calls func for given text
func (f CommentConverterFunc) Convert(text string) string {
return f(text)
}
// NewCommentFormatter makes CommentFormatter
func NewCommentFormatter(converters ...CommentConverter) *CommentFormatter {
return &CommentFormatter{converters: converters}
+6
View File
@@ -33,6 +33,12 @@ func TestFormatter_FormatTextNoConvertor(t *testing.T) {
assert.Equal(t, "<p>12345</p>\n", f.FormatText("12345"))
}
func TestFormatter_FormatTextConvertorFunc(t *testing.T) {
fn := CommentConverterFunc(func(text string) string { return "zz!" + text })
f := NewCommentFormatter(fn)
assert.Equal(t, "zz!<p>12345</p>\n", f.FormatText("12345"))
}
func TestFormatter_FormatComment(t *testing.T) {
comment := Comment{
Text: "blah\n\nxyz",