hugolib: Refactor page.ShouldBuild and table driven test
authorHanchen Wang <hanchen.wang@mail.utoronto.ca>
Wed, 11 May 2016 15:45:09 +0000 (11:45 -0400)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 14 Jun 2016 13:45:26 +0000 (15:45 +0200)
hugolib/page.go
hugolib/page_test.go

index 5c81d79ee8d8ce6a86b97a478b0d29ed9ba01473..3fd5309ed836de90d28671a4f6e36622e86e082b 100644 (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 {
index 876a74caf7bd73d2a957470edc293aa1ea7eb2fd..7a49c611c1f8c372c0f6e03d50e33273df7102a4 100644 (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)
+               }
+       }
+}