Skip to content

Commit

Permalink
More
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 23, 2018
1 parent 247a82f commit 0bb9de7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
5 changes: 5 additions & 0 deletions hugolib/page_frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func (f frontmatterHandler) handleDates(d frontMatterDescriptor) error {
d.dates.Lastmod = d.dates.Date
}

// TODO(bep) date decide vs https://github.com/gohugoio/hugo/issues/3977
if d.dates.PublishDate.IsZero() {
//d.dates.PublishDate = d.dates.Date
}

if d.dates.Date.IsZero() {
d.dates.Date = d.dates.Lastmod
}
Expand Down
42 changes: 28 additions & 14 deletions hugolib/page_frontmatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,20 @@ func doTestFrontMatterDates(t *testing.T, defaultDateSetting string) {

// See http://www.imdb.com/title/tt0133093/
for _, lastModKey := range []string{"lastmod", "modified"} {
testDate = testDate.Add(24 * time.Hour)
t.Log(lastModKey, testDate)
for _, lastModDate := range []time.Time{testDate, sentinel} {
for _, dateKey := range []string{"date"} {
testDate = testDate.Add(24 * time.Hour)
t.Log(dateKey, testDate)
for _, dateDate := range []time.Time{testDate, sentinel} {
for _, pubDateKey := range []string{"publishdate", "pubdate", "published"} {
testDate = testDate.Add(24 * time.Hour)
t.Log(pubDateKey, testDate)
for _, pubDateDate := range []time.Time{testDate, sentinel} {
for _, expiryDateKey := range []string{"expirydate", "unpublishdate"} {
testDate = testDate.Add(24 * time.Hour)
t.Log(expiryDateKey, testDate)
for _, expiryDateDate := range []time.Time{testDate, sentinel} {
d := frontMatterDescriptor{
frontmatter: make(map[string]interface{}),
Expand All @@ -110,16 +115,7 @@ func doTestFrontMatterDates(t *testing.T, defaultDateSetting string) {
pageURLs: &URLPath{},
}

var (
// expLastModP, expDateP, expPubDateP, expExiryDateP = sentinel, sentinel, sentinel, sentinel
expLastMod, expDate, expPubDate, expExiryDate = zero, zero, zero, zero
)

if lastModDate != sentinel {
d.frontmatter[lastModKey] = lastModDate
expLastMod = lastModDate
expDate = lastModDate
}
var expLastMod, expDate, expPubDate, expExiryDate = zero, zero, zero, zero

if dateDate != sentinel {
d.frontmatter[dateKey] = dateDate
Expand All @@ -129,20 +125,35 @@ func doTestFrontMatterDates(t *testing.T, defaultDateSetting string) {
if pubDateDate != sentinel {
d.frontmatter[pubDateKey] = pubDateDate
expPubDate = pubDateDate
if expDate.IsZero() {
expDate = expPubDate
}
}

if lastModDate != sentinel {
d.frontmatter[lastModKey] = lastModDate
expLastMod = lastModDate

if expDate.IsZero() {
expDate = lastModDate
}
}

if expiryDateDate != sentinel {
d.frontmatter[expiryDateKey] = expiryDateDate
expExiryDate = expiryDateDate
}

if expLastMod.IsZero() {
expLastMod = expDate
}

assert.NoError(handler.handleDates(d))

assertFrontMatterDate(assert, d, expDate, "date")
assertFrontMatterDate(assert, d, expLastMod, "lastmod")
assertFrontMatterDate(assert, d, expPubDate, "publishdate")
assertFrontMatterDate(assert, d, expExiryDate, "expirydate")

}
}
}
Expand All @@ -165,16 +176,19 @@ func assertFrontMatterDate(assert *require.Assertions, d frontMatterDescriptor,

param, found := d.params[dateField]

message := fmt.Sprintf("[%s] Found: %t Expected: %v Params: %v Front matter: %v", dateField, found, expected, d.params, d.frontmatter)
if found && param.(time.Time).IsZero() {
assert.Fail("Zero time in params", dateField)
}

message := fmt.Sprintf("[%s] Found: %t Expected: %v (%t) Param: %v Params: %v Front matter: %v",
dateField, found, expected, expected.IsZero(), param, d.params, d.frontmatter)

assert.True(found != expected.IsZero(), message)

if found {
if expected != param {
assert.Fail("Params check failed", "[%s] Expected:\n%q\nGot:\n%q", dateField, expected, param)
}
assert.Equal(expected, param)

}
}

Expand Down

0 comments on commit 0bb9de7

Please sign in to comment.