Fix for 6d9b0dd of pongo2
This commit is contained in:
		
							
								
								
									
										17
									
								
								filters.go
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								filters.go
									
									
									
									
									
								
							| @@ -2,6 +2,7 @@ package pongo2addons | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"regexp" | ||||
| 	"strings" | ||||
| @@ -221,8 +222,8 @@ func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Va | ||||
| 	basetime, is_time := in.Interface().(time.Time) | ||||
| 	if !is_time { | ||||
| 		return nil, &pongo2.Error{ | ||||
| 			Sender: "filter:timeuntil/timesince", | ||||
| 			ErrorMsg: "time-value is not a time.Time-instance.", | ||||
| 			Sender:    "filter:timeuntil/timesince", | ||||
| 			OrigError: errors.New("time-value is not a time.Time-instance."), | ||||
| 		} | ||||
| 	} | ||||
| 	var paramtime time.Time | ||||
| @@ -230,8 +231,8 @@ func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Va | ||||
| 		paramtime, is_time = param.Interface().(time.Time) | ||||
| 		if !is_time { | ||||
| 			return nil, &pongo2.Error{ | ||||
| 				Sender: "filter:timeuntil/timesince", | ||||
| 				ErrorMsg: "time-parameter is not a time.Time-instance.", | ||||
| 				Sender:    "filter:timeuntil/timesince", | ||||
| 				OrigError: errors.New("time-parameter is not a time.Time-instance."), | ||||
| 			} | ||||
| 		} | ||||
| 	} else { | ||||
| @@ -253,8 +254,8 @@ func filterNaturalday(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *po | ||||
| 	basetime, is_time := in.Interface().(time.Time) | ||||
| 	if !is_time { | ||||
| 		return nil, &pongo2.Error{ | ||||
| 			Sender: "filter:naturalday", | ||||
| 			ErrorMsg: "naturalday-value is not a time.Time-instance.", | ||||
| 			Sender:    "filter:naturalday", | ||||
| 			OrigError: errors.New("naturalday-value is not a time.Time-instance."), | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| @@ -263,8 +264,8 @@ func filterNaturalday(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *po | ||||
| 		reference_time, is_time = param.Interface().(time.Time) | ||||
| 		if !is_time { | ||||
| 			return nil, &pongo2.Error{ | ||||
| 				Sender: "filter:naturalday", | ||||
| 				ErrorMsg: "naturalday-parameter is not a time.Time-instance.", | ||||
| 				Sender:    "filter:naturalday", | ||||
| 				OrigError: errors.New("naturalday-parameter is not a time.Time-instance."), | ||||
| 			} | ||||
| 		} | ||||
| 	} else { | ||||
|   | ||||
| @@ -14,40 +14,46 @@ func Test(t *testing.T) { | ||||
| 	TestingT(t) | ||||
| } | ||||
|  | ||||
| // A wrapprt of pongo2.RenderTemplateString | ||||
| func getResult(s string, ctx pongo2.Context) string { | ||||
| 	result, _ := pongo2.RenderTemplateString(s, ctx) | ||||
| 	return result | ||||
| } | ||||
|  | ||||
| type TestSuite1 struct{} | ||||
|  | ||||
| var _ = Suite(&TestSuite1{}) | ||||
|  | ||||
| func (s *TestSuite1) TestFilters(c *C) { | ||||
| 	// Markdown | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ \"**test**\"|markdown }}", nil), Equals, "<p><strong>test</strong></p>\n") | ||||
| 	c.Assert(getResult("{{ \"**test**\"|markdown }}", nil), Equals, "<p><strong>test</strong></p>\n") | ||||
|  | ||||
| 	// Slugify | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ \"this is ä test!\"|slugify }}", nil), Equals, "this-is-a-test") | ||||
| 	c.Assert(getResult("{{ \"this is ä test!\"|slugify }}", nil), Equals, "this-is-a-test") | ||||
|  | ||||
| 	// Filesizeformat | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ 123456789|filesizeformat }}", nil), Equals, "118MiB") | ||||
| 	c.Assert(getResult("{{ 123456789|filesizeformat }}", nil), Equals, "118MiB") | ||||
|  | ||||
| 	// Timesince/timeuntil | ||||
| 	base_date := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) | ||||
| 	future_date := base_date.Add(24*7*4*time.Hour + 2*time.Hour) | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ future_date|timeuntil:base_date }}", | ||||
| 	c.Assert(getResult("{{ future_date|timeuntil:base_date }}", | ||||
| 		pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "4 weeks from now") | ||||
|  | ||||
| 	base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) | ||||
| 	future_date = base_date.Add(2 * time.Hour) | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ future_date|timeuntil:base_date }}", | ||||
| 	c.Assert(getResult("{{ future_date|timeuntil:base_date }}", | ||||
| 		pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "2 hours from now") | ||||
|  | ||||
| 	base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) | ||||
| 	future_date = base_date.Add(2 * time.Hour) | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ base_date|timesince:future_date }}", | ||||
| 	c.Assert(getResult("{{ base_date|timesince:future_date }}", | ||||
| 		pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "2 hours ago") | ||||
|  | ||||
| 	// Natural time | ||||
| 	base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) | ||||
| 	future_date = base_date.Add(4 * time.Second) | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ base_date|naturaltime:future_date }}", | ||||
| 	c.Assert(getResult("{{ base_date|naturaltime:future_date }}", | ||||
| 		pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "4 seconds ago") | ||||
|  | ||||
| 	// Naturalday | ||||
| @@ -55,32 +61,32 @@ func (s *TestSuite1) TestFilters(c *C) { | ||||
| 	yesterday := today.Add(-24 * time.Hour) | ||||
| 	tomorrow := today.Add(24 * time.Hour) | ||||
| 	today_plus_3 := today.Add(3 * 24 * time.Hour) | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}", | ||||
| 	c.Assert(getResult("{{ date|naturalday:today }}", | ||||
| 		pongo2.Context{"date": today, "today": today}), Equals, "today") | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}", | ||||
| 	c.Assert(getResult("{{ date|naturalday:today }}", | ||||
| 		pongo2.Context{"date": yesterday, "today": today}), Equals, "yesterday") | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}", | ||||
| 	c.Assert(getResult("{{ date|naturalday:today }}", | ||||
| 		pongo2.Context{"date": tomorrow, "today": today}), Equals, "tomorrow") | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}", | ||||
| 	c.Assert(getResult("{{ date|naturalday:today }}", | ||||
| 		pongo2.Context{"date": today_plus_3, "today": today}), Equals, "3 days from now") | ||||
|  | ||||
| 	// Intcomma | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ 123456789|intcomma }}", nil), Equals, "123,456,789") | ||||
| 	c.Assert(getResult("{{ 123456789|intcomma }}", nil), Equals, "123,456,789") | ||||
|  | ||||
| 	// Ordinal | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ 1|ordinal }} {{ 2|ordinal }} {{ 3|ordinal }} {{ 18241|ordinal }}", nil), | ||||
| 	c.Assert(getResult("{{ 1|ordinal }} {{ 2|ordinal }} {{ 3|ordinal }} {{ 18241|ordinal }}", nil), | ||||
| 		Equals, "1st 2nd 3rd 18241st") | ||||
|  | ||||
| 	// Truncatesentences | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ text|truncatesentences:3|safe }}", pongo2.Context{ | ||||
| 	c.Assert(getResult("{{ text|truncatesentences:3|safe }}", pongo2.Context{ | ||||
| 		"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 | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ text|truncatesentences_html:2 }}", pongo2.Context{ | ||||
| 	c.Assert(getResult("{{ text|truncatesentences_html:2 }}", pongo2.Context{ | ||||
| 		"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>`) | ||||
| 	c.Assert(pongo2.RenderTemplateString("{{ text|truncatesentences_html:3 }}", pongo2.Context{ | ||||
| 	c.Assert(getResult("{{ text|truncatesentences_html:3 }}", pongo2.Context{ | ||||
| 		"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>`) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Yuta Hayashibe
					Yuta Hayashibe