...

Source file src/github.com/go-openapi/validate/helpers_test.go

Documentation: github.com/go-openapi/validate

     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  	"errors"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestHelpers_addPointerError(t *testing.T) {
    26  	res := new(Result)
    27  	r := errorHelp.addPointerError(res, errors.New("my error"), "my ref", "path")
    28  	require.NotEmpty(t, r.Errors)
    29  	msg := r.Errors[0].Error()
    30  	assert.Contains(t, msg, "could not resolve reference in path to $ref my ref: my error")
    31  }
    32  
    33  func integerFactory(base int) []interface{} {
    34  	return []interface{}{
    35  		base,
    36  		int8(base),
    37  		int16(base),
    38  		int32(base),
    39  		int64(base),
    40  		uint(base),
    41  		uint8(base),
    42  		uint16(base),
    43  		uint32(base),
    44  		uint64(base),
    45  		float32(base),
    46  		float64(base),
    47  	}
    48  }
    49  
    50  // Test cases in private method asInt64()
    51  func TestHelpers_asInt64(t *testing.T) {
    52  	for _, v := range integerFactory(3) {
    53  		assert.Equal(t, int64(3), valueHelp.asInt64(v))
    54  	}
    55  
    56  	// Non numeric
    57  	if assert.NotPanics(t, func() {
    58  		valueHelp.asInt64("123")
    59  	}) {
    60  		assert.Equal(t, int64(0), valueHelp.asInt64("123"))
    61  	}
    62  }
    63  
    64  // Test cases in private method asUint64()
    65  func TestHelpers_asUint64(t *testing.T) {
    66  	for _, v := range integerFactory(3) {
    67  		assert.Equal(t, uint64(3), valueHelp.asUint64(v))
    68  	}
    69  
    70  	// Non numeric
    71  	if assert.NotPanics(t, func() {
    72  		valueHelp.asUint64("123")
    73  	}) {
    74  		assert.Equal(t, uint64(0), valueHelp.asUint64("123"))
    75  	}
    76  }
    77  
    78  // Test cases in private method asFloat64()
    79  func TestHelpers_asFloat64(t *testing.T) {
    80  	const epsilon = 1e-9
    81  
    82  	for _, v := range integerFactory(3) {
    83  		assert.InDelta(t, float64(3), valueHelp.asFloat64(v), epsilon)
    84  	}
    85  
    86  	// Non numeric
    87  	if assert.NotPanics(t, func() {
    88  		valueHelp.asFloat64("123")
    89  	}) {
    90  		assert.InDelta(t, float64(0), valueHelp.asFloat64("123"), epsilon)
    91  	}
    92  }
    93  

View as plain text