| 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 |
"time" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/resources/page" |
| 23 |
|
| 24 |
qt "github.com/frankban/quicktest" |
| 25 |
) |
| 26 |
|
| 27 |
func TestSimilarPercentage(t *testing.T) { |
| 28 |
c := qt.New(t) |
| 29 |
|
| 30 |
sentence := "this is some words about nothing, Hugo!" |
| 31 |
words := strings.Fields(sentence) |
| 32 |
for i, j := 0, len(words)-1; i < j; i, j = i+1, j-1 { |
| 33 |
words[i], words[j] = words[j], words[i] |
| 34 |
} |
| 35 |
sentenceReversed := strings.Join(words, " ") |
| 36 |
|
| 37 |
c.Assert(howSimilar("Hugo Rules", "Hugo Rules"), qt.Equals, 100) |
| 38 |
c.Assert(howSimilar("Hugo Rules", "Hugo Rocks"), qt.Equals, 50) |
| 39 |
c.Assert(howSimilar("The Hugo Rules", "The Hugo Rocks"), qt.Equals, 66) |
| 40 |
c.Assert(howSimilar("The Hugo Rules", "The Hugo"), qt.Equals, 66) |
| 41 |
c.Assert(howSimilar("The Hugo", "The Hugo Rules"), qt.Equals, 66) |
| 42 |
c.Assert(howSimilar("Totally different", "Not Same"), qt.Equals, 0) |
| 43 |
c.Assert(howSimilar(sentence, sentenceReversed), qt.Equals, 14) |
| 44 |
c.Assert(howSimilar(template.HTML("Hugo Rules"), template.HTML("Hugo Rules")), qt.Equals, 100) |
| 45 |
c.Assert(howSimilar(map[string]any{"a": 32, "b": 33}, map[string]any{"a": 32, "b": 33}), qt.Equals, 100) |
| 46 |
c.Assert(howSimilar(map[string]any{"a": 32, "b": 33}, map[string]any{"a": 32, "b": 34}), qt.Equals, 0) |
| 47 |
c.Assert(howSimilar("\n", ""), qt.Equals, 100) |
| 48 |
} |
| 49 |
|
| 50 |
type testStruct struct { |
| 51 |
Name string |
| 52 |
} |
| 53 |
|
| 54 |
func TestSimilarPercentageNonString(t *testing.T) { |
| 55 |
c := qt.New(t) |
| 56 |
c.Assert(howSimilar(page.NopPage, page.NopPage), qt.Equals, 100) |
| 57 |
c.Assert(howSimilar(page.Pages{}, page.Pages{}), qt.Equals, 90) |
| 58 |
c.Assert(howSimilar(testStruct{Name: "A"}, testStruct{Name: "B"}), qt.Equals, 0) |
| 59 |
c.Assert(howSimilar(testStruct{Name: "A"}, testStruct{Name: "A"}), qt.Equals, 100) |
| 60 |
} |
| 61 |
|
| 62 |
func BenchmarkHowSimilar(b *testing.B) { |
| 63 |
s1 := "Hugo is cool and " + strings.Repeat("fun ", 10) + "!" |
| 64 |
s2 := "Hugo is cool and " + strings.Repeat("cool ", 10) + "!" |
| 65 |
|
| 66 |
for b.Loop() { |
| 67 |
howSimilar(s1, s2) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func TestFormatDuration(t *testing.T) { |
| 72 |
c := qt.New(t) |
| 73 |
tests := []struct { |
| 74 |
duration time.Duration |
| 75 |
want string |
| 76 |
}{ |
| 77 |
{4*time.Second + 342*time.Millisecond, "4.34 s"}, // additional spacing between value and unit |
| 78 |
{170*time.Millisecond + 289*time.Microsecond, "170.29 ms"}, |
| 79 |
{16*time.Microsecond + 90*time.Nanosecond, "16.09 µs"}, |
| 80 |
{147 * time.Nanosecond, "147.00 ns"}, |
| 81 |
} |
| 82 |
|
| 83 |
for _, tt := range tests { |
| 84 |
got := formatDuration(tt.duration) |
| 85 |
c.Assert(got, qt.Equals, tt.want) |
| 86 |
} |
| 87 |
} |