hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

metrics_test.go (2530B)

    1 // Copyright 2017 The Hugo Authors. All rights reserved.
    2 //
    3 // Licensed under the Apache License, Version 2.0 (the "License");
    4 // you may not use this file except in compliance with the License.
    5 // You may obtain a copy of the License at
    6 // http://www.apache.org/licenses/LICENSE-2.0
    7 //
    8 // Unless required by applicable law or agreed to in writing, software
    9 // distributed under the License is distributed on an "AS IS" BASIS,
   10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   11 // See the License for the specific language governing permissions and
   12 // limitations under the License.
   13 
   14 package metrics
   15 
   16 import (
   17 	"html/template"
   18 	"strings"
   19 	"testing"
   20 
   21 	"github.com/gohugoio/hugo/resources/page"
   22 
   23 	qt "github.com/frankban/quicktest"
   24 )
   25 
   26 func TestSimilarPercentage(t *testing.T) {
   27 	c := qt.New(t)
   28 
   29 	sentence := "this is some words about nothing, Hugo!"
   30 	words := strings.Fields(sentence)
   31 	for i, j := 0, len(words)-1; i < j; i, j = i+1, j-1 {
   32 		words[i], words[j] = words[j], words[i]
   33 	}
   34 	sentenceReversed := strings.Join(words, " ")
   35 
   36 	c.Assert(howSimilar("Hugo Rules", "Hugo Rules"), qt.Equals, 100)
   37 	c.Assert(howSimilar("Hugo Rules", "Hugo Rocks"), qt.Equals, 50)
   38 	c.Assert(howSimilar("The Hugo Rules", "The Hugo Rocks"), qt.Equals, 66)
   39 	c.Assert(howSimilar("The Hugo Rules", "The Hugo"), qt.Equals, 66)
   40 	c.Assert(howSimilar("The Hugo", "The Hugo Rules"), qt.Equals, 66)
   41 	c.Assert(howSimilar("Totally different", "Not Same"), qt.Equals, 0)
   42 	c.Assert(howSimilar(sentence, sentenceReversed), qt.Equals, 14)
   43 	c.Assert(howSimilar(template.HTML("Hugo Rules"), template.HTML("Hugo Rules")), qt.Equals, 100)
   44 	c.Assert(howSimilar(map[string]any{"a": 32, "b": 33}, map[string]any{"a": 32, "b": 33}), qt.Equals, 100)
   45 	c.Assert(howSimilar(map[string]any{"a": 32, "b": 33}, map[string]any{"a": 32, "b": 34}), qt.Equals, 0)
   46 	c.Assert(howSimilar("\n", ""), qt.Equals, 100)
   47 }
   48 
   49 type testStruct struct {
   50 	Name string
   51 }
   52 
   53 func TestSimilarPercentageNonString(t *testing.T) {
   54 	c := qt.New(t)
   55 	c.Assert(howSimilar(page.NopPage, page.NopPage), qt.Equals, 100)
   56 	c.Assert(howSimilar(page.Pages{}, page.Pages{}), qt.Equals, 90)
   57 	c.Assert(howSimilar(testStruct{Name: "A"}, testStruct{Name: "B"}), qt.Equals, 0)
   58 	c.Assert(howSimilar(testStruct{Name: "A"}, testStruct{Name: "A"}), qt.Equals, 100)
   59 }
   60 
   61 func BenchmarkHowSimilar(b *testing.B) {
   62 	s1 := "Hugo is cool and " + strings.Repeat("fun ", 10) + "!"
   63 	s2 := "Hugo is cool and " + strings.Repeat("cool ", 10) + "!"
   64 
   65 	b.ResetTimer()
   66 	for i := 0; i < b.N; i++ {
   67 		howSimilar(s1, s2)
   68 	}
   69 }