hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

cast_test.go (2514B)

    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 	qt "github.com/frankban/quicktest"
   21 )
   22 
   23 func TestToInt(t *testing.T) {
   24 	t.Parallel()
   25 	c := qt.New(t)
   26 
   27 	ns := New()
   28 
   29 	for i, test := range []struct {
   30 		v      any
   31 		expect any
   32 	}{
   33 		{"1", 1},
   34 		{template.HTML("2"), 2},
   35 		{template.CSS("3"), 3},
   36 		{template.HTMLAttr("4"), 4},
   37 		{template.JS("5"), 5},
   38 		{template.JSStr("6"), 6},
   39 		{"a", false},
   40 		{t, false},
   41 	} {
   42 		errMsg := qt.Commentf("[%d] %v", i, test.v)
   43 
   44 		result, err := ns.ToInt(test.v)
   45 
   46 		if b, ok := test.expect.(bool); ok && !b {
   47 			c.Assert(err, qt.Not(qt.IsNil), errMsg)
   48 			continue
   49 		}
   50 
   51 		c.Assert(err, qt.IsNil, errMsg)
   52 		c.Assert(result, qt.Equals, test.expect, errMsg)
   53 	}
   54 }
   55 
   56 func TestToString(t *testing.T) {
   57 	t.Parallel()
   58 	c := qt.New(t)
   59 	ns := New()
   60 
   61 	for i, test := range []struct {
   62 		v      any
   63 		expect any
   64 	}{
   65 		{1, "1"},
   66 		{template.HTML("2"), "2"},
   67 		{"a", "a"},
   68 		{t, false},
   69 	} {
   70 		errMsg := qt.Commentf("[%d] %v", i, test.v)
   71 
   72 		result, err := ns.ToString(test.v)
   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 TestToFloat(t *testing.T) {
   85 	t.Parallel()
   86 	c := qt.New(t)
   87 	ns := New()
   88 
   89 	for i, test := range []struct {
   90 		v      any
   91 		expect any
   92 	}{
   93 		{"1", 1.0},
   94 		{template.HTML("2"), 2.0},
   95 		{template.CSS("3"), 3.0},
   96 		{template.HTMLAttr("4"), 4.0},
   97 		{template.JS("-5.67"), -5.67},
   98 		{template.JSStr("6"), 6.0},
   99 		{"1.23", 1.23},
  100 		{"-1.23", -1.23},
  101 		{"0", 0.0},
  102 		{float64(2.12), 2.12},
  103 		{int64(123), 123.0},
  104 		{2, 2.0},
  105 		{t, false},
  106 	} {
  107 		errMsg := qt.Commentf("[%d] %v", i, test.v)
  108 
  109 		result, err := ns.ToFloat(test.v)
  110 
  111 		if b, ok := test.expect.(bool); ok && !b {
  112 			c.Assert(err, qt.Not(qt.IsNil), errMsg)
  113 			continue
  114 		}
  115 
  116 		c.Assert(err, qt.IsNil, errMsg)
  117 		c.Assert(result, qt.Equals, test.expect, errMsg)
  118 	}
  119 }