]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
revamp pagegroup tests with quicktest
authorn1xx1 <me@simonemiraglia.it>
Thu, 1 Aug 2024 13:06:28 +0000 (15:06 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 2 Aug 2024 08:03:20 +0000 (10:03 +0200)
resources/page/pagegroup_test.go

index 1168e519e0958f3fcf981123f466785f76e56c84..69243ac48367db8b55f9a03a2401142662d5c732 100644 (file)
@@ -15,7 +15,7 @@ package page
 
 import (
        "context"
-       "reflect"
+       "github.com/google/go-cmp/cmp"
        "strings"
        "testing"
 
@@ -65,7 +65,13 @@ func preparePageGroupTestPages(t *testing.T) Pages {
        return pages
 }
 
+var comparePageGroup = qt.CmpEquals(cmp.Comparer(func(a, b Page) bool {
+       return a == b
+}))
+
 func TestGroupByWithFieldNameArg(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -75,15 +81,13 @@ func TestGroupByWithFieldNameArg(t *testing.T) {
        }
 
        groups, err := pages.GroupBy(context.Background(), "Weight")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByWithMethodNameArg(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -92,15 +96,13 @@ func TestGroupByWithMethodNameArg(t *testing.T) {
        }
 
        groups, err := pages.GroupBy(context.Background(), "Type")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByWithSectionArg(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -109,15 +111,13 @@ func TestGroupByWithSectionArg(t *testing.T) {
        }
 
        groups, err := pages.GroupBy(context.Background(), "Section")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be\n%#v, got\n%#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByInReverseOrder(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -127,47 +127,39 @@ func TestGroupByInReverseOrder(t *testing.T) {
        }
 
        groups, err := pages.GroupBy(context.Background(), "Weight", "desc")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByCalledWithEmptyPages(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        var pages Pages
        groups, err := pages.GroupBy(context.Background(), "Weight")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if groups != nil {
-               t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, qt.IsNil)
 }
 
 func TestReverse(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
 
        groups1, err := pages.GroupBy(context.Background(), "Weight", "desc")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
+       c.Assert(err, qt.IsNil)
 
        groups2, err := pages.GroupBy(context.Background(), "Weight")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       groups2 = groups2.Reverse()
+       c.Assert(err, qt.IsNil)
 
-       if !reflect.DeepEqual(groups2, groups1) {
-               t.Errorf("PagesGroup is sorted in unexpected order. It should be %#v, got %#v", groups2, groups1)
-       }
+       groups2 = groups2.Reverse()
+       c.Assert(groups2, comparePageGroup, groups1)
 }
 
 func TestGroupByParam(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -177,15 +169,13 @@ func TestGroupByParam(t *testing.T) {
        }
 
        groups, err := pages.GroupByParam("custom_param")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByParamInReverseOrder(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -195,12 +185,8 @@ func TestGroupByParamInReverseOrder(t *testing.T) {
        }
 
        groups, err := pages.GroupByParam("custom_param", "desc")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByParamCalledWithCapitalLetterString(t *testing.T) {
@@ -213,10 +199,12 @@ func TestGroupByParamCalledWithCapitalLetterString(t *testing.T) {
        groups, err := pages.GroupByParam("custom_param")
 
        c.Assert(err, qt.IsNil)
-       c.Assert(groups[0].Key, qt.Equals, testStr)
+       c.Assert(groups[0].Key, qt.DeepEquals, testStr)
 }
 
 func TestGroupByParamCalledWithSomeUnavailableParams(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        delete(pages[1].Params(), "custom_param")
@@ -228,36 +216,32 @@ func TestGroupByParamCalledWithSomeUnavailableParams(t *testing.T) {
        }
 
        groups, err := pages.GroupByParam("custom_param")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByParamCalledWithEmptyPages(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        var pages Pages
        groups, err := pages.GroupByParam("custom_param")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if groups != nil {
-               t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, qt.IsNil)
 }
 
 func TestGroupByParamCalledWithUnavailableParam(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        _, err := pages.GroupByParam("unavailable_param")
-       if err != nil {
-               t.Errorf("GroupByParam returned an error when it shouldn't")
-       }
+       c.Assert(err, qt.IsNil)
 }
 
 func TestGroupByParamNested(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
 
@@ -268,15 +252,13 @@ func TestGroupByParamNested(t *testing.T) {
        }
 
        groups, err := pages.GroupByParam("custom_object.param")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByDate(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -286,15 +268,13 @@ func TestGroupByDate(t *testing.T) {
        }
 
        groups, err := pages.GroupByDate("2006-01")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByDateInReverseOrder(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -304,15 +284,13 @@ func TestGroupByDateInReverseOrder(t *testing.T) {
        }
 
        groups, err := pages.GroupByDate("2006-01", "asc")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByPublishDate(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -322,15 +300,13 @@ func TestGroupByPublishDate(t *testing.T) {
        }
 
        groups, err := pages.GroupByPublishDate("2006-01")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByPublishDateInReverseOrder(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -340,27 +316,23 @@ func TestGroupByPublishDateInReverseOrder(t *testing.T) {
        }
 
        groups, err := pages.GroupByDate("2006-01", "asc")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByPublishDateWithEmptyPages(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        var pages Pages
        groups, err := pages.GroupByPublishDate("2006-01")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if groups != nil {
-               t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, qt.IsNil)
 }
 
 func TestGroupByExpiryDate(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -370,15 +342,13 @@ func TestGroupByExpiryDate(t *testing.T) {
        }
 
        groups, err := pages.GroupByExpiryDate("2006-01")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByParamDate(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -388,15 +358,13 @@ func TestGroupByParamDate(t *testing.T) {
        }
 
        groups, err := pages.GroupByParamDate("custom_date", "2006-01")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByParamDateNested(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -406,16 +374,14 @@ func TestGroupByParamDateNested(t *testing.T) {
        }
 
        groups, err := pages.GroupByParamDate("custom_object.date", "2006-01")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 // https://github.com/gohugoio/hugo/issues/3983
 func TestGroupByParamDateWithStringParams(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -425,15 +391,13 @@ func TestGroupByParamDateWithStringParams(t *testing.T) {
        }
 
        groups, err := pages.GroupByParamDate("custom_string_date", "2006-01")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByParamDateNestedWithStringParams(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -443,15 +407,13 @@ func TestGroupByParamDateNestedWithStringParams(t *testing.T) {
        }
 
        groups, err := pages.GroupByParamDate("custom_object.string_date", "2006-01")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByLastmod(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -461,15 +423,13 @@ func TestGroupByLastmod(t *testing.T) {
        }
 
        groups, err := pages.GroupByLastmod("2006-01")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByLastmodInReverseOrder(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -479,15 +439,13 @@ func TestGroupByLastmodInReverseOrder(t *testing.T) {
        }
 
        groups, err := pages.GroupByLastmod("2006-01", "asc")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be\n%#v, got\n%#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByParamDateInReverseOrder(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        pages := preparePageGroupTestPages(t)
        expect := PagesGroup{
@@ -497,22 +455,16 @@ func TestGroupByParamDateInReverseOrder(t *testing.T) {
        }
 
        groups, err := pages.GroupByParamDate("custom_date", "2006-01", "asc")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if !reflect.DeepEqual(groups, expect) {
-               t.Errorf("PagesGroup has unexpected groups. It should be %#v, got %#v", expect, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, comparePageGroup, expect)
 }
 
 func TestGroupByParamDateWithEmptyPages(t *testing.T) {
+       c := qt.New(t)
+
        t.Parallel()
        var pages Pages
        groups, err := pages.GroupByParamDate("custom_date", "2006-01")
-       if err != nil {
-               t.Fatalf("Unable to make PagesGroup array: %s", err)
-       }
-       if groups != nil {
-               t.Errorf("PagesGroup isn't empty. It should be %#v, got %#v", nil, groups)
-       }
+       c.Assert(err, qt.IsNil)
+       c.Assert(groups, qt.IsNil)
 }