Skip to content
Permalink
Browse files

resources/page: Implement compare.ProbablyEqer for the core slices

Fixes #5808
  • Loading branch information...
bep committed Apr 2, 2019
1 parent 5185fb0 commit e91e222cd21213961d1e6206e1523bee2c21fa0c
Showing with 125 additions and 1 deletion.
  1. +40 −1 resources/page/pagegroup.go
  2. +30 −0 resources/page/pages.go
  3. +55 −0 resources/page/pages_test.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) {
@@ -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 {
@@ -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}))

})

}

0 comments on commit e91e222

Please sign in to comment.
You can’t perform that action at this time.