ref: 4724a5794eaaf582252b988a7def8a792b5bccdf
parent: f7b2e532e21c74a0825b39332ec595b95c1f6b23
author: Hanchen Wang <hanchen.wang@mail.utoronto.ca>
date: Wed May 11 07:45:09 EDT 2016
hugolib: Refactor page.ShouldBuild and table driven test
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -468,13 +468,22 @@
}
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 {--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -1089,3 +1089,49 @@
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)+ }
+ }
+}
--
⑨