| 1 |
// Copyright 2019 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 page |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
qt "github.com/frankban/quicktest" |
| 20 |
) |
| 21 |
|
| 22 |
func TestProbablyEq(t *testing.T) { |
| 23 |
p1, p2, p3 := &testPage{title: "p1"}, &testPage{title: "p2"}, &testPage{title: "p3"} |
| 24 |
pages12 := Pages{p1, p2} |
| 25 |
pages21 := Pages{p2, p1} |
| 26 |
pages123 := Pages{p1, p2, p3} |
| 27 |
|
| 28 |
t.Run("Pages", func(t *testing.T) { |
| 29 |
c := qt.New(t) |
| 30 |
|
| 31 |
c.Assert(pages12.ProbablyEq(pages12), qt.Equals, true) |
| 32 |
c.Assert(pages123.ProbablyEq(pages12), qt.Equals, false) |
| 33 |
c.Assert(pages12.ProbablyEq(pages21), qt.Equals, false) |
| 34 |
}) |
| 35 |
|
| 36 |
t.Run("PageGroup", func(t *testing.T) { |
| 37 |
c := qt.New(t) |
| 38 |
|
| 39 |
c.Assert(PageGroup{Key: "a", Pages: pages12}.ProbablyEq(PageGroup{Key: "a", Pages: pages12}), qt.Equals, true) |
| 40 |
c.Assert(PageGroup{Key: "a", Pages: pages12}.ProbablyEq(PageGroup{Key: "b", Pages: pages12}), qt.Equals, false) |
| 41 |
}) |
| 42 |
|
| 43 |
t.Run("PagesGroup", func(t *testing.T) { |
| 44 |
c := qt.New(t) |
| 45 |
|
| 46 |
pg1, pg2 := PageGroup{Key: "a", Pages: pages12}, PageGroup{Key: "b", Pages: pages123} |
| 47 |
|
| 48 |
c.Assert(PagesGroup{pg1, pg2}.ProbablyEq(PagesGroup{pg1, pg2}), qt.Equals, true) |
| 49 |
c.Assert(PagesGroup{pg1, pg2}.ProbablyEq(PagesGroup{pg2, pg1}), qt.Equals, false) |
| 50 |
}) |
| 51 |
} |
| 52 |
|
| 53 |
func TestToPages(t *testing.T) { |
| 54 |
c := qt.New(t) |
| 55 |
|
| 56 |
p1, p2 := &testPage{title: "p1"}, &testPage{title: "p2"} |
| 57 |
pages12 := Pages{p1, p2} |
| 58 |
|
| 59 |
mustToPages := func(in any) Pages { |
| 60 |
p, err := ToPages(in) |
| 61 |
c.Assert(err, qt.IsNil) |
| 62 |
return p |
| 63 |
} |
| 64 |
|
| 65 |
c.Assert(mustToPages(nil), eq, Pages{}) |
| 66 |
c.Assert(mustToPages(pages12), eq, pages12) |
| 67 |
c.Assert(mustToPages([]Page{p1, p2}), eq, pages12) |
| 68 |
c.Assert(mustToPages([]any{p1, p2}), eq, pages12) |
| 69 |
|
| 70 |
_, err := ToPages("not a page") |
| 71 |
c.Assert(err, qt.Not(qt.IsNil)) |
| 72 |
} |
| 73 |
|
| 74 |
func TestIndexOf(t *testing.T) { |
| 75 |
c := qt.New(t) |
| 76 |
|
| 77 |
p1, p2, p3, p4 := &testPage{title: "p1"}, &testPage{title: "p2"}, &testPage{title: "p3"}, &testPage{title: "p4"} |
| 78 |
|
| 79 |
pages := Pages{p1, p2, p3} |
| 80 |
c.Assert(pages.IndexOf(p1), qt.Equals, 0) |
| 81 |
c.Assert(pages.IndexOf(p2), qt.Equals, 1) |
| 82 |
c.Assert(pages.IndexOf(p3), qt.Equals, 2) |
| 83 |
c.Assert(pages.IndexOf(p4), qt.Equals, -1) |
| 84 |
} |