shithub: hugo

Download patch

ref: 207d8fb7af6d1b7bcb9753a290fd60e90f8e5e0c
parent: 3ecc698f5e109610288b48b49c5ac2ee442a5ed0
author: Noah Campbell <[email protected]>
date: Tue Sep 3 10:51:06 EDT 2013

Date rendering unit tests in pages

Tests to ensure rendering dates in templates is working correctly.
Actually, I was running into invalid templates not giving warnings when
I was trying to render a date.

--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -2,6 +2,7 @@
 
 import (
 	"path/filepath"
+	"time"
 	"strings"
 	"testing"
 	"html/template"
@@ -54,7 +55,7 @@
 
 Content of the file goes Here
 `
-
+var SIMPLE_PAGE_RFC3339_DATE = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content"
 var SIMPLE_PAGE_JSON_MULTIPLE = `
 {
 	"title": "foobar",
@@ -165,6 +166,12 @@
 	}
 }
 
+func checkPageDate(t *testing.T, page *Page, time time.Time) {
+	if page.Date != time {
+		t.Fatalf("Page date is: %s.  Expected: %s", page.Date, time)
+	}
+}
+
 func TestCreateNewPage(t *testing.T) {
 	p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE), "simple")
 	if err != nil {
@@ -200,6 +207,18 @@
 	checkPageSummary(t, p, "<p>Simple Page</p>\n")
 	checkPageType(t, p, "page")
 	checkPageLayout(t, p, "page/single.html")
+}
+
+func TestPageWithDate(t *testing.T) {
+	p, err := ReadFrom(strings.NewReader(SIMPLE_PAGE_RFC3339_DATE), "simple")
+	if err != nil {
+		t.Fatalf("Unable to create a page with frontmatter and body content: %s", err)
+	}
+	d, err := time.Parse(time.RFC3339, "2013-05-17T16:59:30Z")
+	if err != nil {
+		t.Fatalf("Unable to prase page.")
+	}
+	checkPageDate(t, p, d)
 }
 
 func TestCreatePage(t *testing.T) {
--- a/hugolib/site_test.go
+++ b/hugolib/site_test.go
@@ -17,6 +17,8 @@
 var TEMPLATE_MISSING_FUNC = "{{ .Title | funcdoesnotexists }}"
 var TEMPLATE_FUNC = "{{ .Title | urlize }}"
 var TEMPLATE_CONTENT = "{{ .Content }}"
+var TEMPLATE_DATE = "{{ .Date }}"
+var INVALID_TEMPLATE_FORMAT_DATE = "{{ .Date.Format time.RFC3339 }}"
 
 var PAGE_URL_SPECIFIED = `---
 title: simple template
@@ -106,6 +108,7 @@
 		{PAGE_SIMPLE_TITLE, TEMPLATE_TITLE, "simple template"},
 		{PAGE_SIMPLE_TITLE, TEMPLATE_FUNC, "simple-template"},
 		{PAGE_WITH_MD, TEMPLATE_CONTENT, "<h1>heading 1</h1>\n\n<p>text</p>\n\n<h2>heading 2</h2>\n\n<p>more text</p>\n"},
+		{SIMPLE_PAGE_RFC3339_DATE, TEMPLATE_DATE, "2013-05-17 16:59:30 &#43;0000 UTC"},
 	}
 
 	s := new(Site)
@@ -129,7 +132,7 @@
 		}
 
 		if string(html.Bytes()) != test.expected {
-			t.Errorf("Content does not match.  Expected\n\t'%q'\ngot\n\t'%q'", test.expected, html)
+			t.Errorf("Content does not match.\nExpected\n\t'%q'\ngot\n\t'%q'", test.expected, html)
 		}
 	}
 }