📜 feeds is a multi-protocol / multi-platform feed aggregation service that produces twtxt feeds for consumption by twtxt clients. https://feeds.twtxt.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

77 lines
2.4 KiB

package main
import (
"fmt"
"testing"
)
func TestProcessFeedContent(t *testing.T) {
for _, testCase := range []struct {
name string
title string
body string
max int
result string
}{
{
name: "when max rune length is greater than content length then return whole content",
title: "Some Test",
body: "Oh yeah, this is cool!",
max: 37,
result: "**Some Test**\u2028Oh yeah, this is cool!",
},
{
name: "when max rune length is exactly the content length then return whole content",
title: "Some Test",
body: "Oh yeah, this is cool!",
max: 36,
result: "**Some Test**\u2028Oh yeah, this is cool!",
},
{
name: "when max rune length is smaller than content length then return truncated content",
title: "Some Test",
body: "Oh yeah, this is cool!",
max: 35,
result: "**Some Test**\u2028Oh yeah, this is cool ...",
},
{
name: "when max rune length is smaller than content length and content is multi-byte Unicode then return truncated content with multi-byte characters intact",
title: "Äöüß", // all these umlauts are two bytes long encoded in UTF-8 and would be chopped in half when counting bytes instead of runes
body: "truncated anyways",
max: 5,
result: "**Äöü ...",
},
} {
t.Run(testCase.name, func(t *testing.T) {
actual := ProcessFeedContent(testCase.title, testCase.body, testCase.max)
if testCase.result != actual {
expectedRunes := []rune(testCase.result)
actualRunes := []rune(actual)
hint := func() string {
for i, expectedRune := range expectedRunes {
if i >= len(actualRunes) {
return fmt.Sprintf("actual too short, ends at rune index %d", i)
}
actualRune := actualRunes[i]
if expectedRune != actualRune {
return fmt.Sprintf("first difference at rune index %d:\n"+
"expected: '%s' (%U)\n"+
"actual: '%s' (%U)",
i, string(expectedRune), expectedRune,
string(actualRune), actualRune)
}
}
return fmt.Sprintf("actual too long, expected ends at rune index %d", len(expectedRunes))
}()
t.Logf("markdown not equal\n"+
"expected: '%s' (byte length %d, rune length %d)\n"+
"actual: '%s' (byte length %d, rune length %d)\n"+hint,
testCase.result, len(testCase.result), len(expectedRunes),
actual, len(actual), len(actualRunes))
t.Fail()
}
})
}
}