11package solace
22
33import (
4- "reflect"
54 "testing"
5+
6+ "github.com/stretchr/testify/assert"
7+ "github.com/stretchr/testify/require"
68)
79
810func TestDefaultOptions (t * testing.T ) {
911 opts := defaultOptions ()
1012
1113 // Test default values
12- if opts .vpn != "default" {
13- t .Errorf ("Expected default vpn to be 'default', got %s" , opts .vpn )
14- }
15-
16- if opts .username != "root" {
17- t .Errorf ("Expected default username to be 'root', got %s" , opts .username )
18- }
19-
20- if opts .password != "password" {
21- t .Errorf ("Expected default password to be 'password', got %s" , opts .password )
22- }
23-
24- if opts .shmSize != 1 << 30 {
25- t .Errorf ("Expected default shmSize to be %d, got %d" , 1 << 30 , opts .shmSize )
26- }
14+ assert .Equal (t , "default" , opts .vpn )
15+ assert .Equal (t , "root" , opts .username )
16+ assert .Equal (t , "password" , opts .password )
17+ assert .Equal (t , int64 (1 << 30 ), opts .shmSize )
2718
2819 // Test that all default services are included
2920 expectedServices := []Service {ServiceAMQP , ServiceSMF , ServiceREST , ServiceMQTT }
30- if len (opts .services ) != len (expectedServices ) {
31- t .Errorf ("Expected %d services, got %d" , len (expectedServices ), len (opts .services ))
32- }
21+ require .Len (t , opts .services , len (expectedServices ))
3322
3423 for _ , expectedService := range expectedServices {
3524 found := false
@@ -39,9 +28,7 @@ func TestDefaultOptions(t *testing.T) {
3928 break
4029 }
4130 }
42- if ! found {
43- t .Errorf ("Expected service %s:%d to be in services, but it wasn't found" , expectedService .Name , expectedService .Port )
44- }
31+ assert .True (t , found , "Expected service %s:%d to be in services" , expectedService .Name , expectedService .Port )
4532 }
4633}
4734
@@ -74,19 +61,17 @@ func TestWithServices(t *testing.T) {
7461 option := WithServices (tt .services ... )
7562
7663 err := option (opts )
77- if err != nil {
78- if len (tt .services ) == 0 {
79- return
80- }
81- t .Errorf ("WithServices returned error: %v" , err )
64+ if len (tt .services ) == 0 {
65+ assert .Error (t , err )
66+ return
8267 }
8368
84- if ! reflect .DeepEqual (opts .services , tt .expectedServices ) {
85- t .Errorf ("Expected services %v, got %v" , tt .expectedServices , opts .services )
86- }
69+ require .NoError (t , err )
70+ assert .Equal (t , tt .expectedServices , opts .services )
8771 })
8872 }
8973}
74+
9075func TestWithCredentials (t * testing.T ) {
9176 tests := []struct {
9277 name string
@@ -116,17 +101,10 @@ func TestWithCredentials(t *testing.T) {
116101 option := WithCredentials (tt .username , tt .password )
117102
118103 err := option (opts )
119- if err != nil {
120- t .Errorf ("WithCredentials returned error: %v" , err )
121- }
122-
123- if opts .username != tt .username {
124- t .Errorf ("Expected username %s, got %s" , tt .username , opts .username )
125- }
104+ require .NoError (t , err )
126105
127- if opts .password != tt .password {
128- t .Errorf ("Expected password %s, got %s" , tt .password , opts .password )
129- }
106+ assert .Equal (t , tt .username , opts .username )
107+ assert .Equal (t , tt .password , opts .password )
130108 })
131109 }
132110}
@@ -156,13 +134,9 @@ func TestWithVpn(t *testing.T) {
156134 option := WithVPN (tt .vpn )
157135
158136 err := option (opts )
159- if err != nil {
160- t .Errorf ("WithVPN returned error: %v" , err )
161- }
137+ require .NoError (t , err )
162138
163- if opts .vpn != tt .vpn {
164- t .Errorf ("Expected vpn %s, got %s" , tt .vpn , opts .vpn )
165- }
139+ assert .Equal (t , tt .vpn , opts .vpn )
166140 })
167141 }
168142}
@@ -204,39 +178,21 @@ func TestWithQueue(t *testing.T) {
204178 option := WithQueue (tt .queueName , tt .topic )
205179
206180 err := option (opts )
207- if err != nil {
208- t .Errorf ("WithQueue returned error: %v" , err )
209- }
181+ require .NoError (t , err )
210182
211- if opts .queues == nil {
212- t .Errorf ("Expected queues to be initialized" )
213- return
214- }
183+ assert .NotNil (t , opts .queues , "Expected queues to be initialized" )
215184
216185 topics , exists := opts .queues [tt .queueName ]
217- if ! exists {
218- t .Errorf ("Expected queue %s to exist" , tt .queueName )
219- return
220- }
186+ assert .True (t , exists , "Expected queue %s to exist" , tt .queueName )
221187
222- if len (topics ) != tt .expectedLen {
223- t .Errorf ("Expected %d topics for queue %s, got %d" , tt .expectedLen , tt .queueName , len (topics ))
224- }
188+ assert .Len (t , topics , tt .expectedLen , "Expected %d topics for queue %s" , tt .expectedLen , tt .queueName )
225189
226190 // Check if the new topic is in the list
227- found := false
228- for _ , topic := range topics {
229- if topic == tt .topic {
230- found = true
231- break
232- }
233- }
234- if ! found {
235- t .Errorf ("Expected topic %s to be in queue %s" , tt .topic , tt .queueName )
236- }
191+ assert .Contains (t , topics , tt .topic , "Expected topic %s to be in queue %s" , tt .topic , tt .queueName )
237192 })
238193 }
239194}
195+
240196func TestWithShmSize (t * testing.T ) {
241197 tests := []struct {
242198 name string
@@ -266,13 +222,9 @@ func TestWithShmSize(t *testing.T) {
266222 option := WithShmSize (tt .shmSize )
267223
268224 err := option (opts )
269- if err != nil {
270- t .Errorf ("WithShmSize returned error: %v" , err )
271- }
225+ require .NoError (t , err )
272226
273- if opts .shmSize != tt .expected {
274- t .Errorf ("Expected shmSize %d, got %d" , tt .expected , opts .shmSize )
275- }
227+ assert .Equal (t , tt .expected , opts .shmSize )
276228 })
277229 }
278230}
@@ -294,44 +246,25 @@ func TestOptionChaining(t *testing.T) {
294246
295247 for _ , option := range options {
296248 err := option (& opts )
297- if err != nil {
298- t .Errorf ("Option returned error: %v" , err )
299- }
249+ require .NoError (t , err )
300250 }
301251
302252 // Verify all options were applied
303- if opts .username != "newuser" {
304- t .Errorf ("Expected username 'newuser', got %s" , opts .username )
305- }
306- if opts .password != "newpass" {
307- t .Errorf ("Expected password 'newpass', got %s" , opts .password )
308- }
309- if opts .vpn != "testvpn" {
310- t .Errorf ("Expected vpn 'testvpn', got %s" , opts .vpn )
311- }
312- if opts .shmSize != 2 << 30 {
313- t .Errorf ("Expected shmSize %d, got %d" , 2 << 30 , opts .shmSize )
314- }
253+ assert .Equal (t , "newuser" , opts .username )
254+ assert .Equal (t , "newpass" , opts .password )
255+ assert .Equal (t , "testvpn" , opts .vpn )
256+ assert .Equal (t , int64 (2 << 30 ), opts .shmSize )
315257
316258 // Check that services were set correctly
317259 expectedServices := []Service {ServiceAMQP , ServiceMQTT }
318- if len (opts .services ) != len (expectedServices ) {
319- t .Errorf ("Expected %d services, got %d" , len (expectedServices ), len (opts .services ))
320- }
260+ require .Len (t , opts .services , len (expectedServices ))
321261 for i , expectedService := range expectedServices {
322- if opts .services [i ].Name != expectedService .Name || opts .services [i ].Port != expectedService .Port {
323- t .Errorf ("Expected service %s:%d, got %s:%d" , expectedService .Name , expectedService .Port , opts .services [i ].Name , opts .services [i ].Port )
324- }
262+ assert .Equal (t , expectedService .Name , opts .services [i ].Name )
263+ assert .Equal (t , expectedService .Port , opts .services [i ].Port )
325264 }
326265
327266 // Check queues
328- if len (opts .queues ) != 2 {
329- t .Errorf ("Expected 2 queues, got %d" , len (opts .queues ))
330- }
331- if len (opts .queues ["queue1" ]) != 2 {
332- t .Errorf ("Expected 2 topics for queue1, got %d" , len (opts .queues ["queue1" ]))
333- }
334- if len (opts .queues ["queue2" ]) != 1 {
335- t .Errorf ("Expected 1 topic for queue2, got %d" , len (opts .queues ["queue2" ]))
336- }
267+ assert .Len (t , opts .queues , 2 )
268+ assert .Len (t , opts .queues ["queue1" ], 2 )
269+ assert .Len (t , opts .queues ["queue2" ], 1 )
337270}
0 commit comments