@@ -114,10 +114,11 @@ func TestAbilityApiCall(t *testing.T) {
114114 }))
115115 defer ts .Close ()
116116
117- ability , _ , err := AbilityApiCall ("/ability" , "non-existent-ability" , ts .URL )
117+ ability , name , err := AbilityApiCall ("/ability" , "non-existent-ability" , ts .URL )
118118
119119 require .Error (t , err , "Expected an error for invalid ability" )
120120 assert .Equal (t , structs.AbilityJSONStruct {}, ability , "Expected empty ability struct on error" )
121+ assert .Equal (t , "" , name , "Expected empty name string on error" )
121122
122123 assert .Contains (t , err .Error (), "Ability not found" , "Expected 'Ability not found' in error message" )
123124 assert .Contains (t , err .Error (), "Perhaps a typo?" , "Expected helpful suggestion in error message" )
@@ -151,10 +152,11 @@ func TestItemApiCall(t *testing.T) {
151152 }))
152153 defer ts .Close ()
153154
154- item , _ , err := ItemApiCall ("/item" , "non-existent-item" , ts .URL )
155+ item , name , err := ItemApiCall ("/item" , "non-existent-item" , ts .URL )
155156
156157 require .Error (t , err , "Expected an error for invalid item" )
157158 assert .Equal (t , structs.ItemJSONStruct {}, item , "Expected empty item struct on error" )
159+ assert .Equal (t , "" , name , "Expected empty name string on error" )
158160
159161 assert .Contains (t , err .Error (), "Item not found" , "Expected 'Item not found' in error message" )
160162 assert .Contains (t , err .Error (), "Perhaps a typo?" , "Expected helpful suggestion in error message" )
@@ -188,10 +190,11 @@ func TestMoveApiCall(t *testing.T) {
188190 }))
189191 defer ts .Close ()
190192
191- move , _ , err := MoveApiCall ("/move" , "non-existent-move" , ts .URL )
193+ move , name , err := MoveApiCall ("/move" , "non-existent-move" , ts .URL )
192194
193195 require .Error (t , err , "Expected an error for invalid move" )
194196 assert .Equal (t , structs.MoveJSONStruct {}, move , "Expected empty move struct on error" )
197+ assert .Equal (t , "" , name , "Expected empty name string on error" )
195198
196199 assert .Contains (t , err .Error (), "Move not found" , "Expected 'Move not found' in error message" )
197200 assert .Contains (t , err .Error (), "Perhaps a typo?" , "Expected helpful suggestion in error message" )
@@ -225,10 +228,11 @@ func TestPokemonApiCall(t *testing.T) {
225228 }))
226229 defer ts .Close ()
227230
228- pokemon , _ , err := PokemonApiCall ("/pokemon" , "non-existent-pokemon" , ts .URL )
231+ pokemon , name , err := PokemonApiCall ("/pokemon" , "non-existent-pokemon" , ts .URL )
229232
230233 require .Error (t , err , "Expected an error for invalid pokemon" )
231234 assert .Equal (t , structs.PokemonJSONStruct {}, pokemon , "Expected empty pokemon struct on error" )
235+ assert .Equal (t , "" , name , "Expected empty name string on error" )
232236
233237 assert .Contains (t , err .Error (), "Pokémon not found" , "Expected 'Pokémon not found' in error message" )
234238 assert .Contains (t , err .Error (), "Perhaps a typo?" , "Expected helpful suggestion in error message" )
@@ -237,40 +241,58 @@ func TestPokemonApiCall(t *testing.T) {
237241
238242// TestTypesApiCall - Test for the TypesApiCall function
239243func TestTypesApiCall (t * testing.T ) {
240- expectedTypes := structs.TypesJSONStruct {
241- Name : "electric" ,
242- ID : 13 ,
243- Pokemon : []struct {
244- Pokemon struct {
245- Name string `json:"name"`
246- URL string `json:"url"`
247- } `json:"pokemon"`
248- Slot int `json:"slot"`
249- }{
250- {Pokemon : struct {
251- Name string `json:"name"`
252- URL string `json:"url"`
253- }{Name : "pikachu" , URL : "https://pokeapi.co/api/v2/pokemon/25/" },
254- Slot : 1 },
255- },
256- }
244+ t .Run ("Successful API call returns expected type" , func (t * testing.T ) {
245+ expectedTypes := structs.TypesJSONStruct {
246+ Name : "electric" ,
247+ ID : 13 ,
248+ Pokemon : []struct {
249+ Pokemon struct {
250+ Name string `json:"name"`
251+ URL string `json:"url"`
252+ } `json:"pokemon"`
253+ Slot int `json:"slot"`
254+ }{
255+ {Pokemon : struct {
256+ Name string `json:"name"`
257+ URL string `json:"url"`
258+ }{Name : "pikachu" , URL : "https://pokeapi.co/api/v2/pokemon/25/" },
259+ Slot : 1 },
260+ },
261+ }
257262
258- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
259- w .WriteHeader (http .StatusOK )
260- err := json .NewEncoder (w ).Encode (expectedTypes )
261- assert .NoError (t , err , "Expected no error for skipHTTPSCheck" )
262- }))
263- defer ts .Close ()
263+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
264+ w .WriteHeader (http .StatusOK )
265+ err := json .NewEncoder (w ).Encode (expectedTypes )
266+ assert .NoError (t , err , "Expected no error for encoding response" )
267+ }))
268+ defer ts .Close ()
269+
270+ typesStruct , name , err := TypesApiCall ("/type" , "electric" , ts .URL )
271+
272+ require .NoError (t , err , "Expected no error on successful API call" )
273+ assert .Equal (t , expectedTypes , typesStruct , "Expected types struct does not match" )
274+ assert .Equal (t , "electric" , name , "Expected type name does not match" )
275+ })
264276
265- typesStruct , name , id := TypesApiCall ("/type" , "electric" , ts .URL )
277+ t .Run ("Failed API call returns styled error" , func (t * testing.T ) {
278+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
279+ // Simulate API failure (e.g., 404 Not Found)
280+ http .Error (w , "Not Found" , http .StatusNotFound )
281+ }))
282+ defer ts .Close ()
283+
284+ typesStruct , name , err := TypesApiCall ("/type" , "non-existent-type" , ts .URL )
285+
286+ require .Error (t , err , "Expected an error for invalid type" )
287+ assert .Equal (t , structs.TypesJSONStruct {}, typesStruct , "Expected empty types struct on error" )
288+ assert .Equal (t , "" , name , "Expected empty name string on error" )
266289
267- assert .Equal (t , expectedTypes , typesStruct )
268- assert .Equal (t , "electric " , name )
269- assert . Equal ( t , 13 , id )
290+ assert .Contains (t , err . Error (), "Type not found" , "Expected 'Type not found' in error message" )
291+ assert .Contains (t , err . Error (), "Perhaps a typo? " , "Expected helpful suggestion in error message" )
292+ } )
270293}
271294
272295func TestPokemonSpeciesApiCall (t * testing.T ) {
273- // Successful API call returns expected species data
274296 t .Run ("Successful API call returns expected species" , func (t * testing.T ) {
275297 expectedSpecies := structs.PokemonSpeciesJSONStruct {
276298 Name : "flareon" ,
@@ -283,26 +305,27 @@ func TestPokemonSpeciesApiCall(t *testing.T) {
283305 }))
284306 defer ts .Close ()
285307
286- species , err := PokemonSpeciesApiCall ("/pokemon-species" , "flareon" , ts .URL )
308+ species , name , err := PokemonSpeciesApiCall ("/pokemon-species" , "flareon" , ts .URL )
287309
288310 require .NoError (t , err , "Expected no error on successful API call" )
289311 assert .Equal (t , expectedSpecies , species , "Expected species struct does not match" )
312+ assert .Equal (t , "flareon" , name , "Expected species name does not match" )
290313 })
291314
292- // Failed API call returns styled error
293315 t .Run ("Failed API call returns styled error" , func (t * testing.T ) {
294316 ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
295317 // Simulate API failure (e.g., 404 Not Found)
296318 http .Error (w , "Not Found" , http .StatusNotFound )
297319 }))
298320 defer ts .Close ()
299321
300- species , err := PokemonSpeciesApiCall ("/pokemon-species" , "non-existent-species" , ts .URL )
322+ species , name , err := PokemonSpeciesApiCall ("/pokemon-species" , "non-existent-species" , ts .URL )
301323
302324 require .Error (t , err , "Expected an error for invalid species" )
303325 assert .Equal (t , structs.PokemonSpeciesJSONStruct {}, species , "Expected empty species struct on error" )
326+ assert .Equal (t , "" , name , "Expected empty name string on error" )
304327
305- assert .Contains (t , err .Error (), "Pokémon not found" , "Expected 'Pokémon not found' in error message" )
328+ assert .Contains (t , err .Error (), "PokémonSpecies not found" , "Expected 'PokémonSpecies not found' in error message" )
306329 assert .Contains (t , err .Error (), "Perhaps a typo?" , "Expected helpful suggestion in error message" )
307330 })
308331}
0 commit comments