File: q_test.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (150 lines) | stat: -rw-r--r-- 3,262 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2021, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

package tdhttp_test

import (
	"net/url"
	"testing"

	"github.com/maxatome/go-testdeep/helpers/tdhttp"
	"github.com/maxatome/go-testdeep/td"
)

type qTest1 struct{}

func (qTest1) String() string { return "qTest1!" }

type qTest2 struct{}

func (*qTest2) String() string { return "qTest2!" }

func TestQ(t *testing.T) {
	q := tdhttp.Q{
		"str1":   "v1",
		"str2":   []string{"v20", "v21"},
		"int1":   1234,
		"int2":   []int{1, 2, 3},
		"uint1":  uint(1234),
		"uint2":  [3]uint{1, 2, 3},
		"float1": 1.2,
		"float2": []float64{1.2, 3.4},
		"bool1":  true,
		"bool2":  [2]bool{true, false},
	}
	td.Cmp(t, q.Values(), url.Values{
		"str1":   []string{"v1"},
		"str2":   []string{"v20", "v21"},
		"int1":   []string{"1234"},
		"int2":   []string{"1", "2", "3"},
		"uint1":  []string{"1234"},
		"uint2":  []string{"1", "2", "3"},
		"float1": []string{"1.2"},
		"float2": []string{"1.2", "3.4"},
		"bool1":  []string{"true"},
		"bool2":  []string{"true", "false"},
	})

	// Auto deref pointers
	num := 123
	pnum := &num
	ppnum := &pnum
	q = tdhttp.Q{
		"pnum":   pnum,
		"ppnum":  ppnum,
		"pppnum": &ppnum,
		"slice":  []***int{&ppnum, &ppnum},
		"pslice": &[]***int{&ppnum, &ppnum},
		"array":  [2]***int{&ppnum, &ppnum},
		"parray": &[2]***int{&ppnum, &ppnum},
	}
	td.Cmp(t, q.Values(), url.Values{
		"pnum":   []string{"123"},
		"ppnum":  []string{"123"},
		"pppnum": []string{"123"},
		"slice":  []string{"123", "123"},
		"pslice": []string{"123", "123"},
		"array":  []string{"123", "123"},
		"parray": []string{"123", "123"},
	})

	// Auto deref interfaces
	q = tdhttp.Q{
		"all": []any{
			"string",
			-1,
			int8(-2),
			int16(-3),
			int32(-4),
			int64(-5),
			uint(1),
			uint8(2),
			uint16(3),
			uint32(4),
			uint64(5),
			float32(6),
			float64(7),
			true,
			ppnum,
			(*int)(nil), // ignored
			nil,         // ignored
			qTest1{},
			&qTest1{}, // does not implement fmt.Stringer, but qTest does
			// qTest2{} panics as it does not implement fmt.Stringer, see Errors below
			&qTest2{},
		},
	}
	td.Cmp(t, q.Values(), url.Values{
		"all": []string{
			"string",
			"-1",
			"-2",
			"-3",
			"-4",
			"-5",
			"1",
			"2",
			"3",
			"4",
			"5",
			"6",
			"7",
			"true",
			"123",
			"qTest1!",
			"qTest1!",
			"qTest2!",
		},
	})

	// nil case
	pnum = nil
	q = tdhttp.Q{
		"nil1": &ppnum,
		"nil2": (*int)(nil),
		"nil3": nil,
		"nil4": []*int{nil, nil},
		"nil5": ([]int)(nil),
		"nil6": []any{nil, nil},
	}
	td.Cmp(t, q.Values(), url.Values{})

	q = tdhttp.Q{
		"id":    []int{12, 34},
		"draft": true,
	}
	td.Cmp(t, q.Encode(), "draft=true&id=12&id=34")

	// Errors
	td.CmpPanic(t, func() { (tdhttp.Q{"panic": map[string]bool{}}).Values() },
		td.Contains(`don't know how to add type map[string]bool (map) to param "panic"`))
	td.CmpPanic(t, func() { (tdhttp.Q{"panic": qTest2{}}).Values() },
		td.Contains(`don't know how to add type tdhttp_test.qTest2 (struct) to param "panic"`))

	td.CmpPanic(t,
		func() { (tdhttp.Q{"panic": []any{[]int{}}}).Values() },
		td.Contains(`slice is only allowed at the root level for param "panic"`))
}