cast_test.go - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
cast_test.go (2672B)
---
1 // Copyright 2017 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 cast
15
16 import (
17 "html/template"
18 "testing"
19
20 "github.com/bep/imagemeta"
21 qt "github.com/frankban/quicktest"
22 "github.com/gohugoio/hugo/htesting/hqt"
23 )
24
25 func TestToInt(t *testing.T) {
26 t.Parallel()
27 c := qt.New(t)
28
29 ns := New()
30
31 for i, test := range []struct {
32 v any
33 expect any
34 }{
35 {"1", 1},
36 {template.HTML("2"), 2},
37 {template.CSS("3"), 3},
38 {template.HTMLAttr("4"), 4},
39 {template.JS("5"), 5},
40 {template.JSStr("6"), 6},
41 {"a", false},
42 {t, false},
43 } {
44 errMsg := qt.Commentf("[%d] %v", i, test.v)
45
46 result, err := ns.ToInt(test.v)
47
48 if b, ok := test.expect.(bool); ok && !b {
49 c.Assert(err, qt.Not(qt.IsNil), errMsg)
50 continue
51 }
52
53 c.Assert(err, qt.IsNil, errMsg)
54 c.Assert(result, qt.Equals, test.expect, errMsg)
55 }
56 }
57
58 func TestToString(t *testing.T) {
59 t.Parallel()
60 c := qt.New(t)
61 ns := New()
62
63 for i, test := range []struct {
64 v any
65 expect any
66 }{
67 {1, "1"},
68 {template.HTML("2"), "2"},
69 {"a", "a"},
70 {t, false},
71 } {
72 errMsg := qt.Commentf("[%d] %v", i, test.v)
73
74 result, err := ns.ToString(test.v)
75
76 if b, ok := test.expect.(bool); ok && !b {
77 c.Assert(err, qt.Not(qt.IsNil), errMsg)
78 continue
79 }
80
81 c.Assert(err, qt.IsNil, errMsg)
82 c.Assert(result, qt.Equals, test.expect, errMsg)
83 }
84 }
85
86 func TestToFloat(t *testing.T) {
87 t.Parallel()
88 c := qt.New(t)
89 ns := New()
90 oneThird, _ := imagemeta.NewRat[uint32](1, 3)
91
92 for i, test := range []struct {
93 v any
94 expect any
95 }{
96 {"1", 1.0},
97 {template.HTML("2"), 2.0},
98 {template.CSS("3"), 3.0},
99 {template.HTMLAttr("4"), 4.0},
100 {template.JS("-5.67"), -5.67},
101 {template.JSStr("6"), 6.0},
102 {"1.23", 1.23},
103 {"-1.23", -1.23},
104 {"0", 0.0},
105 {float64(2.12), 2.12},
106 {int64(123), 123.0},
107 {oneThird, 0.3333333333333333},
108 {2, 2.0},
109 {t, false},
110 } {
111 errMsg := qt.Commentf("[%d] %v", i, test.v)
112
113 result, err := ns.ToFloat(test.v)
114
115 if b, ok := test.expect.(bool); ok && !b {
116 c.Assert(err, qt.Not(qt.IsNil), errMsg)
117 continue
118 }
119
120 c.Assert(err, qt.IsNil, errMsg)
121 c.Assert(result, hqt.IsSameFloat64, test.expect, errMsg)
122 }
123 }