github.com/whatlly/hugo@v0.47.1/tpl/cast/cast_test.go (about)

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