pongo2-addons/filters_test.go

98 lines
4.0 KiB
Go
Raw Normal View History

2014-07-26 17:28:14 +02:00
package pongo2addons
import (
"testing"
2014-07-28 16:54:04 +02:00
"time"
2014-07-26 17:28:14 +02:00
"github.com/flosch/pongo2"
2022-05-06 14:34:48 +02:00
. "gopkg.in/check.v1"
2014-07-26 17:28:14 +02:00
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) {
TestingT(t)
}
2017-02-13 11:53:32 +01:00
// A wrapprt of pongo2.RenderTemplateString
func getResult(s string, ctx pongo2.Context) string {
result, _ := pongo2.RenderTemplateString(s, ctx)
return result
}
2014-07-26 17:28:14 +02:00
type TestSuite1 struct{}
var _ = Suite(&TestSuite1{})
func (s *TestSuite1) TestFilters(c *C) {
// Markdown
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ \"**test**\"|markdown }}", nil), Equals, "<p><strong>test</strong></p>\n")
2014-07-26 17:28:14 +02:00
// Slugify
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ \"this is ä test!\"|slugify }}", nil), Equals, "this-is-a-test")
// Filesizeformat
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ 123456789|filesizeformat }}", nil), Equals, "118MiB")
// Timesince/timeuntil
baseDate := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
futureDate := baseDate.Add(24*7*4*time.Hour + 2*time.Hour)
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ future_date|timeuntil:base_date }}",
pongo2.Context{"base_date": baseDate, "future_date": futureDate}), Equals, "4 weeks from now")
baseDate = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
futureDate = baseDate.Add(2 * time.Hour)
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ future_date|timeuntil:base_date }}",
pongo2.Context{"base_date": baseDate, "future_date": futureDate}), Equals, "2 hours from now")
baseDate = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
futureDate = baseDate.Add(2 * time.Hour)
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ base_date|timesince:future_date }}",
pongo2.Context{"base_date": baseDate, "future_date": futureDate}), Equals, "2 hours ago")
// Natural time
baseDate = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
futureDate = baseDate.Add(4 * time.Second)
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ base_date|naturaltime:future_date }}",
pongo2.Context{"base_date": baseDate, "future_date": futureDate}), Equals, "4 seconds ago")
2014-07-28 16:54:04 +02:00
// Naturalday
today := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
yesterday := today.Add(-24 * time.Hour)
tomorrow := today.Add(24 * time.Hour)
todayPlus3 := today.Add(3 * 24 * time.Hour)
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ date|naturalday:today }}",
2014-07-28 16:54:04 +02:00
pongo2.Context{"date": today, "today": today}), Equals, "today")
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ date|naturalday:today }}",
2014-07-28 16:54:04 +02:00
pongo2.Context{"date": yesterday, "today": today}), Equals, "yesterday")
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ date|naturalday:today }}",
2014-07-28 16:54:04 +02:00
pongo2.Context{"date": tomorrow, "today": today}), Equals, "tomorrow")
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ date|naturalday:today }}",
pongo2.Context{"date": todayPlus3, "today": today}), Equals, "3 days from now")
// Intcomma
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ 123456789|intcomma }}", nil), Equals, "123,456,789")
2014-07-28 16:24:44 +02:00
// Ordinal
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ 1|ordinal }} {{ 2|ordinal }} {{ 3|ordinal }} {{ 18241|ordinal }}", nil),
2014-07-28 16:24:44 +02:00
Equals, "1st 2nd 3rd 18241st")
2014-07-30 22:58:02 +02:00
// Truncatesentences
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ text|truncatesentences:3|safe }}", pongo2.Context{
2014-07-31 18:16:55 +02:00
"text": `This is a first sentence with a 4.50 number. The second one is even more fun! Isn't it? Last sentence, okay.`}),
Equals, "This is a first sentence with a 4.50 number. The second one is even more fun! Isn't it?")
// Truncatesentences_html
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ text|truncatesentences_html:2 }}", pongo2.Context{
2014-07-31 18:16:55 +02:00
"text": `<div class="test"><ul><li>This is a first sentence with a 4.50 number.</li><li>The second one is even more fun! Isn't it?</li><li>Last sentence, okay.</li></ul></div>`}),
Equals, `<div class="test"><ul><li>This is a first sentence with a 4.50 number.</li><li>The second one is even more fun!</li></ul></div>`)
2017-02-13 11:53:32 +01:00
c.Assert(getResult("{{ text|truncatesentences_html:3 }}", pongo2.Context{
2014-07-31 18:16:55 +02:00
"text": `<div class="test"><ul><li>This is a first sentence with a 4.50 number.</li><li>The second one is even more fun! Isn't it?</li><li>Last sentence, okay.</li></ul></div>`}),
Equals, `<div class="test"><ul><li>This is a first sentence with a 4.50 number.</li><li>The second one is even more fun! Isn't it?</li></ul></div>`)
2017-09-02 01:51:18 +02:00
// Random
c.Assert(getResult("{{ array|random }}",
pongo2.Context{"array": []int{42}}),
Equals, "42")
2014-07-26 17:28:14 +02:00
}