k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/validate/helpers_test.go (about)

     1  // Copyright 2015 go-swagger maintainers
     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  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package validate
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func integerFactory(base int) []interface{} {
    24  	return []interface{}{
    25  		base,
    26  		int8(base),
    27  		int16(base),
    28  		int32(base),
    29  		int64(base),
    30  		uint(base),
    31  		uint8(base),
    32  		uint16(base),
    33  		uint32(base),
    34  		uint64(base),
    35  		float32(base),
    36  		float64(base),
    37  	}
    38  }
    39  
    40  // Test cases in private method asInt64()
    41  func TestHelpers_asInt64(t *testing.T) {
    42  	for _, v := range integerFactory(3) {
    43  		assert.Equal(t, int64(3), valueHelp.asInt64(v))
    44  	}
    45  
    46  	// Non numeric
    47  	if assert.NotPanics(t, func() {
    48  		valueHelp.asInt64("123")
    49  	}) {
    50  		assert.Equal(t, valueHelp.asInt64("123"), (int64)(0))
    51  	}
    52  }
    53  
    54  // Test cases in private method asUint64()
    55  func TestHelpers_asUint64(t *testing.T) {
    56  	for _, v := range integerFactory(3) {
    57  		assert.Equal(t, uint64(3), valueHelp.asUint64(v))
    58  	}
    59  
    60  	// Non numeric
    61  	if assert.NotPanics(t, func() {
    62  		valueHelp.asUint64("123")
    63  	}) {
    64  		assert.Equal(t, valueHelp.asUint64("123"), (uint64)(0))
    65  	}
    66  }
    67  
    68  // Test cases in private method asFloat64()
    69  func TestHelpers_asFloat64(t *testing.T) {
    70  	for _, v := range integerFactory(3) {
    71  		assert.Equal(t, float64(3), valueHelp.asFloat64(v))
    72  	}
    73  
    74  	// Non numeric
    75  	if assert.NotPanics(t, func() {
    76  		valueHelp.asFloat64("123")
    77  	}) {
    78  		assert.Equal(t, valueHelp.asFloat64("123"), (float64)(0))
    79  	}
    80  }