Skip to content

Commit

Permalink
Test work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 23, 2018
1 parent 69b97b2 commit 247a82f
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hugolib/page_frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ func init() {
}

func (f frontmatterHandler) handleDates(d frontMatterDescriptor) error {
if d.dates == nil {
panic("missing dates")
}

err := f.handleDate(d)
if err != nil {
Expand Down
133 changes: 133 additions & 0 deletions hugolib/page_frontmatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"testing"
"time"

"github.com/spf13/viper"

"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -58,3 +60,134 @@ func TestDateAndSlugFromBaseFilename(t *testing.T) {

}
}

func TestFrontMatterDates(t *testing.T) {
t.Parallel()

defaultDateSettings := []string{"none", "file"}

for _, defaultDateSetting := range defaultDateSettings {
t.Run(fmt.Sprintf("defaultDate=%s", defaultDateSetting), func(t *testing.T) {
doTestFrontMatterDates(t, defaultDateSetting)
})
}
}

func doTestFrontMatterDates(t *testing.T, defaultDateSetting string) {
assert := require.New(t)

cfg := viper.New()

cfg.Set("frontmatter", map[string]interface{}{
"defaultDate": []string{defaultDateSetting},
})

handler, err := newFrontmatterHandler(newWarningLogger(), cfg)
assert.NoError(err)

testDate, err := time.Parse("2006-01-02", "2018-02-01")
assert.NoError(err)

sentinel := (time.Time{}).Add(1 * time.Hour)
zero := time.Time{}

// See http://www.imdb.com/title/tt0133093/
for _, lastModKey := range []string{"lastmod", "modified"} {
for _, lastModDate := range []time.Time{testDate, sentinel} {
for _, dateKey := range []string{"date"} {
testDate = testDate.Add(24 * time.Hour)
for _, dateDate := range []time.Time{testDate, sentinel} {
for _, pubDateKey := range []string{"publishdate", "pubdate", "published"} {
testDate = testDate.Add(24 * time.Hour)
for _, pubDateDate := range []time.Time{testDate, sentinel} {
for _, expiryDateKey := range []string{"expirydate", "unpublishdate"} {
testDate = testDate.Add(24 * time.Hour)
for _, expiryDateDate := range []time.Time{testDate, sentinel} {
d := frontMatterDescriptor{
frontmatter: make(map[string]interface{}),
params: make(map[string]interface{}),
dates: &PageDates{},
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
}

if dateDate != sentinel {
d.frontmatter[dateKey] = dateDate
expDate = dateDate
}

if pubDateDate != sentinel {
d.frontmatter[pubDateKey] = pubDateDate
expPubDate = pubDateDate
}

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

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")

}
}
}
}
}
}
}
}
}

func assertFrontMatterDate(assert *require.Assertions, d frontMatterDescriptor, expected time.Time, dateField string) {
switch dateField {
case "date":
case "lastmod":
case "publishdate":
case "expirydate":
default:
assert.Failf("Unknown datefield %s", dateField)
}

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)

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)

}
}

type dateTestHelper struct {
name string

dates PageDates
}

func (d dateTestHelper) descriptor() frontMatterDescriptor {
return frontMatterDescriptor{dates: &d.dates}
}

func (d dateTestHelper) assert(t *testing.T) {

}

0 comments on commit 247a82f

Please sign in to comment.