| 1 |
// Copyright 2024 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 collections |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
qt "github.com/frankban/quicktest" |
| 20 |
"github.com/gohugoio/hugo/common/hmaps" |
| 21 |
) |
| 22 |
|
| 23 |
func TestQuerify(t *testing.T) { |
| 24 |
t.Parallel() |
| 25 |
c := qt.New(t) |
| 26 |
ns := newNs() |
| 27 |
|
| 28 |
for _, test := range []struct { |
| 29 |
name string |
| 30 |
params []any |
| 31 |
expect any |
| 32 |
}{ |
| 33 |
// map |
| 34 |
{"01", []any{hmaps.Params{"a": "foo", "b": "bar"}}, `a=foo&b=bar`}, |
| 35 |
{"02", []any{hmaps.Params{"a": 6, "b": 7}}, `a=6&b=7`}, |
| 36 |
{"03", []any{hmaps.Params{"a": "foo", "b": 7}}, `a=foo&b=7`}, |
| 37 |
{"04", []any{map[string]any{"a": "foo", "b": "bar"}}, `a=foo&b=bar`}, |
| 38 |
{"05", []any{map[string]any{"a": 6, "b": 7}}, `a=6&b=7`}, |
| 39 |
{"06", []any{map[string]any{"a": "foo", "b": 7}}, `a=foo&b=7`}, |
| 40 |
// slice |
| 41 |
{"07", []any{[]string{"a", "foo", "b", "bar"}}, `a=foo&b=bar`}, |
| 42 |
{"08", []any{[]any{"a", 6, "b", 7}}, `a=6&b=7`}, |
| 43 |
{"09", []any{[]any{"a", "foo", "b", 7}}, `a=foo&b=7`}, |
| 44 |
// sequence of scalar values |
| 45 |
{"10", []any{"a", "foo", "b", "bar"}, `a=foo&b=bar`}, |
| 46 |
{"11", []any{"a", 6, "b", 7}, `a=6&b=7`}, |
| 47 |
{"12", []any{"a", "foo", "b", 7}, `a=foo&b=7`}, |
| 48 |
// empty map |
| 49 |
{"13", []any{map[string]any{}}, ``}, |
| 50 |
// empty slice |
| 51 |
{"14", []any{[]string{}}, ``}, |
| 52 |
{"15", []any{[]any{}}, ``}, |
| 53 |
// no arguments |
| 54 |
{"16", []any{}, ``}, |
| 55 |
// errors: zero key length |
| 56 |
{"17", []any{hmaps.Params{"": "foo"}}, false}, |
| 57 |
{"18", []any{map[string]any{"": "foo"}}, false}, |
| 58 |
{"19", []any{[]string{"", "foo"}}, false}, |
| 59 |
{"20", []any{[]any{"", 6}}, false}, |
| 60 |
{"21", []any{"", "foo"}, false}, |
| 61 |
// errors: odd number of values |
| 62 |
{"22", []any{[]string{"a", "foo", "b"}}, false}, |
| 63 |
{"23", []any{[]any{"a", 6, "b"}}, false}, |
| 64 |
{"24", []any{"a", "foo", "b"}, false}, |
| 65 |
// errors: value cannot be cast to string |
| 66 |
{"25", []any{map[string]any{"a": "foo", "b": tstNoStringer{}}}, false}, |
| 67 |
{"26", []any{[]any{"a", "foo", "b", tstNoStringer{}}}, false}, |
| 68 |
{"27", []any{"a", "foo", "b", tstNoStringer{}}, false}, |
| 69 |
} { |
| 70 |
errMsg := qt.Commentf("[%s] %v", test.name, test.params) |
| 71 |
|
| 72 |
result, err := ns.Querify(test.params...) |
| 73 |
|
| 74 |
if b, ok := test.expect.(bool); ok && !b { |
| 75 |
c.Assert(err, qt.Not(qt.IsNil), errMsg) |
| 76 |
continue |
| 77 |
} |
| 78 |
|
| 79 |
c.Assert(err, qt.IsNil, errMsg) |
| 80 |
c.Assert(result, qt.Equals, test.expect, errMsg) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func BenchmarkQuerify(b *testing.B) { |
| 85 |
ns := newNs() |
| 86 |
params := []any{"a", "b", "c", "d", "f", " &"} |
| 87 |
|
| 88 |
for b.Loop() { |
| 89 |
_, err := ns.Querify(params...) |
| 90 |
if err != nil { |
| 91 |
b.Fatal(err) |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func BenchmarkQuerifySlice(b *testing.B) { |
| 97 |
ns := newNs() |
| 98 |
params := []string{"a", "b", "c", "d", "f", " &"} |
| 99 |
|
| 100 |
for b.Loop() { |
| 101 |
_, err := ns.Querify(params) |
| 102 |
if err != nil { |
| 103 |
b.Fatal(err) |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func BenchmarkQuerifyMap(b *testing.B) { |
| 109 |
ns := newNs() |
| 110 |
params := map[string]any{"a": "b", "c": "d", "f": " &"} |
| 111 |
|
| 112 |
for b.Loop() { |
| 113 |
_, err := ns.Querify(params) |
| 114 |
if err != nil { |
| 115 |
b.Fatal(err) |
| 116 |
} |
| 117 |
} |
| 118 |
} |