| 1 |
// Copyright (c) 2021, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package tdhttp_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"net/url" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/helpers/tdhttp" |
| 14 |
"github.com/maxatome/go-testdeep/td" |
| 15 |
) |
| 16 |
|
| 17 |
type qTest1 struct{} |
| 18 |
|
| 19 |
func (qTest1) String() string { return "qTest1!" } |
| 20 |
|
| 21 |
type qTest2 struct{} |
| 22 |
|
| 23 |
func (*qTest2) String() string { return "qTest2!" } |
| 24 |
|
| 25 |
func TestQ(t *testing.T) { |
| 26 |
q := tdhttp.Q{ |
| 27 |
"str1": "v1", |
| 28 |
"str2": []string{"v20", "v21"}, |
| 29 |
"int1": 1234, |
| 30 |
"int2": []int{1, 2, 3}, |
| 31 |
"uint1": uint(1234), |
| 32 |
"uint2": [3]uint{1, 2, 3}, |
| 33 |
"float1": 1.2, |
| 34 |
"float2": []float64{1.2, 3.4}, |
| 35 |
"bool1": true, |
| 36 |
"bool2": [2]bool{true, false}, |
| 37 |
} |
| 38 |
td.Cmp(t, q.Values(), url.Values{ |
| 39 |
"str1": []string{"v1"}, |
| 40 |
"str2": []string{"v20", "v21"}, |
| 41 |
"int1": []string{"1234"}, |
| 42 |
"int2": []string{"1", "2", "3"}, |
| 43 |
"uint1": []string{"1234"}, |
| 44 |
"uint2": []string{"1", "2", "3"}, |
| 45 |
"float1": []string{"1.2"}, |
| 46 |
"float2": []string{"1.2", "3.4"}, |
| 47 |
"bool1": []string{"true"}, |
| 48 |
"bool2": []string{"true", "false"}, |
| 49 |
}) |
| 50 |
|
| 51 |
// Auto deref pointers |
| 52 |
num := 123 |
| 53 |
pnum := &num |
| 54 |
ppnum := &pnum |
| 55 |
q = tdhttp.Q{ |
| 56 |
"pnum": pnum, |
| 57 |
"ppnum": ppnum, |
| 58 |
"pppnum": &ppnum, |
| 59 |
"slice": []***int{&ppnum, &ppnum}, |
| 60 |
"pslice": &[]***int{&ppnum, &ppnum}, |
| 61 |
"array": [2]***int{&ppnum, &ppnum}, |
| 62 |
"parray": &[2]***int{&ppnum, &ppnum}, |
| 63 |
} |
| 64 |
td.Cmp(t, q.Values(), url.Values{ |
| 65 |
"pnum": []string{"123"}, |
| 66 |
"ppnum": []string{"123"}, |
| 67 |
"pppnum": []string{"123"}, |
| 68 |
"slice": []string{"123", "123"}, |
| 69 |
"pslice": []string{"123", "123"}, |
| 70 |
"array": []string{"123", "123"}, |
| 71 |
"parray": []string{"123", "123"}, |
| 72 |
}) |
| 73 |
|
| 74 |
// Auto deref interfaces |
| 75 |
q = tdhttp.Q{ |
| 76 |
"all": []any{ |
| 77 |
"string", |
| 78 |
-1, |
| 79 |
int8(-2), |
| 80 |
int16(-3), |
| 81 |
int32(-4), |
| 82 |
int64(-5), |
| 83 |
uint(1), |
| 84 |
uint8(2), |
| 85 |
uint16(3), |
| 86 |
uint32(4), |
| 87 |
uint64(5), |
| 88 |
float32(6), |
| 89 |
float64(7), |
| 90 |
true, |
| 91 |
ppnum, |
| 92 |
(*int)(nil), // ignored |
| 93 |
nil, // ignored |
| 94 |
qTest1{}, |
| 95 |
&qTest1{}, // does not implement fmt.Stringer, but qTest does |
| 96 |
// qTest2{} panics as it does not implement fmt.Stringer, see Errors below |
| 97 |
&qTest2{}, |
| 98 |
}, |
| 99 |
} |
| 100 |
td.Cmp(t, q.Values(), url.Values{ |
| 101 |
"all": []string{ |
| 102 |
"string", |
| 103 |
"-1", |
| 104 |
"-2", |
| 105 |
"-3", |
| 106 |
"-4", |
| 107 |
"-5", |
| 108 |
"1", |
| 109 |
"2", |
| 110 |
"3", |
| 111 |
"4", |
| 112 |
"5", |
| 113 |
"6", |
| 114 |
"7", |
| 115 |
"true", |
| 116 |
"123", |
| 117 |
"qTest1!", |
| 118 |
"qTest1!", |
| 119 |
"qTest2!", |
| 120 |
}, |
| 121 |
}) |
| 122 |
|
| 123 |
// nil case |
| 124 |
pnum = nil |
| 125 |
q = tdhttp.Q{ |
| 126 |
"nil1": &ppnum, |
| 127 |
"nil2": (*int)(nil), |
| 128 |
"nil3": nil, |
| 129 |
"nil4": []*int{nil, nil}, |
| 130 |
"nil5": ([]int)(nil), |
| 131 |
"nil6": []any{nil, nil}, |
| 132 |
} |
| 133 |
td.Cmp(t, q.Values(), url.Values{}) |
| 134 |
|
| 135 |
q = tdhttp.Q{ |
| 136 |
"id": []int{12, 34}, |
| 137 |
"draft": true, |
| 138 |
} |
| 139 |
td.Cmp(t, q.Encode(), "draft=true&id=12&id=34") |
| 140 |
|
| 141 |
// Errors |
| 142 |
td.CmpPanic(t, func() { (tdhttp.Q{"panic": map[string]bool{}}).Values() }, |
| 143 |
td.Contains(`don't know how to add type map[string]bool (map) to param "panic"`)) |
| 144 |
td.CmpPanic(t, func() { (tdhttp.Q{"panic": qTest2{}}).Values() }, |
| 145 |
td.Contains(`don't know how to add type tdhttp_test.qTest2 (struct) to param "panic"`)) |
| 146 |
|
| 147 |
td.CmpPanic(t, |
| 148 |
func() { (tdhttp.Q{"panic": []any{[]int{}}}).Values() }, |
| 149 |
td.Contains(`slice is only allowed at the root level for param "panic"`)) |
| 150 |
} |