shithub: hugo

Download patch

ref: e91e222cd21213961d1e6206e1523bee2c21fa0c
parent: 5185fb065b0f8a4142c29ee3e3cd917e917280a4
author: Bjørn Erik Pedersen <[email protected]>
date: Tue Apr 2 06:52:43 EDT 2019

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

Fixes #5808

--- a/resources/page/pagegroup.go
+++ b/resources/page/pagegroup.go
@@ -22,12 +22,15 @@
 	"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 @@
 	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) {
@@ -335,6 +353,27 @@
 		l += len(pg.Pages)
 	}
 	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.
--- a/resources/page/pages.go
+++ b/resources/page/pages.go
@@ -17,11 +17,14 @@
 	"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.
@@ -93,6 +96,33 @@
 // Len returns the number of pages in the list.
 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 {
--- /dev/null
+++ b/resources/page/pages_test.go
@@ -1,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}))
+
+	})
+
+}