resources/page: Implement compare.ProbablyEqer for the core slices
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 2 Apr 2019 08:52:43 +0000 (10:52 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 2 Apr 2019 14:10:13 +0000 (16:10 +0200)
Fixes #5808

resources/page/pagegroup.go
resources/page/pages.go
resources/page/pages_test.go [new file with mode: 0644]

index 46d9bd174440e48369f0d8b2425703f5078e1323..3d87d90144fff16de2e66d5ed995d4ced3802392 100644 (file)
@@ -22,12 +22,15 @@ import (
        "time"
 
        "github.com/gohugoio/hugo/common/collections"
+       "github.com/gohugoio/hugo/compare"
 
        "github.com/gohugoio/hugo/resources/resource"
 )
 
 var (
-       _ collections.Slicer = PageGroup{}
+       _ collections.Slicer   = PageGroup{}
+       _ compare.ProbablyEqer = PageGroup{}
+       _ compare.ProbablyEqer = PagesGroup{}
 )
 
 // PageGroup represents a group of pages, grouped by the key.
@@ -307,6 +310,21 @@ func (p Pages) GroupByParamDate(key string, format string, order ...string) (Pag
        return p.groupByDateField(sorter, formatter, order...)
 }
 
+// ProbablyEq wraps comare.ProbablyEqer
+func (p PageGroup) ProbablyEq(other interface{}) bool {
+       otherP, ok := other.(PageGroup)
+       if !ok {
+               return false
+       }
+
+       if p.Key != otherP.Key {
+               return false
+       }
+
+       return p.Pages.ProbablyEq(otherP.Pages)
+
+}
+
 // Slice is not meant to be used externally. It's a bridge function
 // for the template functions. See collections.Slice.
 func (p PageGroup) Slice(in interface{}) (interface{}, error) {
@@ -337,6 +355,27 @@ func (psg PagesGroup) Len() int {
        return l
 }
 
+// ProbablyEq wraps comare.ProbablyEqer
+func (psg PagesGroup) ProbablyEq(other interface{}) bool {
+       otherPsg, ok := other.(PagesGroup)
+       if !ok {
+               return false
+       }
+
+       if len(psg) != len(otherPsg) {
+               return false
+       }
+
+       for i := range psg {
+               if !psg[i].ProbablyEq(otherPsg[i]) {
+                       return false
+               }
+       }
+
+       return true
+
+}
+
 // ToPagesGroup tries to convert seq into a PagesGroup.
 func ToPagesGroup(seq interface{}) (PagesGroup, error) {
        switch v := seq.(type) {
index 1f79932a9cb2637fd7d4c49b34e993acb1a4efa4..ccfecdf2bc0950ad748327608d2c5b43cd2e6d52 100644 (file)
@@ -17,11 +17,14 @@ import (
        "fmt"
        "math/rand"
 
+       "github.com/gohugoio/hugo/compare"
+
        "github.com/gohugoio/hugo/resources/resource"
 )
 
 var (
        _ resource.ResourcesConverter = Pages{}
+       _ compare.ProbablyEqer        = Pages{}
 )
 
 // Pages is a slice of pages. This is the most common list type in Hugo.
@@ -95,6 +98,33 @@ func (p Pages) Len() int {
        return len(p)
 }
 
+// ProbablyEq wraps comare.ProbablyEqer
+func (pages Pages) ProbablyEq(other interface{}) bool {
+       otherPages, ok := other.(Pages)
+       if !ok {
+               return false
+       }
+
+       if len(pages) != len(otherPages) {
+               return false
+       }
+
+       step := 1
+
+       for i := 0; i < len(pages); i += step {
+               if !pages[i].Eq(otherPages[i]) {
+                       return false
+               }
+
+               if i > 50 {
+                       // This is most likely the same.
+                       step = 50
+               }
+       }
+
+       return true
+}
+
 func (ps Pages) removeFirstIfFound(p Page) Pages {
        ii := -1
        for i, pp := range ps {
diff --git a/resources/page/pages_test.go b/resources/page/pages_test.go
new file mode 100644 (file)
index 0000000..5220a6d
--- /dev/null
@@ -0,0 +1,55 @@
+// Copyright 2019 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package page
+
+import (
+       "testing"
+
+       "github.com/stretchr/testify/require"
+)
+
+func TestProbablyEq(t *testing.T) {
+
+       p1, p2, p3 := &testPage{title: "p1"}, &testPage{title: "p2"}, &testPage{title: "p3"}
+       pages12 := Pages{p1, p2}
+       pages21 := Pages{p2, p1}
+       pages123 := Pages{p1, p2, p3}
+
+       t.Run("Pages", func(t *testing.T) {
+               assert := require.New(t)
+
+               assert.True(pages12.ProbablyEq(pages12))
+               assert.False(pages123.ProbablyEq(pages12))
+               assert.False(pages12.ProbablyEq(pages21))
+       })
+
+       t.Run("PageGroup", func(t *testing.T) {
+               assert := require.New(t)
+
+               assert.True(PageGroup{Key: "a", Pages: pages12}.ProbablyEq(PageGroup{Key: "a", Pages: pages12}))
+               assert.False(PageGroup{Key: "a", Pages: pages12}.ProbablyEq(PageGroup{Key: "b", Pages: pages12}))
+
+       })
+
+       t.Run("PagesGroup", func(t *testing.T) {
+               assert := require.New(t)
+
+               pg1, pg2 := PageGroup{Key: "a", Pages: pages12}, PageGroup{Key: "b", Pages: pages123}
+
+               assert.True(PagesGroup{pg1, pg2}.ProbablyEq(PagesGroup{pg1, pg2}))
+               assert.False(PagesGroup{pg1, pg2}.ProbablyEq(PagesGroup{pg2, pg1}))
+
+       })
+
+}