1 package resourcename
2
3 import (
4 "testing"
5
6 "gotest.tools/v3/assert"
7 )
8
9 func TestScan(t *testing.T) {
10 t.Parallel()
11 t.Run("no variables", func(t *testing.T) {
12 t.Parallel()
13 assert.NilError(
14 t,
15 Sscan(
16 "publishers",
17 "publishers",
18 ),
19 )
20 })
21
22 t.Run("single variable", func(t *testing.T) {
23 t.Parallel()
24 var publisher string
25 assert.NilError(
26 t,
27 Sscan(
28 "publishers/foo",
29 "publishers/{publisher}",
30 &publisher,
31 ),
32 )
33 assert.Equal(t, "foo", publisher)
34 })
35
36 t.Run("two variables", func(t *testing.T) {
37 t.Parallel()
38 var publisher, book string
39 assert.NilError(
40 t,
41 Sscan(
42 "publishers/foo/books/bar",
43 "publishers/{publisher}/books/{book}",
44 &publisher,
45 &book,
46 ),
47 )
48 assert.Equal(t, "foo", publisher)
49 assert.Equal(t, "bar", book)
50 })
51
52 t.Run("two variables singleton", func(t *testing.T) {
53 t.Parallel()
54 var publisher, book string
55 assert.NilError(
56 t,
57 Sscan(
58 "publishers/foo/books/bar/settings",
59 "publishers/{publisher}/books/{book}/settings",
60 &publisher,
61 &book,
62 ),
63 )
64 assert.Equal(t, "foo", publisher)
65 assert.Equal(t, "bar", book)
66 })
67
68 t.Run("two variables singleton", func(t *testing.T) {
69 t.Parallel()
70 var publisher, book string
71 assert.NilError(
72 t,
73 Sscan(
74 "publishers/foo/books/bar/settings",
75 "publishers/{publisher}/books/{book}/settings",
76 &publisher,
77 &book,
78 ),
79 )
80 assert.Equal(t, "foo", publisher)
81 assert.Equal(t, "bar", book)
82 })
83
84 t.Run("trailing segments", func(t *testing.T) {
85 t.Parallel()
86 var publisher, book string
87 assert.ErrorContains(
88 t,
89 Sscan(
90 "publishers/foo/books/bar/settings",
91 "publishers/{publisher}/books/{book}",
92 &publisher,
93 &book,
94 ),
95 "trailing",
96 )
97 })
98
99 t.Run("too few variables", func(t *testing.T) {
100 t.Parallel()
101 var publisher string
102 assert.ErrorContains(
103 t,
104 Sscan(
105 "publishers/foo/books/bar/settings",
106 "publishers/{publisher}/books/{book}",
107 &publisher,
108 ),
109 "too few variables",
110 )
111 })
112
113 t.Run("too many variables", func(t *testing.T) {
114 t.Parallel()
115 var publisher, book, extra string
116 assert.ErrorContains(
117 t,
118 Sscan(
119 "publishers/foo/books/bar",
120 "publishers/{publisher}/books/{book}",
121 &publisher,
122 &book,
123 &extra,
124 ),
125 "too many variables",
126 )
127 })
128 }
129
130
131 var benchmarkScanSink string
132
133 func BenchmarkScan(b *testing.B) {
134 for i := 0; i < b.N; i++ {
135 var publisher, book string
136 if err := Sscan(
137 "publishers/foo/books/bar",
138 "publishers/{publisher}/books/{book}",
139 &publisher,
140 &book,
141 ); err != nil {
142 b.Fatal(err)
143 }
144 benchmarkScanSink = publisher
145 }
146 }
147
View as plain text