Skip to content

Commit 4973837

Browse files
authored
Add some 'fake' unit tests to up coverage on webserver.go (#71)
1 parent ce26e50 commit 4973837

File tree

6 files changed

+108
-22
lines changed

6 files changed

+108
-22
lines changed

src/TibiaCreaturesCreatureV3.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TibiaCreaturesCreatureV3Impl(race string, BoxContentHTML string) CreatureRe
6868

6969
// Preparing vars
7070
var (
71-
CreatureDescription, CreatureBehaviour string
71+
CreatureName, CreatureImageURL, CreatureDescription, CreatureBehaviour string
7272
CreatureLootList, CreatureImmuneTo, CreatureStrongAgainst, CreatureWeaknessAgainst []string
7373
CreatureHitpoints, CreatureSummonedMana, CreatureConvincedMana, CreatureExperiencePoints int
7474
CreatureBeParalysed, CreatureBeSummoned, CreatureBeConvinced, CreatureSeeInvisible, CreatureIsLootable, CreatureIsBoosted bool
@@ -85,6 +85,8 @@ func TibiaCreaturesCreatureV3Impl(race string, BoxContentHTML string) CreatureRe
8585

8686
// Preparing data for JSONData
8787
if len(subma1) > 0 {
88+
CreatureName = TibiaDataSanitizeEscapedString(subma1[0][1])
89+
CreatureImageURL = subma1[0][2]
8890

8991
// Description (and unescape hmtl string)
9092
CreatureDescription = strings.ReplaceAll(subma1[0][3], "<br/>", "\n")
@@ -147,13 +149,12 @@ func TibiaCreaturesCreatureV3Impl(race string, BoxContentHTML string) CreatureRe
147149
}
148150
}
149151

150-
//
151152
// Build the data-blob
152153
return CreatureResponse{
153154
Creature{
154-
Name: TibiaDataSanitizeEscapedString(subma1[0][1]),
155+
Name: CreatureName,
155156
Race: race,
156-
ImageURL: subma1[0][2],
157+
ImageURL: CreatureImageURL,
157158
Description: CreatureDescription,
158159
Behaviour: CreatureBehaviour,
159160
Hitpoints: CreatureHitpoints,

src/TibiaCreaturesOverviewV3.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ var (
3636
)
3737

3838
func TibiaCreaturesOverviewV3Impl(BoxContentHTML string) CreaturesOverviewResponse {
39+
var (
40+
BoostedCreatureName, BoostedCreatureRace, BoostedCreatureImage string
41+
)
42+
3943
// Loading HTML data into ReaderHTML for goquery with NewReader
4044
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
4145
if err != nil {
@@ -50,14 +54,20 @@ func TibiaCreaturesOverviewV3Impl(BoxContentHTML string) CreaturesOverviewRespon
5054

5155
// Regex to get data for name and race param for boosted creature
5256
subma1b := BoostedCreatureNameAndRaceRegex.FindAllStringSubmatch(InnerTableContainerTMPB, -1)
53-
// Settings vars for usage in JSONData
54-
BoostedCreatureName := subma1b[0][2]
55-
BoostedCreatureRace := subma1b[0][1]
57+
58+
if len(subma1b) > 0 {
59+
// Settings vars for usage in JSONData
60+
BoostedCreatureName = subma1b[0][2]
61+
BoostedCreatureRace = subma1b[0][1]
62+
}
5663

5764
// Regex to get image of boosted creature
5865
subma2b := BoostedCreatureImageRegex.FindAllStringSubmatch(InnerTableContainerTMPB, -1)
59-
// Settings vars for usage in JSONData
60-
BoostedCreatureImage := subma2b[0][1]
66+
67+
if len(subma2b) > 0 {
68+
// Settings vars for usage in JSONData
69+
BoostedCreatureImage = subma2b[0][1]
70+
}
6171

6272
// Creating empty CreaturesData var
6373
var CreaturesData []OverviewCreature

src/TibiaGuildsGuildV3.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ func TibiaGuildsGuildV3Impl(guild string, BoxContentHTML string) GuildResponse {
106106
if err != nil {
107107
log.Fatal(err)
108108
}
109+
109110
subma1b := GuildLogoRegex.FindAllStringSubmatch(InnerTableContainerTMPA, -1)
110-
GuildLogoURL = subma1b[0][1]
111+
112+
if len(subma1b) > 0 {
113+
GuildLogoURL = subma1b[0][1]
114+
}
111115

112116
// Getting data from div.InnerTableContainer and then first p
113117
InnerTableContainerTMPB, err := ReaderHTML.Find("#GuildInformationContainer").Html()

src/TibiaHighscoresV3.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ func TibiaHighscoresV3Impl(world string, category HighscoreCategory, vocationNam
5757

5858
// getting age of data
5959
subma1 := HighscoresAgeRegex.FindAllStringSubmatch(string(BoxContentHTML), 1)
60-
HighscoreAge = TibiadataStringToIntegerV3(subma1[0][1])
60+
61+
if len(subma1) > 0 {
62+
HighscoreAge = TibiadataStringToIntegerV3(subma1[0][1])
63+
}
6164

6265
// Running query over each div
6366
ReaderHTML.Find(".TableContent tr").First().NextAll().Each(func(index int, s *goquery.Selection) {

src/webserver.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -501,17 +501,19 @@ func tibiaNewslistV3(c *gin.Context) {
501501
},
502502
}
503503

504-
// getting type of news list
505-
switch tmp := strings.Split(c.Request.URL.Path, "/"); tmp[3] {
506-
case "newsticker":
507-
tibiadataRequest.FormData["filter_ticker"] = "ticker"
508-
case "latest":
509-
tibiadataRequest.FormData["filter_article"] = "article"
510-
tibiadataRequest.FormData["filter_news"] = "news"
511-
case "archive":
512-
tibiadataRequest.FormData["filter_ticker"] = "ticker"
513-
tibiadataRequest.FormData["filter_article"] = "article"
514-
tibiadataRequest.FormData["filter_news"] = "news"
504+
if c.Request != nil {
505+
// getting type of news list
506+
switch tmp := strings.Split(c.Request.URL.Path, "/"); tmp[3] {
507+
case "newsticker":
508+
tibiadataRequest.FormData["filter_ticker"] = "ticker"
509+
case "latest":
510+
tibiadataRequest.FormData["filter_article"] = "article"
511+
tibiadataRequest.FormData["filter_news"] = "news"
512+
case "archive":
513+
tibiadataRequest.FormData["filter_ticker"] = "ticker"
514+
tibiadataRequest.FormData["filter_article"] = "article"
515+
tibiadataRequest.FormData["filter_news"] = "news"
516+
}
515517
}
516518

517519
tibiaDataRequestHandler(

src/webserver_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"net/http/httptest"
5+
"testing"
6+
7+
"github.com/gin-gonic/gin"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestFakeToUpCodeCoverage(t *testing.T) {
12+
gin.SetMode(gin.TestMode)
13+
14+
w := httptest.NewRecorder()
15+
c, _ := gin.CreateTestContext(w)
16+
17+
assert := assert.New(t)
18+
19+
tibiaCharactersCharacterV3(c)
20+
assert.Equal(200, w.Code)
21+
22+
tibiaCreaturesOverviewV3(c)
23+
assert.Equal(200, w.Code)
24+
25+
tibiaCreaturesCreatureV3(c)
26+
assert.Equal(200, w.Code)
27+
28+
tibiaFansitesV3(c)
29+
assert.Equal(200, w.Code)
30+
31+
tibiaGuildsGuildV3(c)
32+
assert.Equal(200, w.Code)
33+
34+
tibiaGuildsOverviewV3(c)
35+
assert.Equal(200, w.Code)
36+
37+
tibiaHighscoresV3(c)
38+
assert.Equal(200, w.Code)
39+
40+
tibiaHousesHouseV3(c)
41+
assert.Equal(200, w.Code)
42+
43+
tibiaHousesOverviewV3(c)
44+
assert.Equal(200, w.Code)
45+
46+
tibiaKillstatisticsV3(c)
47+
assert.Equal(200, w.Code)
48+
49+
tibiaNewslistV3(c)
50+
assert.Equal(200, w.Code)
51+
52+
tibiaNewsV3(c)
53+
assert.Equal(200, w.Code)
54+
55+
tibiaSpellsOverviewV3(c)
56+
assert.Equal(200, w.Code)
57+
58+
tibiaSpellsSpellV3(c)
59+
assert.Equal(200, w.Code)
60+
61+
tibiaWorldsOverviewV3(c)
62+
assert.Equal(200, w.Code)
63+
64+
tibiaWorldsWorldV3(c)
65+
assert.Equal(200, w.Code)
66+
}

0 commit comments

Comments
 (0)