| 1 |
// Copyright 2025 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 metadecoders_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/hugolib" |
| 20 |
) |
| 21 |
|
| 22 |
func TestYAMLIntegerSortIssue14078(t *testing.T) { |
| 23 |
files := ` |
| 24 |
-- assets/mydata.yaml -- |
| 25 |
a: |
| 26 |
weight: 1 |
| 27 |
x: |
| 28 |
weight: 2 |
| 29 |
c: |
| 30 |
weight: 3 |
| 31 |
t: |
| 32 |
weight: 4 |
| 33 |
|
| 34 |
-- layouts/all.html -- |
| 35 |
{{ $mydata := resources.Get "mydata.yaml" | transform.Unmarshal }} |
| 36 |
Type: {{ printf "%T" $mydata.a.weight }}| |
| 37 |
Sorted: {{ sort $mydata "weight" }}| |
| 38 |
|
| 39 |
` |
| 40 |
|
| 41 |
b := hugolib.Test(t, files) |
| 42 |
|
| 43 |
b.AssertFileContent("public/index.html", "Sorted: [map[weight:1] map[weight:2] map[weight:3] map[weight:4]]|") |
| 44 |
} |
| 45 |
|
| 46 |
func TestYAMLIntegerWhere(t *testing.T) { |
| 47 |
files := ` |
| 48 |
-- assets/mydata.yaml -- |
| 49 |
a: |
| 50 |
weight: 1 |
| 51 |
x: |
| 52 |
weight: 2 |
| 53 |
c: |
| 54 |
weight: 3 |
| 55 |
t: |
| 56 |
weight: 4 |
| 57 |
-- assets/myslice.yaml -- |
| 58 |
- weight: 1 |
| 59 |
name: one |
| 60 |
- weight: 2 |
| 61 |
name: two |
| 62 |
- weight: 3 |
| 63 |
name: three |
| 64 |
- weight: 4 |
| 65 |
name: four |
| 66 |
|
| 67 |
-- layouts/all.html -- |
| 68 |
{{ $mydata1 := resources.Get "mydata.yaml" | transform.Unmarshal }} |
| 69 |
{{ $myslice := resources.Get "myslice.yaml" | transform.Unmarshal }} |
| 70 |
{{ $filtered := where $myslice "weight" "ge" $mydata1.x.weight }} |
| 71 |
mydata1: {{ $mydata1 }}| |
| 72 |
Filtered: {{ $filtered }}| |
| 73 |
|
| 74 |
` |
| 75 |
|
| 76 |
b := hugolib.Test(t, files) |
| 77 |
|
| 78 |
b.AssertFileContent("public/index.html", "[map[name:two weight:2] map[name:three weight:3] map[name:four weight:4]]|") |
| 79 |
} |