hugolib: Refactor page.ShouldBuild and table driven test

This commit is contained in:
Hanchen Wang 2016-05-11 11:45:09 -04:00 committed by Bjørn Erik Pedersen
parent f7b2e532e2
commit 4724a5794e
2 changed files with 61 additions and 6 deletions

View file

@ -468,13 +468,22 @@ func (p *Page) LinkTitle() string {
}
func (p *Page) ShouldBuild() bool {
if (viper.GetBool("BuildFuture") || p.PublishDate.IsZero() || p.PublishDate.Before(time.Now())) &&
(viper.GetBool("BuildExpired") || p.ExpiryDate.IsZero() || p.ExpiryDate.After(time.Now())) {
if viper.GetBool("BuildDrafts") || !p.Draft {
return true
}
return AssertShouldBuild(viper.GetBool("BuildFuture"), viper.GetBool("BuildExpired"),
viper.GetBool("BuildDrafts"), p.Draft, p.PublishDate, p.ExpiryDate)
}
func AssertShouldBuild(buildFuture bool, buildExpired bool, buildDrafts bool, Draft bool,
publishDate time.Time, expiryDate time.Time) bool {
if !(buildDrafts || !Draft) {
return false
}
return false
if !buildFuture && !publishDate.IsZero() && publishDate.After(time.Now()) {
return false
}
if !buildExpired && !expiryDate.IsZero() && expiryDate.Before(time.Now()) {
return false
}
return true
}
func (p *Page) IsDraft() bool {

View file

@ -1089,3 +1089,49 @@ func compareObjects(a interface{}, b interface{}) bool {
return strings.Join(aStr, "") == strings.Join(bStr, "")
}
func TestAssertShouldBuild(t *testing.T) {
var past = time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
var future = time.Date(2037, 11, 17, 20, 34, 58, 651387237, time.UTC)
var zero = time.Time{}
var publishSettings = []struct {
buildFuture bool
buildExpired bool
buildDrafts bool
draft bool
publishDate time.Time
expiryDate time.Time
out bool
}{
// publishDate and expiryDate
{false, false, false, false, zero, zero, true},
{false, false, false, false, zero, future, true},
{false, false, false, false, past, zero, true},
{false, false, false, false, past, future, true},
{false, false, false, false, past, past, false},
{false, false, false, false, future, future, false},
{false, false, false, false, future, past, false},
// buildFuture and buildExpired
{false, true, false, false, past, past, true},
{true, true, false, false, past, past, true},
{true, false, false, false, past, past, false},
{true, false, false, false, future, future, true},
{true, true, false, false, future, future, true},
{false, true, false, false, future, past, false},
// buildDrafts and draft
{true, true, false, true, past, future, false},
{true, true, true, true, past, future, true},
{true, true, true, true, past, future, true},
}
for _, ps := range publishSettings {
s := AssertShouldBuild(ps.buildFuture, ps.buildExpired, ps.buildDrafts, ps.draft,
ps.publishDate, ps.expiryDate)
if s != ps.out {
t.Errorf("AssertShouldBuild unexpected output with params: %+v", ps)
}
}
}