Debian Codegraph @

tpl/compare/compare_integration_test.go

raw
1 // Copyright 2026 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 compare_test
15
16 import (
17 "testing"
18
19 "github.com/gohugoio/hugo/hugolib"
20 "github.com/gohugoio/hugo/tpl/compare"
21 )
22
23 // See issue 15322.
24 func TestEqIntFloat(t *testing.T) {
25 t.Parallel()
26
27 files := `
28 -- hugo.toml --
29 -- layouts/home.html --
30 eq: {{ eq 1 1.0 }}|{{ eq 1.0 1 }}|{{ eq 1 1.5 }}|{{ eq 1 2 1.0 }}
31 ne: {{ ne 1 1.0 }}|{{ ne 1 1.5 }}
32 lt: {{ lt 1 1.0 }}|{{ lt 1 1.5 }}
33 le: {{ le 1 1.0 }}|{{ le 1 0.5 }}
34 gt: {{ gt 1 1.0 }}|{{ gt 1 0.5 }}
35 ge: {{ ge 1 1.0 }}|{{ ge 1 1.5 }}
36 `
37 b := hugolib.Test(t, files)
38
39 b.AssertFileContent(
40 "public/index.html",
41 "eq: true|true|false|true",
42 "ne: false|true",
43 "lt: false|true",
44 "le: true|false",
45 "gt: false|true",
46 "ge: true|false",
47 )
48 }
49
50 func BenchmarkCompare(b *testing.B) {
51 files := `
52 -- hugo.toml --
53 `
54 bb := hugolib.Test(b, files)
55
56 ns := bb.H.TemplateStore.GetTemplateFuncsNamespace("compare").(*compare.Namespace)
57
58 b.ResetTimer()
59
60 run := func(name string, fn func(v1, v2 any)) {
61 b.Run(name, func(b *testing.B) {
62 b.Run("mixed int and float", func(b *testing.B) {
63 for b.Loop() {
64 fn(42, 42.0)
65 }
66 })
67 b.Run("mixed float and int", func(b *testing.B) {
68 for b.Loop() {
69 fn(42.0, 42)
70 }
71 })
72 b.Run("mixed string and int", func(b *testing.B) {
73 for b.Loop() {
74 fn("42", 42)
75 }
76 })
77 b.Run("mixed string and float", func(b *testing.B) {
78 for b.Loop() {
79 fn("42", 42.0)
80 }
81 })
82 b.Run("mixed float and string", func(b *testing.B) {
83 for b.Loop() {
84 fn(42.0, "42")
85 }
86 })
87 // End of mixed type benchmark
88 // start of same type benchmark
89 b.Run("same int", func(b *testing.B) {
90 for b.Loop() {
91 fn(42, 42)
92 }
93 })
94 b.Run("same float", func(b *testing.B) {
95 for b.Loop() {
96 fn(42.0, 42.0)
97 }
98 })
99 b.Run("same string", func(b *testing.B) {
100 for b.Loop() {
101 fn("foo", "foo")
102 }
103 })
104 b.Run("same bool", func(b *testing.B) {
105 for b.Loop() {
106 fn(true, true)
107 }
108 })
109 b.Run("same nil", func(b *testing.B) {
110 for b.Loop() {
111 fn(nil, nil)
112 }
113 })
114 b.Run("same complex", func(b *testing.B) {
115 for b.Loop() {
116 fn(complex(1, 2), complex(1, 2))
117 }
118 })
119 // End of same type benchmark
120 // start of edge case benchmark
121 b.Run("empty slice", func(b *testing.B) {
122 for b.Loop() {
123 fn([]int{}, []int{})
124 }
125 })
126 b.Run("empty map", func(b *testing.B) {
127 for b.Loop() {
128 fn(map[string]int{}, map[string]int{})
129 }
130 })
131 // End of edge case benchmark
132 // start of nil benchmark
133 b.Run("nil and non-nil", func(b *testing.B) {
134 for b.Loop() {
135 fn(nil, 42)
136 }
137 })
138 b.Run("non-nil and nil", func(b *testing.B) {
139 for b.Loop() {
140 fn(42, nil)
141 }
142 })
143 // End of nil benchmark
144 // start of large number benchmark
145 b.Run("large numbers", func(b *testing.B) {
146 for b.Loop() {
147 fn(1e18, 1e18)
148 }
149 })
150 // End of large number benchmark
151 // start of small number benchmark
152 b.Run("small numbers", func(b *testing.B) {
153 for b.Loop() {
154 fn(1e-18, 1e-18)
155 }
156 })
157 // End of small number benchmark
158 })
159 }
160
161 run("Eq", func(v1, v2 any) {
162 ns.Eq(v1, v2)
163 })
164
165 run("Gt", func(v1, v2 any) {
166 ns.Gt(v1, v2)
167 })
168 }