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