diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-04-02 10:52:43 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-04-02 16:10:13 +0200 |
commit | e91e222cd21213961d1e6206e1523bee2c21fa0c (patch) | |
tree | 5e97ed5b92fe54e6ea94bac3caa1cdd24e5631fd /resources/page | |
parent | 5185fb065b0f8a4142c29ee3e3cd917e917280a4 (diff) |
resources/page: Implement compare.ProbablyEqer for the core slices
Fixes #5808
Diffstat (limited to 'resources/page')
-rw-r--r-- | resources/page/pagegroup.go | 41 | ||||
-rw-r--r-- | resources/page/pages.go | 30 | ||||
-rw-r--r-- | resources/page/pages_test.go | 55 |
3 files changed, 125 insertions, 1 deletions
diff --git a/resources/page/pagegroup.go b/resources/page/pagegroup.go index 46d9bd174..3d87d9014 100644 --- a/resources/page/pagegroup.go +++ b/resources/page/pagegroup.go @@ -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) { diff --git a/resources/page/pages.go b/resources/page/pages.go index 1f79932a9..ccfecdf2b 100644 --- a/resources/page/pages.go +++ b/resources/page/pages.go @@ -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 index 000000000..5220a6d33 --- /dev/null +++ b/resources/page/pages_test.go @@ -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})) + + }) + +} |