Skip to content

Commit 48ad29e

Browse files
committed
chore: improving validation-tests
1 parent 73b7e3a commit 48ad29e

File tree

2 files changed

+115
-121
lines changed

2 files changed

+115
-121
lines changed

src/validation/highscore_test.go

Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -61,52 +61,80 @@ func TestHighscoreCategoryInvalidValueString(t *testing.T) {
6161
func TestHighscoreCategoryFromString(t *testing.T) {
6262
assert := assert.New(t)
6363

64-
assert.Equal(HighScoreExperience, HighscoreCategoryFromString("experience"))
65-
assert.Equal(HighScoreExperience, HighscoreCategoryFromString(""))
66-
67-
assert.Equal(HighScoreAchievements, HighscoreCategoryFromString("achievements"))
68-
assert.Equal(HighScoreAchievements, HighscoreCategoryFromString("achievement"))
69-
70-
assert.Equal(HighScoreAxefighting, HighscoreCategoryFromString("axe"))
71-
assert.Equal(HighScoreAxefighting, HighscoreCategoryFromString("axefighting"))
72-
73-
assert.Equal(HighScoreCharmpoints, HighscoreCategoryFromString("charm"))
74-
assert.Equal(HighScoreCharmpoints, HighscoreCategoryFromString("charms"))
75-
assert.Equal(HighScoreCharmpoints, HighscoreCategoryFromString("charmpoints"))
76-
assert.Equal(HighScoreCharmpoints, HighscoreCategoryFromString("charmspoints"))
77-
78-
assert.Equal(HighScoreClubfighting, HighscoreCategoryFromString("club"))
79-
assert.Equal(HighScoreClubfighting, HighscoreCategoryFromString("clubfighting"))
80-
81-
assert.Equal(HighScoreDistancefighting, HighscoreCategoryFromString("distance"))
82-
assert.Equal(HighScoreDistancefighting, HighscoreCategoryFromString("distancefighting"))
83-
84-
assert.Equal(HighScoreFishing, HighscoreCategoryFromString("fishing"))
85-
86-
assert.Equal(HighScoreFistfighting, HighscoreCategoryFromString("fist"))
87-
assert.Equal(HighScoreFistfighting, HighscoreCategoryFromString("fistfighting"))
88-
89-
assert.Equal(HighScoreGoshnarstaint, HighscoreCategoryFromString("goshnar"))
90-
assert.Equal(HighScoreGoshnarstaint, HighscoreCategoryFromString("goshnars"))
91-
assert.Equal(HighScoreGoshnarstaint, HighscoreCategoryFromString("goshnarstaint"))
92-
93-
assert.Equal(HighScoreLoyaltypoints, HighscoreCategoryFromString("loyalty"))
94-
assert.Equal(HighScoreLoyaltypoints, HighscoreCategoryFromString("loyaltypoints"))
95-
96-
assert.Equal(HighScoreMagiclevel, HighscoreCategoryFromString("magic"))
97-
assert.Equal(HighScoreMagiclevel, HighscoreCategoryFromString("mlvl"))
98-
assert.Equal(HighScoreMagiclevel, HighscoreCategoryFromString("magiclevel"))
99-
100-
assert.Equal(HighScoreShielding, HighscoreCategoryFromString("shielding"))
101-
assert.Equal(HighScoreShielding, HighscoreCategoryFromString("shield"))
102-
103-
assert.Equal(HighScoreSwordfighting, HighscoreCategoryFromString("sword"))
104-
assert.Equal(HighScoreSwordfighting, HighscoreCategoryFromString("swordfighting"))
105-
106-
assert.Equal(HighScoreDromescore, HighscoreCategoryFromString("drome"))
107-
assert.Equal(HighScoreDromescore, HighscoreCategoryFromString("dromescore"))
108-
109-
assert.Equal(HighScoreBosspoints, HighscoreCategoryFromString("boss"))
110-
assert.Equal(HighScoreBosspoints, HighscoreCategoryFromString("bosses"))
111-
assert.Equal(HighScoreBosspoints, HighscoreCategoryFromString("bosspoints"))
64+
categoryTests := map[string]struct {
65+
inputs []string
66+
expected HighscoreCategory
67+
}{
68+
"Experience": {
69+
inputs: []string{"experience", ""},
70+
expected: HighScoreExperience,
71+
},
72+
"Achievements": {
73+
inputs: []string{"achievements", "achievement"},
74+
expected: HighScoreAchievements,
75+
},
76+
"Axefighting": {
77+
inputs: []string{"axe", "axefighting"},
78+
expected: HighScoreAxefighting,
79+
},
80+
"Charmpoints": {
81+
inputs: []string{"charm", "charms", "charmpoints", "charmspoints"},
82+
expected: HighScoreCharmpoints,
83+
},
84+
"Clubfighting": {
85+
inputs: []string{"club", "clubfighting"},
86+
expected: HighScoreClubfighting,
87+
},
88+
"Distancefighting": {
89+
inputs: []string{"distance", "distancefighting"},
90+
expected: HighScoreDistancefighting,
91+
},
92+
"Fishing": {
93+
inputs: []string{"fishing"},
94+
expected: HighScoreFishing,
95+
},
96+
"Fistfighting": {
97+
inputs: []string{"fist", "fistfighting"},
98+
expected: HighScoreFistfighting,
99+
},
100+
"Goshnarstaint": {
101+
inputs: []string{"goshnar", "goshnars", "goshnarstaint"},
102+
expected: HighScoreGoshnarstaint,
103+
},
104+
"Loyaltypoints": {
105+
inputs: []string{"loyalty", "loyaltypoints"},
106+
expected: HighScoreLoyaltypoints,
107+
},
108+
"Magiclevel": {
109+
inputs: []string{"magic", "mlvl", "magiclevel"},
110+
expected: HighScoreMagiclevel,
111+
},
112+
"Shielding": {
113+
inputs: []string{"shielding", "shield"},
114+
expected: HighScoreShielding,
115+
},
116+
"Swordfighting": {
117+
inputs: []string{"sword", "swordfighting"},
118+
expected: HighScoreSwordfighting,
119+
},
120+
"Dromescore": {
121+
inputs: []string{"drome", "dromescore"},
122+
expected: HighScoreDromescore,
123+
},
124+
"Bosspoints": {
125+
inputs: []string{"boss", "bosses", "bosspoints"},
126+
expected: HighScoreBosspoints,
127+
},
128+
}
129+
130+
for category, data := range categoryTests {
131+
t.Run(category, func(t *testing.T) {
132+
for _, input := range data.inputs {
133+
t.Run(input, func(t *testing.T) {
134+
result := HighscoreCategoryFromString(input)
135+
assert.Equal(data.expected, result, "unexpected result for input: %s", input)
136+
})
137+
}
138+
})
139+
}
112140
}

src/validation/tibia_test.go

Lines changed: 39 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package validation
22

3-
import "testing"
3+
import (
4+
"fmt"
5+
"testing"
6+
)
47

58
func TestTownExists(t *testing.T) {
69
if !initiated {
@@ -10,94 +13,57 @@ func TestTownExists(t *testing.T) {
1013
}
1114
}
1215

13-
tests := []struct {
14-
name string
15-
town string
16+
// Define test data with expected results
17+
testData := map[string]struct {
18+
towns []string
1619
want bool
1720
wantErr bool
1821
}{
19-
{
20-
name: "empty",
21-
town: "",
22+
"empty": {
23+
towns: []string{""},
2224
want: false,
2325
wantErr: false,
24-
}, {
25-
name: "unknown",
26-
town: "anything",
26+
},
27+
"unknown": {
28+
towns: []string{"anything"},
2729
want: false,
2830
wantErr: false,
29-
}, {
30-
name: "carlin lower case",
31-
town: "carlin",
32-
want: true,
33-
wantErr: false,
34-
}, {
35-
name: "carlin upper case",
36-
town: "CARLIN",
37-
want: true,
38-
wantErr: false,
39-
}, {
40-
name: "carlin mixed case",
41-
town: "CaRlIn",
42-
want: true,
43-
wantErr: false,
44-
}, {
45-
name: "ab'dendriel lower case",
46-
town: "ab'dendriel",
47-
want: true,
48-
wantErr: false,
49-
}, {
50-
name: "ab'dendriel upper case",
51-
town: "AB'DENDRIEL",
52-
want: true,
53-
wantErr: false,
54-
}, {
55-
name: "ab'dendriel mixed case",
56-
town: "Ab'DeNdRiEl",
57-
want: true,
58-
wantErr: false,
59-
}, {
60-
name: "port hope lower case",
61-
town: "port hope",
62-
want: true,
63-
wantErr: false,
64-
}, {
65-
name: "port hope upper case",
66-
town: "PORT HOPE",
67-
want: true,
68-
wantErr: false,
69-
}, {
70-
name: "port hope mixed case",
71-
town: "PoRt HoPe",
31+
},
32+
"carlin": {
33+
towns: []string{"carlin", "CARLIN", "CaRlIn"},
7234
want: true,
7335
wantErr: false,
74-
}, {
75-
name: "port hope lower case with '+'",
76-
town: "port+hope",
36+
},
37+
"ab'dendriel": {
38+
towns: []string{"ab'dendriel", "AB'DENDRIEL", "Ab'DeNdRiEl"},
7739
want: true,
7840
wantErr: false,
79-
}, {
80-
name: "port hope upper case with '+'",
81-
town: "PORT+HOPE",
41+
},
42+
"port hope": {
43+
towns: []string{"port hope", "PORT HOPE", "PoRt HoPe"},
8244
want: true,
8345
wantErr: false,
84-
}, {
85-
name: "port hope mixed case with '+'",
86-
town: "PoRt+HoPe",
46+
},
47+
"port hope with +": {
48+
towns: []string{"port+hope", "PORT+HOPE", "PoRt+HoPe"},
8749
want: true,
8850
wantErr: false,
8951
},
9052
}
91-
for _, tt := range tests {
92-
t.Run(tt.name, func(t *testing.T) {
93-
got, err := TownExists(tt.town)
94-
if (err != nil) != tt.wantErr {
95-
t.Errorf("TownExists() error = %v, wantErr %v", err, tt.wantErr)
96-
return
97-
}
98-
if got != tt.want {
99-
t.Errorf("TownExists() = %v, want %v", got, tt.want)
100-
}
101-
})
53+
54+
// Iterate over test data and run subtests
55+
for name, data := range testData {
56+
for _, town := range data.towns {
57+
t.Run(fmt.Sprintf("%s (%s)", name, town), func(t *testing.T) {
58+
got, err := TownExists(town)
59+
if (err != nil) != data.wantErr {
60+
t.Errorf("TownExists() error = %v, wantErr %v", err, data.wantErr)
61+
return
62+
}
63+
if got != data.want {
64+
t.Errorf("TownExists() = %v, want %v", got, data.want)
65+
}
66+
})
67+
}
10268
}
10369
}

0 commit comments

Comments
 (0)