2017-10-04 16:12:51 -04:00
|
|
|
// Copyright 2017 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 metrics
|
|
|
|
|
|
|
|
import (
|
2020-03-12 12:09:49 -04:00
|
|
|
"html/template"
|
2017-10-04 16:12:51 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-04-02 14:43:58 -04:00
|
|
|
"github.com/gohugoio/hugo/resources/page"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2017-10-04 16:12:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSimilarPercentage(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-10-04 16:12:51 -04:00
|
|
|
|
|
|
|
sentence := "this is some words about nothing, Hugo!"
|
|
|
|
words := strings.Fields(sentence)
|
|
|
|
for i, j := 0, len(words)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
words[i], words[j] = words[j], words[i]
|
|
|
|
}
|
|
|
|
sentenceReversed := strings.Join(words, " ")
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(howSimilar("Hugo Rules", "Hugo Rules"), qt.Equals, 100)
|
|
|
|
c.Assert(howSimilar("Hugo Rules", "Hugo Rocks"), qt.Equals, 50)
|
|
|
|
c.Assert(howSimilar("The Hugo Rules", "The Hugo Rocks"), qt.Equals, 66)
|
|
|
|
c.Assert(howSimilar("The Hugo Rules", "The Hugo"), qt.Equals, 66)
|
|
|
|
c.Assert(howSimilar("The Hugo", "The Hugo Rules"), qt.Equals, 66)
|
|
|
|
c.Assert(howSimilar("Totally different", "Not Same"), qt.Equals, 0)
|
|
|
|
c.Assert(howSimilar(sentence, sentenceReversed), qt.Equals, 14)
|
2020-03-12 12:09:49 -04:00
|
|
|
c.Assert(howSimilar(template.HTML("Hugo Rules"), template.HTML("Hugo Rules")), qt.Equals, 100)
|
2022-03-17 17:03:27 -04:00
|
|
|
c.Assert(howSimilar(map[string]any{"a": 32, "b": 33}, map[string]any{"a": 32, "b": 33}), qt.Equals, 100)
|
|
|
|
c.Assert(howSimilar(map[string]any{"a": 32, "b": 33}, map[string]any{"a": 32, "b": 34}), qt.Equals, 0)
|
2022-05-25 22:14:37 -04:00
|
|
|
c.Assert(howSimilar("\n", ""), qt.Equals, 100)
|
2017-10-04 16:12:51 -04:00
|
|
|
}
|
|
|
|
|
2020-03-12 12:09:49 -04:00
|
|
|
type testStruct struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2019-04-02 14:43:58 -04:00
|
|
|
func TestSimilarPercentageNonString(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
|
|
|
c.Assert(howSimilar(page.NopPage, page.NopPage), qt.Equals, 100)
|
|
|
|
c.Assert(howSimilar(page.Pages{}, page.Pages{}), qt.Equals, 90)
|
2020-03-12 12:09:49 -04:00
|
|
|
c.Assert(howSimilar(testStruct{Name: "A"}, testStruct{Name: "B"}), qt.Equals, 0)
|
|
|
|
c.Assert(howSimilar(testStruct{Name: "A"}, testStruct{Name: "A"}), qt.Equals, 100)
|
2019-04-02 14:43:58 -04:00
|
|
|
}
|
|
|
|
|
2017-10-04 16:12:51 -04:00
|
|
|
func BenchmarkHowSimilar(b *testing.B) {
|
|
|
|
s1 := "Hugo is cool and " + strings.Repeat("fun ", 10) + "!"
|
|
|
|
s2 := "Hugo is cool and " + strings.Repeat("cool ", 10) + "!"
|
|
|
|
|
2017-10-07 14:41:25 -04:00
|
|
|
b.ResetTimer()
|
2017-10-04 16:12:51 -04:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
howSimilar(s1, s2)
|
|
|
|
}
|
|
|
|
}
|