ref: 4494a01b794ab785c64c8e93c61ccbfa845bc478
parent: e91e222cd21213961d1e6206e1523bee2c21fa0c
author: Bjørn Erik Pedersen <[email protected]>
date: Tue Apr 2 16:43:58 EDT 2019
metrics: Adjust the howSimilar logic vs strings Also add a test.
--- a/metrics/metrics.go
+++ b/metrics/metrics.go
@@ -27,8 +27,6 @@
"github.com/gohugoio/hugo/compare"
"github.com/gohugoio/hugo/common/hreflect"
-
- "github.com/spf13/cast"
)
// The Provider interface defines an interface for measuring metrics.
@@ -198,18 +196,17 @@
// howSimilar is a naive diff implementation that returns
// a number between 0-100 indicating how similar a and b are.
func howSimilar(a, b interface{}) int {
- if a == b {
- return 100
- }
+ // TODO(bep) object equality fast path, but remember that
+ // we can get anytning in here.
- as, err1 := cast.ToStringE(a)
- bs, err2 := cast.ToStringE(b)
+ as, ok1 := a.(string)
+ bs, ok2 := b.(string)
- if err1 == nil && err2 == nil {
+ if ok1 && ok2 {
return howSimilarStrings(as, bs)
}
- if err1 != err2 {
+ if ok1 != ok2 {
return 0
}
@@ -219,7 +216,6 @@
return 100
}
- // TODO(bep) implement ProbablyEq for Pages etc.
pe1, pok1 := a.(compare.ProbablyEqer)
pe2, pok2 := b.(compare.ProbablyEqer)
if pok1 && pok2 && pe1.ProbablyEq(pe2) {
--- a/metrics/metrics_test.go
+++ b/metrics/metrics_test.go
@@ -17,6 +17,8 @@
"strings"
"testing"
+ "github.com/gohugoio/hugo/resources/page"
+
"github.com/stretchr/testify/require"
)
@@ -38,6 +40,12 @@
assert.Equal(0, howSimilar("Totally different", "Not Same"))
assert.Equal(14, howSimilar(sentence, sentenceReversed))
+}
+
+func TestSimilarPercentageNonString(t *testing.T) {
+ assert := require.New(t)
+ assert.Equal(100, howSimilar(page.NopPage, page.NopPage))
+ assert.Equal(90, howSimilar(page.Pages{}, page.Pages{}))
}
func BenchmarkHowSimilar(b *testing.B) {