2019-01-02 06:33:26 -05:00
|
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2017-03-13 18:55:02 -04:00
|
|
|
|
//
|
|
|
|
|
// 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 collections
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-11 03:48:57 -04:00
|
|
|
|
"context"
|
2017-03-13 18:55:02 -04:00
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"html/template"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
|
"github.com/gohugoio/hugo/common/maps"
|
2023-01-04 12:24:36 -05:00
|
|
|
|
"github.com/gohugoio/hugo/config/testconfig"
|
2019-11-21 15:59:38 -05:00
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
qt "github.com/frankban/quicktest"
|
2017-03-13 18:55:02 -04:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type tstNoStringer struct{}
|
|
|
|
|
|
|
|
|
|
func TestAfter(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
index any
|
|
|
|
|
seq any
|
|
|
|
|
expect any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}{
|
|
|
|
|
{int(2), []string{"a", "b", "c", "d"}, []string{"c", "d"}},
|
2018-07-01 14:34:02 -04:00
|
|
|
|
{int32(3), []string{"a", "b"}, []string{}},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{int64(2), []int{100, 200, 300}, []int{300}},
|
2018-07-01 14:34:02 -04:00
|
|
|
|
{100, []int{100, 200}, []int{}},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{"1", []int{100, 200, 300}, []int{200, 300}},
|
2019-10-10 21:16:15 -04:00
|
|
|
|
{0, []int{100, 200, 300, 400, 500}, []int{100, 200, 300, 400, 500}},
|
|
|
|
|
{0, []string{"a", "b", "c", "d", "e"}, []string{"a", "b", "c", "d", "e"}},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{int64(-1), []int{100, 200, 300}, false},
|
|
|
|
|
{"noint", []int{100, 200, 300}, false},
|
2018-07-01 14:34:02 -04:00
|
|
|
|
{2, []string{}, []string{}},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{1, nil, false},
|
|
|
|
|
{nil, []int{100}, false},
|
|
|
|
|
{1, t, false},
|
|
|
|
|
{1, (*string)(nil), false},
|
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
result, err := ns.After(test.index, test.seq)
|
|
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
|
|
|
|
c.Assert(result, qt.DeepEquals, test.expect, errMsg)
|
2018-09-08 07:00:36 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-28 05:10:27 -04:00
|
|
|
|
type tstGrouper struct{}
|
2018-09-08 07:00:36 -04:00
|
|
|
|
|
|
|
|
|
type tstGroupers []*tstGrouper
|
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
|
func (g tstGrouper) Group(key any, items any) (any, error) {
|
2018-09-08 07:00:36 -04:00
|
|
|
|
ilen := reflect.ValueOf(items).Len()
|
|
|
|
|
return fmt.Sprintf("%v(%d)", key, ilen), nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-28 05:10:27 -04:00
|
|
|
|
type tstGrouper2 struct{}
|
2018-09-08 15:56:36 -04:00
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
|
func (g *tstGrouper2) Group(key any, items any) (any, error) {
|
2018-09-08 15:56:36 -04:00
|
|
|
|
ilen := reflect.ValueOf(items).Len()
|
|
|
|
|
return fmt.Sprintf("%v(%d)", key, ilen), nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-08 07:00:36 -04:00
|
|
|
|
func TestGroup(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2018-09-08 07:00:36 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
key any
|
|
|
|
|
items any
|
|
|
|
|
expect any
|
2018-09-08 07:00:36 -04:00
|
|
|
|
}{
|
2018-10-23 17:18:24 -04:00
|
|
|
|
{"a", []*tstGrouper{{}, {}}, "a(2)"},
|
2018-09-08 07:00:36 -04:00
|
|
|
|
{"b", tstGroupers{&tstGrouper{}, &tstGrouper{}}, "b(2)"},
|
2018-10-23 17:18:24 -04:00
|
|
|
|
{"a", []tstGrouper{{}, {}}, "a(2)"},
|
|
|
|
|
{"a", []*tstGrouper2{{}, {}}, "a(2)"},
|
|
|
|
|
{"b", []tstGrouper2{{}, {}}, "b(2)"},
|
2018-09-08 07:00:36 -04:00
|
|
|
|
{"a", []*tstGrouper{}, "a(0)"},
|
|
|
|
|
{"a", []string{"a", "b"}, false},
|
2018-09-09 04:15:11 -04:00
|
|
|
|
{"a", "asdf", false},
|
|
|
|
|
{"a", nil, false},
|
2018-10-23 17:18:24 -04:00
|
|
|
|
{nil, []*tstGrouper{{}, {}}, false},
|
2018-09-08 07:00:36 -04:00
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2018-09-08 07:00:36 -04:00
|
|
|
|
|
|
|
|
|
result, err := ns.Group(test.key, test.items)
|
|
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
2018-09-08 07:00:36 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
|
|
|
|
c.Assert(result, qt.Equals, test.expect, errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDelimit(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
seq any
|
|
|
|
|
delimiter any
|
|
|
|
|
last any
|
2023-10-28 05:10:27 -04:00
|
|
|
|
expect string
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}{
|
|
|
|
|
{[]string{"class1", "class2", "class3"}, " ", nil, "class1 class2 class3"},
|
|
|
|
|
{[]int{1, 2, 3, 4, 5}, ",", nil, "1,2,3,4,5"},
|
|
|
|
|
{[]int{1, 2, 3, 4, 5}, ", ", nil, "1, 2, 3, 4, 5"},
|
|
|
|
|
{[]string{"class1", "class2", "class3"}, " ", " and ", "class1 class2 and class3"},
|
|
|
|
|
{[]int{1, 2, 3, 4, 5}, ",", ",", "1,2,3,4,5"},
|
|
|
|
|
{[]int{1, 2, 3, 4, 5}, ", ", ", and ", "1, 2, 3, 4, and 5"},
|
|
|
|
|
// test maps with and without sorting required
|
|
|
|
|
{map[string]int{"1": 10, "2": 20, "3": 30, "4": 40, "5": 50}, "--", nil, "10--20--30--40--50"},
|
|
|
|
|
{map[string]int{"3": 10, "2": 20, "1": 30, "4": 40, "5": 50}, "--", nil, "30--20--10--40--50"},
|
|
|
|
|
{map[string]string{"1": "10", "2": "20", "3": "30", "4": "40", "5": "50"}, "--", nil, "10--20--30--40--50"},
|
|
|
|
|
{map[string]string{"3": "10", "2": "20", "1": "30", "4": "40", "5": "50"}, "--", nil, "30--20--10--40--50"},
|
|
|
|
|
{map[string]string{"one": "10", "two": "20", "three": "30", "four": "40", "five": "50"}, "--", nil, "50--40--10--30--20"},
|
|
|
|
|
{map[int]string{1: "10", 2: "20", 3: "30", 4: "40", 5: "50"}, "--", nil, "10--20--30--40--50"},
|
|
|
|
|
{map[int]string{3: "10", 2: "20", 1: "30", 4: "40", 5: "50"}, "--", nil, "30--20--10--40--50"},
|
|
|
|
|
{map[float64]string{3.3: "10", 2.3: "20", 1.3: "30", 4.3: "40", 5.3: "50"}, "--", nil, "30--20--10--40--50"},
|
|
|
|
|
// test maps with a last delimiter
|
|
|
|
|
{map[string]int{"1": 10, "2": 20, "3": 30, "4": 40, "5": 50}, "--", "--and--", "10--20--30--40--and--50"},
|
|
|
|
|
{map[string]int{"3": 10, "2": 20, "1": 30, "4": 40, "5": 50}, "--", "--and--", "30--20--10--40--and--50"},
|
|
|
|
|
{map[string]string{"1": "10", "2": "20", "3": "30", "4": "40", "5": "50"}, "--", "--and--", "10--20--30--40--and--50"},
|
|
|
|
|
{map[string]string{"3": "10", "2": "20", "1": "30", "4": "40", "5": "50"}, "--", "--and--", "30--20--10--40--and--50"},
|
|
|
|
|
{map[string]string{"one": "10", "two": "20", "three": "30", "four": "40", "five": "50"}, "--", "--and--", "50--40--10--30--and--20"},
|
|
|
|
|
{map[int]string{1: "10", 2: "20", 3: "30", 4: "40", 5: "50"}, "--", "--and--", "10--20--30--40--and--50"},
|
|
|
|
|
{map[int]string{3: "10", 2: "20", 1: "30", 4: "40", 5: "50"}, "--", "--and--", "30--20--10--40--and--50"},
|
|
|
|
|
{map[float64]string{3.5: "10", 2.5: "20", 1.5: "30", 4.5: "40", 5.5: "50"}, "--", "--and--", "30--20--10--40--and--50"},
|
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2023-10-28 05:10:27 -04:00
|
|
|
|
var result string
|
2017-03-13 18:55:02 -04:00
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if test.last == nil {
|
2023-07-11 03:48:57 -04:00
|
|
|
|
result, err = ns.Delimit(context.Background(), test.seq, test.delimiter)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
} else {
|
2023-07-11 03:48:57 -04:00
|
|
|
|
result, err = ns.Delimit(context.Background(), test.seq, test.delimiter, test.last)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
|
|
|
|
c.Assert(result, qt.Equals, test.expect, errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDictionary(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
values []any
|
|
|
|
|
expect any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}{
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{"a", "b"}, map[string]any{"a": "b"}},
|
|
|
|
|
{[]any{[]string{"a", "b"}, "c"}, map[string]any{"a": map[string]any{"b": "c"}}},
|
2020-12-02 07:23:25 -05:00
|
|
|
|
{
|
2022-03-17 17:03:27 -04:00
|
|
|
|
[]any{[]string{"a", "b"}, "c", []string{"a", "b2"}, "c2", "b", "c"},
|
|
|
|
|
map[string]any{"a": map[string]any{"b": "c", "b2": "c2"}, "b": "c"},
|
2020-12-02 07:23:25 -05:00
|
|
|
|
},
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{"a", 12, "b", []int{4}}, map[string]any{"a": 12, "b": []int{4}}},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
// errors
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{5, "b"}, false},
|
|
|
|
|
{[]any{"a", "b", "c"}, false},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
} {
|
2019-11-11 08:37:37 -05:00
|
|
|
|
i := i
|
|
|
|
|
test := test
|
|
|
|
|
c.Run(fmt.Sprint(i), func(c *qt.C) {
|
|
|
|
|
c.Parallel()
|
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test.values)
|
|
|
|
|
|
|
|
|
|
result, err := ns.Dictionary(test.values...)
|
|
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
|
|
|
|
c.Assert(result, qt.DeepEquals, test.expect, qt.Commentf(fmt.Sprint(result)))
|
|
|
|
|
})
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 12:14:45 -05:00
|
|
|
|
func TestReverse(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2019-11-11 12:14:45 -05:00
|
|
|
|
|
|
|
|
|
s := []string{"a", "b", "c"}
|
|
|
|
|
reversed, err := ns.Reverse(s)
|
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
c.Assert(reversed, qt.DeepEquals, []string{"c", "b", "a"}, qt.Commentf(fmt.Sprint(reversed)))
|
|
|
|
|
c.Assert(s, qt.DeepEquals, []string{"a", "b", "c"})
|
|
|
|
|
|
|
|
|
|
reversed, err = ns.Reverse(nil)
|
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
c.Assert(reversed, qt.IsNil)
|
|
|
|
|
_, err = ns.Reverse(43)
|
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
|
func TestEchoParam(t *testing.T) {
|
2024-08-06 13:13:14 -04:00
|
|
|
|
t.Skip("deprecated, will be removed in Hugo 0.133.0")
|
2017-03-13 18:55:02 -04:00
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
a any
|
|
|
|
|
key any
|
|
|
|
|
expect any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}{
|
|
|
|
|
{[]int{1, 2, 3}, 1, int64(2)},
|
|
|
|
|
{[]uint{1, 2, 3}, 1, uint64(2)},
|
|
|
|
|
{[]float64{1.1, 2.2, 3.3}, 1, float64(2.2)},
|
|
|
|
|
{[]string{"foo", "bar", "baz"}, 1, "bar"},
|
|
|
|
|
{[]TstX{{A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"}}, 1, ""},
|
|
|
|
|
{map[string]int{"foo": 1, "bar": 2, "baz": 3}, "bar", int64(2)},
|
|
|
|
|
{map[string]uint{"foo": 1, "bar": 2, "baz": 3}, "bar", uint64(2)},
|
|
|
|
|
{map[string]float64{"foo": 1.1, "bar": 2.2, "baz": 3.3}, "bar", float64(2.2)},
|
|
|
|
|
{map[string]string{"foo": "FOO", "bar": "BAR", "baz": "BAZ"}, "bar", "BAR"},
|
|
|
|
|
{map[string]TstX{"foo": {A: "a", B: "b"}, "bar": {A: "c", B: "d"}, "baz": {A: "e", B: "f"}}, "bar", ""},
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{map[string]any{"foo": nil}, "foo", ""},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{(*[]string)(nil), "bar", ""},
|
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
result := ns.EchoParam(test.a, test.key)
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(result, qt.Equals, test.expect, errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestFirst(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
limit any
|
|
|
|
|
seq any
|
|
|
|
|
expect any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}{
|
|
|
|
|
{int(2), []string{"a", "b", "c"}, []string{"a", "b"}},
|
|
|
|
|
{int32(3), []string{"a", "b"}, []string{"a", "b"}},
|
|
|
|
|
{int64(2), []int{100, 200, 300}, []int{100, 200}},
|
|
|
|
|
{100, []int{100, 200}, []int{100, 200}},
|
|
|
|
|
{"1", []int{100, 200, 300}, []int{100}},
|
2018-09-22 14:58:46 -04:00
|
|
|
|
{0, []string{"h", "u", "g", "o"}, []string{}},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{int64(-1), []int{100, 200, 300}, false},
|
|
|
|
|
{"noint", []int{100, 200, 300}, false},
|
|
|
|
|
{1, nil, false},
|
|
|
|
|
{nil, []int{100}, false},
|
|
|
|
|
{1, t, false},
|
|
|
|
|
{1, (*string)(nil), false},
|
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
result, err := ns.First(test.limit, test.seq)
|
|
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
c.Assert(result, qt.DeepEquals, test.expect, errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIn(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
l1 any
|
|
|
|
|
l2 any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
expect bool
|
|
|
|
|
}{
|
|
|
|
|
{[]string{"a", "b", "c"}, "b", true},
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{"a", "b", "c"}, "b", true},
|
|
|
|
|
{[]any{"a", "b", "c"}, "d", false},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{[]string{"a", "b", "c"}, "d", false},
|
|
|
|
|
{[]string{"a", "12", "c"}, 12, false},
|
|
|
|
|
{[]string{"a", "b", "c"}, nil, false},
|
|
|
|
|
{[]int{1, 2, 4}, 2, true},
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{1, 2, 4}, 2, true},
|
|
|
|
|
{[]any{1, 2, 4}, nil, false},
|
|
|
|
|
{[]any{nil}, nil, false},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{[]int{1, 2, 4}, 3, false},
|
|
|
|
|
{[]float64{1.23, 2.45, 4.67}, 1.23, true},
|
|
|
|
|
{[]float64{1.234567, 2.45, 4.67}, 1.234568, false},
|
2017-07-03 04:08:55 -04:00
|
|
|
|
{[]float64{1, 2, 3}, 1, true},
|
|
|
|
|
{[]float32{1, 2, 3}, 1, true},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{"this substring should be found", "substring", true},
|
|
|
|
|
{"this substring should not be found", "subseastring", false},
|
|
|
|
|
{nil, "foo", false},
|
2019-04-18 11:06:54 -04:00
|
|
|
|
// Pointers
|
|
|
|
|
{pagesPtr{p1, p2, p3, p2}, p2, true},
|
|
|
|
|
{pagesPtr{p1, p2, p3, p2}, p4, false},
|
|
|
|
|
// Structs
|
|
|
|
|
{pagesVals{p3v, p2v, p3v, p2v}, p2v, true},
|
|
|
|
|
{pagesVals{p3v, p2v, p3v, p2v}, p4v, false},
|
2020-03-02 14:04:16 -05:00
|
|
|
|
// template.HTML
|
|
|
|
|
{template.HTML("this substring should be found"), "substring", true},
|
|
|
|
|
{template.HTML("this substring should not be found"), "subseastring", false},
|
2020-03-09 08:32:38 -04:00
|
|
|
|
// Uncomparable, use hashstructure
|
|
|
|
|
{[]string{"a", "b"}, []string{"a", "b"}, false},
|
|
|
|
|
{[][]string{{"a", "b"}}, []string{"a", "b"}, true},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
} {
|
2017-07-03 04:08:55 -04:00
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2019-04-19 02:58:12 -04:00
|
|
|
|
result, err := ns.In(test.l1, test.l2)
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
c.Assert(result, qt.Equals, test.expect, errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
|
type testPage struct {
|
2017-07-03 04:32:10 -04:00
|
|
|
|
Title string
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
|
func (p testPage) String() string {
|
2017-07-03 04:32:10 -04:00
|
|
|
|
return "p-" + p.Title
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 07:23:25 -05:00
|
|
|
|
type (
|
|
|
|
|
pagesPtr []*testPage
|
|
|
|
|
pagesVals []testPage
|
|
|
|
|
)
|
2017-07-03 04:32:10 -04:00
|
|
|
|
|
2019-04-18 04:27:23 -04:00
|
|
|
|
var (
|
|
|
|
|
p1 = &testPage{"A"}
|
|
|
|
|
p2 = &testPage{"B"}
|
|
|
|
|
p3 = &testPage{"C"}
|
|
|
|
|
p4 = &testPage{"D"}
|
|
|
|
|
|
|
|
|
|
p1v = testPage{"A"}
|
|
|
|
|
p2v = testPage{"B"}
|
|
|
|
|
p3v = testPage{"C"}
|
|
|
|
|
p4v = testPage{"D"}
|
|
|
|
|
)
|
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
|
func TestIntersect(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
l1, l2 any
|
|
|
|
|
expect any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}{
|
|
|
|
|
{[]string{"a", "b", "c", "c"}, []string{"a", "b", "b"}, []string{"a", "b"}},
|
|
|
|
|
{[]string{"a", "b"}, []string{"a", "b", "c"}, []string{"a", "b"}},
|
|
|
|
|
{[]string{"a", "b", "c"}, []string{"d", "e"}, []string{}},
|
|
|
|
|
{[]string{}, []string{}, []string{}},
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]string{"a", "b"}, nil, []any{}},
|
|
|
|
|
{nil, []string{"a", "b"}, []any{}},
|
|
|
|
|
{nil, nil, []any{}},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{[]string{"1", "2"}, []int{1, 2}, []string{}},
|
|
|
|
|
{[]int{1, 2}, []string{"1", "2"}, []int{}},
|
|
|
|
|
{[]int{1, 2, 4}, []int{2, 4}, []int{2, 4}},
|
|
|
|
|
{[]int{2, 4}, []int{1, 2, 4}, []int{2, 4}},
|
|
|
|
|
{[]int{1, 2, 4}, []int{3, 6}, []int{}},
|
|
|
|
|
{[]float64{2.2, 4.4}, []float64{1.1, 2.2, 4.4}, []float64{2.2, 4.4}},
|
2017-07-03 04:32:10 -04:00
|
|
|
|
|
2017-05-01 22:25:39 -04:00
|
|
|
|
// []interface{} ∩ []interface{}
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{"a", "b", "c"}, []any{"a", "b", "b"}, []any{"a", "b"}},
|
|
|
|
|
{[]any{1, 2, 3}, []any{1, 2, 2}, []any{1, 2}},
|
|
|
|
|
{[]any{int8(1), int8(2), int8(3)}, []any{int8(1), int8(2), int8(2)}, []any{int8(1), int8(2)}},
|
|
|
|
|
{[]any{int16(1), int16(2), int16(3)}, []any{int16(1), int16(2), int16(2)}, []any{int16(1), int16(2)}},
|
|
|
|
|
{[]any{int32(1), int32(2), int32(3)}, []any{int32(1), int32(2), int32(2)}, []any{int32(1), int32(2)}},
|
|
|
|
|
{[]any{int64(1), int64(2), int64(3)}, []any{int64(1), int64(2), int64(2)}, []any{int64(1), int64(2)}},
|
|
|
|
|
{[]any{float32(1), float32(2), float32(3)}, []any{float32(1), float32(2), float32(2)}, []any{float32(1), float32(2)}},
|
|
|
|
|
{[]any{float64(1), float64(2), float64(3)}, []any{float64(1), float64(2), float64(2)}, []any{float64(1), float64(2)}},
|
2017-05-01 22:25:39 -04:00
|
|
|
|
|
|
|
|
|
// []interface{} ∩ []T
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{"a", "b", "c"}, []string{"a", "b", "b"}, []any{"a", "b"}},
|
|
|
|
|
{[]any{1, 2, 3}, []int{1, 2, 2}, []any{1, 2}},
|
|
|
|
|
{[]any{int8(1), int8(2), int8(3)}, []int8{1, 2, 2}, []any{int8(1), int8(2)}},
|
|
|
|
|
{[]any{int16(1), int16(2), int16(3)}, []int16{1, 2, 2}, []any{int16(1), int16(2)}},
|
|
|
|
|
{[]any{int32(1), int32(2), int32(3)}, []int32{1, 2, 2}, []any{int32(1), int32(2)}},
|
|
|
|
|
{[]any{int64(1), int64(2), int64(3)}, []int64{1, 2, 2}, []any{int64(1), int64(2)}},
|
|
|
|
|
{[]any{uint(1), uint(2), uint(3)}, []uint{1, 2, 2}, []any{uint(1), uint(2)}},
|
|
|
|
|
{[]any{float32(1), float32(2), float32(3)}, []float32{1, 2, 2}, []any{float32(1), float32(2)}},
|
|
|
|
|
{[]any{float64(1), float64(2), float64(3)}, []float64{1, 2, 2}, []any{float64(1), float64(2)}},
|
2017-05-01 22:25:39 -04:00
|
|
|
|
|
|
|
|
|
// []T ∩ []interface{}
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]string{"a", "b", "c"}, []any{"a", "b", "b"}, []string{"a", "b"}},
|
|
|
|
|
{[]int{1, 2, 3}, []any{1, 2, 2}, []int{1, 2}},
|
|
|
|
|
{[]int8{1, 2, 3}, []any{int8(1), int8(2), int8(2)}, []int8{1, 2}},
|
|
|
|
|
{[]int16{1, 2, 3}, []any{int16(1), int16(2), int16(2)}, []int16{1, 2}},
|
|
|
|
|
{[]int32{1, 2, 3}, []any{int32(1), int32(2), int32(2)}, []int32{1, 2}},
|
|
|
|
|
{[]int64{1, 2, 3}, []any{int64(1), int64(2), int64(2)}, []int64{1, 2}},
|
|
|
|
|
{[]float32{1, 2, 3}, []any{float32(1), float32(2), float32(2)}, []float32{1, 2}},
|
|
|
|
|
{[]float64{1, 2, 3}, []any{float64(1), float64(2), float64(2)}, []float64{1, 2}},
|
2017-07-03 04:32:10 -04:00
|
|
|
|
|
|
|
|
|
// Structs
|
|
|
|
|
{pagesPtr{p1, p4, p2, p3}, pagesPtr{p4, p2, p2}, pagesPtr{p4, p2}},
|
|
|
|
|
{pagesVals{p1v, p4v, p2v, p3v}, pagesVals{p1v, p3v, p3v}, pagesVals{p1v, p3v}},
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{p1, p4, p2, p3}, []any{p4, p2, p2}, []any{p4, p2}},
|
|
|
|
|
{[]any{p1v, p4v, p2v, p3v}, []any{p1v, p3v, p3v}, []any{p1v, p3v}},
|
2017-07-08 04:34:42 -04:00
|
|
|
|
{pagesPtr{p1, p4, p2, p3}, pagesPtr{}, pagesPtr{}},
|
|
|
|
|
{pagesVals{}, pagesVals{p1v, p3v, p3v}, pagesVals{}},
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{p1, p4, p2, p3}, []any{}, []any{}},
|
|
|
|
|
{[]any{}, []any{p1v, p3v, p3v}, []any{}},
|
2018-09-10 15:16:05 -04:00
|
|
|
|
|
|
|
|
|
// errors
|
|
|
|
|
{"not array or slice", []string{"a"}, false},
|
|
|
|
|
{[]string{"a"}, "not array or slice", false},
|
|
|
|
|
|
|
|
|
|
// uncomparable types - #3820
|
|
|
|
|
{[]map[int]int{{1: 1}, {2: 2}}, []map[int]int{{2: 2}, {3: 3}}, false},
|
|
|
|
|
{[][]int{{1, 1}, {1, 2}}, [][]int{{1, 2}, {1, 2}, {1, 3}}, false},
|
|
|
|
|
{[]int{1, 1}, [][]int{{1, 2}, {1, 2}, {1, 3}}, false},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
} {
|
2017-07-03 04:32:10 -04:00
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
result, err := ns.Intersect(test.l1, test.l2)
|
|
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
2017-07-03 04:32:10 -04:00
|
|
|
|
if !reflect.DeepEqual(result, test.expect) {
|
|
|
|
|
t.Fatalf("[%d] Got\n%v expected\n%v", i, result, test.expect)
|
|
|
|
|
}
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIsSet(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
a any
|
|
|
|
|
key any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
expect bool
|
|
|
|
|
isErr bool
|
|
|
|
|
}{
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{1, 2, 3, 5}, 2, true, false},
|
|
|
|
|
{[]any{1, 2, 3, 5}, "2", true, false},
|
|
|
|
|
{[]any{1, 2, 3, 5}, 2.0, true, false},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{1, 2, 3, 5}, 22, false, false},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{map[string]any{"a": 1, "b": 2}, "b", true, false},
|
|
|
|
|
{map[string]any{"a": 1, "b": 2}, "bc", false, false},
|
2018-10-02 21:29:20 -04:00
|
|
|
|
|
|
|
|
|
{time.Now(), "Day", false, false},
|
|
|
|
|
{nil, "nil", false, false},
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{1, 2, 3, 5}, TstX{}, false, true},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
result, err := ns.IsSet(test.a, test.key)
|
|
|
|
|
if test.isErr {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
|
|
|
|
c.Assert(result, qt.Equals, test.expect, errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestLast(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
limit any
|
|
|
|
|
seq any
|
|
|
|
|
expect any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}{
|
|
|
|
|
{int(2), []string{"a", "b", "c"}, []string{"b", "c"}},
|
|
|
|
|
{int32(3), []string{"a", "b"}, []string{"a", "b"}},
|
|
|
|
|
{int64(2), []int{100, 200, 300}, []int{200, 300}},
|
|
|
|
|
{100, []int{100, 200}, []int{100, 200}},
|
|
|
|
|
{"1", []int{100, 200, 300}, []int{300}},
|
2019-10-11 14:15:39 -04:00
|
|
|
|
{"0", []int{100, 200, 300}, []int{}},
|
|
|
|
|
{"0", []string{"a", "b", "c"}, []string{}},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
// errors
|
|
|
|
|
{int64(-1), []int{100, 200, 300}, false},
|
|
|
|
|
{"noint", []int{100, 200, 300}, false},
|
|
|
|
|
{1, nil, false},
|
|
|
|
|
{nil, []int{100}, false},
|
|
|
|
|
{1, t, false},
|
|
|
|
|
{1, (*string)(nil), false},
|
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
result, err := ns.Last(test.limit, test.seq)
|
|
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
|
|
|
|
c.Assert(result, qt.DeepEquals, test.expect, errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestQuerify(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
params []any
|
|
|
|
|
expect any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}{
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{"a", "b"}, "a=b"},
|
|
|
|
|
{[]any{"a", "b", "c", "d", "f", " &"}, `a=b&c=d&f=+%26`},
|
|
|
|
|
{[]any{[]string{"a", "b"}}, "a=b"},
|
|
|
|
|
{[]any{[]string{"a", "b", "c", "d", "f", " &"}}, `a=b&c=d&f=+%26`},
|
|
|
|
|
{[]any{[]any{"x", "y"}}, `x=y`},
|
|
|
|
|
{[]any{[]any{"x", 5}}, `x=5`},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
// errors
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{5, "b"}, false},
|
|
|
|
|
{[]any{"a", "b", "c"}, false},
|
|
|
|
|
{[]any{[]string{"a", "b", "c"}}, false},
|
|
|
|
|
{[]any{[]string{"a", "b"}, "c"}, false},
|
|
|
|
|
{[]any{[]any{"c", "d", "e"}}, false},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test.params)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
result, err := ns.Querify(test.params...)
|
|
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
|
|
|
|
c.Assert(result, qt.Equals, test.expect, errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-06 13:38:10 -05:00
|
|
|
|
func BenchmarkQuerify(b *testing.B) {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2022-03-17 17:03:27 -04:00
|
|
|
|
params := []any{"a", "b", "c", "d", "f", " &"}
|
2021-03-06 13:38:10 -05:00
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_, err := ns.Querify(params...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkQuerifySlice(b *testing.B) {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2021-03-06 13:38:10 -05:00
|
|
|
|
params := []string{"a", "b", "c", "d", "f", " &"}
|
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
_, err := ns.Querify(params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
|
func TestSeq(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
args []any
|
|
|
|
|
expect any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}{
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{-2, 5}, []int{-2, -1, 0, 1, 2, 3, 4, 5}},
|
|
|
|
|
{[]any{1, 2, 4}, []int{1, 3}},
|
|
|
|
|
{[]any{1}, []int{1}},
|
|
|
|
|
{[]any{3}, []int{1, 2, 3}},
|
|
|
|
|
{[]any{3.2}, []int{1, 2, 3}},
|
|
|
|
|
{[]any{0}, []int{}},
|
|
|
|
|
{[]any{-1}, []int{-1}},
|
|
|
|
|
{[]any{-3}, []int{-1, -2, -3}},
|
|
|
|
|
{[]any{3, -2}, []int{3, 2, 1, 0, -1, -2}},
|
|
|
|
|
{[]any{6, -2, 2}, []int{6, 4, 2}},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
// errors
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{1, 0, 2}, false},
|
|
|
|
|
{[]any{1, -1, 2}, false},
|
|
|
|
|
{[]any{2, 1, 1}, false},
|
|
|
|
|
{[]any{2, 1, 1, 1}, false},
|
|
|
|
|
{[]any{2001}, false},
|
|
|
|
|
{[]any{}, false},
|
|
|
|
|
{[]any{0, -1000000}, false},
|
|
|
|
|
{[]any{tstNoStringer{}}, false},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{nil, false},
|
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
result, err := ns.Seq(test.args...)
|
|
|
|
|
|
|
|
|
|
if b, ok := test.expect.(bool); ok && !b {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
|
|
|
|
c.Assert(result, qt.DeepEquals, test.expect, errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestShuffle(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
seq any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
success bool
|
|
|
|
|
}{
|
|
|
|
|
{[]string{"a", "b", "c", "d"}, true},
|
|
|
|
|
{[]int{100, 200, 300}, true},
|
|
|
|
|
{[]int{100, 200, 300}, true},
|
|
|
|
|
{[]int{100, 200}, true},
|
|
|
|
|
{[]string{"a", "b"}, true},
|
|
|
|
|
{[]int{100, 200, 300}, true},
|
|
|
|
|
{[]int{100, 200, 300}, true},
|
|
|
|
|
{[]int{100}, true},
|
|
|
|
|
// errors
|
|
|
|
|
{nil, false},
|
|
|
|
|
{t, false},
|
|
|
|
|
{(*string)(nil), false},
|
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
result, err := ns.Shuffle(test.seq)
|
|
|
|
|
|
|
|
|
|
if !test.success {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
resultv := reflect.ValueOf(result)
|
|
|
|
|
seqv := reflect.ValueOf(test.seq)
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(seqv.Len(), qt.Equals, resultv.Len(), errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestShuffleRandomising(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
// Note that this test can fail with false negative result if the shuffle
|
|
|
|
|
// of the sequence happens to be the same as the original sequence. However
|
2020-12-16 06:11:32 -05:00
|
|
|
|
// the probability of the event is 10^-158 which is negligible.
|
2017-03-13 18:55:02 -04:00
|
|
|
|
seqLen := 100
|
|
|
|
|
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
|
seq []int
|
|
|
|
|
}{
|
|
|
|
|
{rand.Perm(seqLen)},
|
|
|
|
|
} {
|
|
|
|
|
result, err := ns.Shuffle(test.seq)
|
|
|
|
|
resultv := reflect.ValueOf(result)
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
allSame := true
|
|
|
|
|
for i, v := range test.seq {
|
|
|
|
|
allSame = allSame && (resultv.Index(i).Interface() == v)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(allSame, qt.Equals, false)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-08 04:25:15 -04:00
|
|
|
|
// Also see tests in commons/collection.
|
2017-03-13 18:55:02 -04:00
|
|
|
|
func TestSlice(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
args []any
|
|
|
|
|
expected any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}{
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{"a", "b"}, []string{"a", "b"}},
|
|
|
|
|
{[]any{}, []any{}},
|
|
|
|
|
{[]any{nil}, []any{nil}},
|
|
|
|
|
{[]any{5, "b"}, []any{5, "b"}},
|
|
|
|
|
{[]any{tstNoStringer{}}, []tstNoStringer{{}}},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test.args)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
result := ns.Slice(test.args...)
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(result, qt.DeepEquals, test.expected, errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUnion(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
l1 any
|
|
|
|
|
l2 any
|
|
|
|
|
expect any
|
2017-03-13 18:55:02 -04:00
|
|
|
|
isErr bool
|
|
|
|
|
}{
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{nil, nil, []any{}, false},
|
2017-05-20 12:54:13 -04:00
|
|
|
|
{nil, []string{"a", "b"}, []string{"a", "b"}, false},
|
|
|
|
|
{[]string{"a", "b"}, nil, []string{"a", "b"}, false},
|
|
|
|
|
|
|
|
|
|
// []A ∪ []B
|
|
|
|
|
{[]string{"1", "2"}, []int{3}, []string{}, false},
|
|
|
|
|
{[]int{1, 2}, []string{"1", "2"}, []int{}, false},
|
|
|
|
|
|
|
|
|
|
// []T ∪ []T
|
2017-03-13 18:55:02 -04:00
|
|
|
|
{[]string{"a", "b", "c", "c"}, []string{"a", "b", "b"}, []string{"a", "b", "c"}, false},
|
|
|
|
|
{[]string{"a", "b"}, []string{"a", "b", "c"}, []string{"a", "b", "c"}, false},
|
|
|
|
|
{[]string{"a", "b", "c"}, []string{"d", "e"}, []string{"a", "b", "c", "d", "e"}, false},
|
|
|
|
|
{[]string{}, []string{}, []string{}, false},
|
|
|
|
|
{[]int{1, 2, 3}, []int{3, 4, 5}, []int{1, 2, 3, 4, 5}, false},
|
|
|
|
|
{[]int{1, 2, 3}, []int{1, 2, 3}, []int{1, 2, 3}, false},
|
|
|
|
|
{[]int{1, 2, 4}, []int{2, 4}, []int{1, 2, 4}, false},
|
|
|
|
|
{[]int{2, 4}, []int{1, 2, 4}, []int{2, 4, 1}, false},
|
|
|
|
|
{[]int{1, 2, 4}, []int{3, 6}, []int{1, 2, 4, 3, 6}, false},
|
|
|
|
|
{[]float64{2.2, 4.4}, []float64{1.1, 2.2, 4.4}, []float64{2.2, 4.4, 1.1}, false},
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{"a", "b", "c", "c"}, []any{"a", "b", "b"}, []any{"a", "b", "c"}, false},
|
2017-05-20 12:54:13 -04:00
|
|
|
|
|
|
|
|
|
// []T ∪ []interface{}
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]string{"1", "2"}, []any{"9"}, []string{"1", "2", "9"}, false},
|
|
|
|
|
{[]int{2, 4}, []any{1, 2, 4}, []int{2, 4, 1}, false},
|
|
|
|
|
{[]int8{2, 4}, []any{int8(1), int8(2), int8(4)}, []int8{2, 4, 1}, false},
|
|
|
|
|
{[]int8{2, 4}, []any{1, 2, 4}, []int8{2, 4, 1}, false},
|
|
|
|
|
{[]int16{2, 4}, []any{1, 2, 4}, []int16{2, 4, 1}, false},
|
|
|
|
|
{[]int32{2, 4}, []any{1, 2, 4}, []int32{2, 4, 1}, false},
|
|
|
|
|
{[]int64{2, 4}, []any{1, 2, 4}, []int64{2, 4, 1}, false},
|
2017-07-03 04:32:10 -04:00
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]float64{2.2, 4.4}, []any{1.1, 2.2, 4.4}, []float64{2.2, 4.4, 1.1}, false},
|
|
|
|
|
{[]float32{2.2, 4.4}, []any{1.1, 2.2, 4.4}, []float32{2.2, 4.4, 1.1}, false},
|
2017-05-20 12:54:13 -04:00
|
|
|
|
|
|
|
|
|
// []interface{} ∪ []T
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{"a", "b", "c", "c"}, []string{"a", "b", "d"}, []any{"a", "b", "c", "d"}, false},
|
|
|
|
|
{[]any{}, []string{}, []any{}, false},
|
|
|
|
|
{[]any{1, 2}, []int{2, 3}, []any{1, 2, 3}, false},
|
|
|
|
|
{[]any{1, 2}, []int8{2, 3}, []any{1, 2, 3}, false}, // 28
|
|
|
|
|
{[]any{uint(1), uint(2)}, []uint{2, 3}, []any{uint(1), uint(2), uint(3)}, false},
|
|
|
|
|
{[]any{1.1, 2.2}, []float64{2.2, 3.3}, []any{1.1, 2.2, 3.3}, false},
|
2017-05-20 12:54:13 -04:00
|
|
|
|
|
2017-07-03 04:32:10 -04:00
|
|
|
|
// Structs
|
|
|
|
|
{pagesPtr{p1, p4}, pagesPtr{p4, p2, p2}, pagesPtr{p1, p4, p2}, false},
|
|
|
|
|
{pagesVals{p1v}, pagesVals{p3v, p3v}, pagesVals{p1v, p3v}, false},
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{p1, p4}, []any{p4, p2, p2}, []any{p1, p4, p2}, false},
|
|
|
|
|
{[]any{p1v}, []any{p3v, p3v}, []any{p1v, p3v}, false},
|
2017-07-08 04:31:09 -04:00
|
|
|
|
// #3686
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{[]any{p1v}, []any{}, []any{p1v}, false},
|
|
|
|
|
{[]any{}, []any{p1v}, []any{p1v}, false},
|
2017-07-08 04:31:09 -04:00
|
|
|
|
{pagesPtr{p1}, pagesPtr{}, pagesPtr{p1}, false},
|
|
|
|
|
{pagesVals{p1v}, pagesVals{}, pagesVals{p1v}, false},
|
|
|
|
|
{pagesPtr{}, pagesPtr{p1}, pagesPtr{p1}, false},
|
|
|
|
|
{pagesVals{}, pagesVals{p1v}, pagesVals{p1v}, false},
|
2017-07-03 04:32:10 -04:00
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
|
// errors
|
|
|
|
|
{"not array or slice", []string{"a"}, false, true},
|
|
|
|
|
{[]string{"a"}, "not array or slice", false, true},
|
2018-09-10 15:16:05 -04:00
|
|
|
|
|
|
|
|
|
// uncomparable types - #3820
|
|
|
|
|
{[]map[string]int{{"K1": 1}}, []map[string]int{{"K2": 2}, {"K2": 2}}, false, true},
|
|
|
|
|
{[][]int{{1, 1}, {1, 2}}, [][]int{{2, 1}, {2, 2}}, false, true},
|
2017-03-13 18:55:02 -04:00
|
|
|
|
} {
|
2017-07-03 04:32:10 -04:00
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
|
|
result, err := ns.Union(test.l1, test.l2)
|
|
|
|
|
if test.isErr {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
2017-07-03 04:32:10 -04:00
|
|
|
|
if !reflect.DeepEqual(result, test.expect) {
|
|
|
|
|
t.Fatalf("[%d] Got\n%v expected\n%v", i, result, test.expect)
|
|
|
|
|
}
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-29 15:03:38 -04:00
|
|
|
|
func TestUniq(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2023-01-04 12:24:36 -05:00
|
|
|
|
ns := newNs()
|
2017-05-29 15:03:38 -04:00
|
|
|
|
for i, test := range []struct {
|
2022-03-17 17:03:27 -04:00
|
|
|
|
l any
|
|
|
|
|
expect any
|
2017-05-29 15:03:38 -04:00
|
|
|
|
isErr bool
|
|
|
|
|
}{
|
|
|
|
|
{[]string{"a", "b", "c"}, []string{"a", "b", "c"}, false},
|
|
|
|
|
{[]string{"a", "b", "c", "c"}, []string{"a", "b", "c"}, false},
|
|
|
|
|
{[]string{"a", "b", "b", "c"}, []string{"a", "b", "c"}, false},
|
|
|
|
|
{[]string{"a", "b", "c", "b"}, []string{"a", "b", "c"}, false},
|
|
|
|
|
{[]int{1, 2, 3}, []int{1, 2, 3}, false},
|
|
|
|
|
{[]int{1, 2, 3, 3}, []int{1, 2, 3}, false},
|
|
|
|
|
{[]int{1, 2, 2, 3}, []int{1, 2, 3}, false},
|
|
|
|
|
{[]int{1, 2, 3, 2}, []int{1, 2, 3}, false},
|
|
|
|
|
{[4]int{1, 2, 3, 2}, []int{1, 2, 3}, false},
|
2022-03-17 17:03:27 -04:00
|
|
|
|
{nil, make([]any, 0), false},
|
2019-04-18 04:27:23 -04:00
|
|
|
|
// Pointers
|
|
|
|
|
{pagesPtr{p1, p2, p3, p2}, pagesPtr{p1, p2, p3}, false},
|
|
|
|
|
{pagesPtr{}, pagesPtr{}, false},
|
|
|
|
|
// Structs
|
|
|
|
|
{pagesVals{p3v, p2v, p3v, p2v}, pagesVals{p3v, p2v}, false},
|
|
|
|
|
|
2020-12-16 06:11:32 -05:00
|
|
|
|
// not Comparable(), use hashstructure
|
2020-03-09 08:32:38 -04:00
|
|
|
|
{[]map[string]int{
|
|
|
|
|
{"K1": 1}, {"K2": 2}, {"K1": 1}, {"K2": 1},
|
|
|
|
|
}, []map[string]int{
|
|
|
|
|
{"K1": 1}, {"K2": 2}, {"K2": 1},
|
|
|
|
|
}, false},
|
|
|
|
|
|
2019-04-18 04:27:23 -04:00
|
|
|
|
// should fail
|
2017-05-29 15:03:38 -04:00
|
|
|
|
{1, 1, true},
|
|
|
|
|
{"foo", "fo", true},
|
|
|
|
|
} {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
errMsg := qt.Commentf("[%d] %v", i, test)
|
2017-05-29 15:03:38 -04:00
|
|
|
|
|
|
|
|
|
result, err := ns.Uniq(test.l)
|
|
|
|
|
if test.isErr {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil), errMsg)
|
2017-05-29 15:03:38 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil, errMsg)
|
|
|
|
|
c.Assert(result, qt.DeepEquals, test.expect, errMsg)
|
2017-05-29 15:03:38 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
|
func (x *TstX) TstRp() string {
|
|
|
|
|
return "r" + x.A
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (x TstX) TstRv() string {
|
|
|
|
|
return "r" + x.B
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 16:57:57 -04:00
|
|
|
|
func (x TstX) TstRv2() string {
|
|
|
|
|
return "r" + x.B
|
|
|
|
|
}
|
|
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
|
//lint:ignore U1000 reflect test
|
2017-03-13 18:55:02 -04:00
|
|
|
|
func (x TstX) unexportedMethod() string {
|
|
|
|
|
return x.unexported
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (x TstX) MethodWithArg(s string) string {
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (x TstX) MethodReturnNothing() {}
|
|
|
|
|
|
|
|
|
|
func (x TstX) MethodReturnErrorOnly() error {
|
|
|
|
|
return errors.New("some error occurred")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (x TstX) MethodReturnTwoValues() (string, string) {
|
|
|
|
|
return "foo", "bar"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (x TstX) MethodReturnValueWithError() (string, error) {
|
|
|
|
|
return "", errors.New("some error occurred")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (x TstX) String() string {
|
|
|
|
|
return fmt.Sprintf("A: %s, B: %s", x.A, x.B)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type TstX struct {
|
|
|
|
|
A, B string
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
|
unexported string //lint:ignore U1000 reflect test
|
2017-03-13 18:55:02 -04:00
|
|
|
|
}
|
2017-05-17 22:29:35 -04:00
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
|
type TstParams struct {
|
|
|
|
|
params maps.Params
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (x TstParams) Params() maps.Params {
|
|
|
|
|
return x.params
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 16:57:57 -04:00
|
|
|
|
type TstXIHolder struct {
|
|
|
|
|
XI TstXI
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Partially implemented by the TstX struct.
|
|
|
|
|
type TstXI interface {
|
|
|
|
|
TstRv2() string
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
|
func ToTstXIs(slice any) []TstXI {
|
2019-05-27 16:57:57 -04:00
|
|
|
|
s := reflect.ValueOf(slice)
|
|
|
|
|
if s.Kind() != reflect.Slice {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
tis := make([]TstXI, s.Len())
|
|
|
|
|
|
|
|
|
|
for i := 0; i < s.Len(); i++ {
|
|
|
|
|
tsti, ok := s.Index(i).Interface().(TstXI)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
tis[i] = tsti
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tis
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
|
func newNs() *Namespace {
|
|
|
|
|
return New(testconfig.GetTestDeps(nil, nil))
|
2018-03-21 12:21:46 -04:00
|
|
|
|
}
|