Skip to content

Commit 1d88fbc

Browse files
committed
common: fix ZoomLevel constants + add more tests
1 parent b1eb3c4 commit 1d88fbc

File tree

4 files changed

+104
-6
lines changed

4 files changed

+104
-6
lines changed

pkg/demoinfocs/common/common_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func entityWithProperty(propName string, value st.PropertyValue) *stfake.Entity
188188
prop.On("Value").Return(value)
189189

190190
entity.On("Property", propName).Return(prop)
191-
entity.On("PropertyValue", propName).Return(prop, true)
191+
entity.On("PropertyValue", propName).Return(value, true)
192192
entity.On("PropertyValueMust", propName).Return(value)
193193

194194
return entity
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package common
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestGetFloat_Nil(t *testing.T) {
10+
assert.Zero(t, getFloat(nil, "test"))
11+
}
12+
13+
func TestGetInt_Nil(t *testing.T) {
14+
assert.Zero(t, getInt(nil, "test"))
15+
}
16+
17+
func TestGetString_Nil(t *testing.T) {
18+
assert.Empty(t, getString(nil, "test"))
19+
}
20+
21+
func TestGetBool_Nil(t *testing.T) {
22+
assert.Empty(t, getBool(nil, "test"))
23+
}

pkg/demoinfocs/common/equipment.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ func MapEquipment(eqName string) EquipmentType {
273273
type ZoomLevel int
274274

275275
const (
276-
ZoomNone = 0
277-
ZoomHalf = 1
278-
ZoomFull = 2
276+
ZoomNone ZoomLevel = 0
277+
ZoomHalf ZoomLevel = 1
278+
ZoomFull ZoomLevel = 2
279279
)
280280

281281
// Equipment is a weapon / piece of equipment belonging to a player.
@@ -315,12 +315,17 @@ func (e Equipment) AmmoInMagazine() int {
315315
return 1
316316
}
317317

318+
if e.Entity == nil {
319+
return 0
320+
}
321+
318322
val, ok := e.Entity.PropertyValue("m_iClip1")
319323
if !ok {
320324
return -1
321325
}
322326

323-
return val.IntVal
327+
// need to subtract 1 as m_iClip1 is nrOfBullets + 1
328+
return val.IntVal - 1
324329
}
325330

326331
// AmmoType returns the weapon's ammo type, mostly (only?) relevant for grenades.
@@ -354,6 +359,10 @@ func (e Equipment) AmmoReserve() int {
354359
return 0
355360
}
356361

362+
if e.Entity == nil {
363+
return 0
364+
}
365+
357366
// if the property doesn't exist we return 0 by default
358367
val, _ := e.Entity.PropertyValue("m_iPrimaryReserveAmmoCount")
359368

pkg/demoinfocs/common/equipment_test.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ func TestEquipment_UniqueID(t *testing.T) {
3838
assert.NotEqual(t, NewEquipment(EqAK47).UniqueID(), NewEquipment(EqAK47).UniqueID(), "UniqueIDs of different equipment instances should be different")
3939
}
4040

41+
func TestEquipment_AmmoInMagazine(t *testing.T) {
42+
wep := &Equipment{
43+
Type: EqAK47,
44+
Entity: entityWithProperty("m_iClip1", st.PropertyValue{IntVal: 31}),
45+
}
46+
47+
// returned value should be minus 1, m_iClip1 is always 1 more than the actual number of bullets
48+
assert.Equal(t, 30, wep.AmmoInMagazine())
49+
}
50+
51+
func TestEquipment_AmmoInMagazine_NotFound(t *testing.T) {
52+
entity := entityWithID(1)
53+
entity.On("PropertyValue", "m_iClip1").Return(st.PropertyValue{}, false)
54+
55+
wep := &Equipment{
56+
Type: EqAK47,
57+
Entity: entity,
58+
}
59+
60+
// returned value should be minus 1, m_iClip1 is always 1 more than the actual number of bullets
61+
assert.Equal(t, -1, wep.AmmoInMagazine())
62+
}
63+
4164
func TestEquipment_AmmoInMagazine_Grenade(t *testing.T) {
4265
wep := &Equipment{
4366
Type: EqFlash,
@@ -46,6 +69,24 @@ func TestEquipment_AmmoInMagazine_Grenade(t *testing.T) {
4669
assert.Equal(t, 1, wep.AmmoInMagazine())
4770
}
4871

72+
func TestEquipment_AmmoInMagazine_EntityNil(t *testing.T) {
73+
wep := &Equipment{
74+
Type: EqAK47,
75+
}
76+
77+
assert.Equal(t, 0, wep.AmmoInMagazine())
78+
}
79+
80+
func TestEquipment_AmmoReserve(t *testing.T) {
81+
entity := entityWithProperty("m_iPrimaryReserveAmmoCount", st.PropertyValue{IntVal: 60})
82+
wep := &Equipment{
83+
Type: EqAK47,
84+
Entity: entity,
85+
}
86+
87+
assert.Equal(t, 60, wep.AmmoReserve())
88+
}
89+
4990
func TestEquipment_AmmoReserve_Grenade(t *testing.T) {
5091
owner := new(Player)
5192
owner.AmmoLeft[1] = 2
@@ -60,14 +101,39 @@ func TestEquipment_AmmoReserve_Grenade(t *testing.T) {
60101
assert.Equal(t, 1, wep.AmmoReserve())
61102
}
62103

63-
func TestEquipment_AmmoReserve2_Grenade_OwnerNil(t *testing.T) {
104+
func TestEquipment_AmmoReserve_Grenade_OwnerNil(t *testing.T) {
64105
wep := &Equipment{
65106
Type: EqFlash,
66107
}
67108

68109
assert.Equal(t, 0, wep.AmmoReserve())
69110
}
70111

112+
func TestEquipment_AmmoReserve_EntityNil(t *testing.T) {
113+
wep := &Equipment{
114+
Type: EqAK47,
115+
}
116+
117+
assert.Equal(t, 0, wep.AmmoReserve())
118+
}
119+
120+
func TestEquipment_ZoomLevel(t *testing.T) {
121+
wep := &Equipment{
122+
Type: EqAK47,
123+
Entity: entityWithProperty("m_zoomLevel", st.PropertyValue{IntVal: 2}),
124+
}
125+
126+
assert.Equal(t, ZoomFull, wep.ZoomLevel())
127+
}
128+
129+
func TestEquipment_ZoomLevel_EntityNil(t *testing.T) {
130+
wep := &Equipment{
131+
Type: EqAK47,
132+
}
133+
134+
assert.Equal(t, ZoomLevel(0), wep.ZoomLevel())
135+
}
136+
71137
func TestEquipmentAlternative(t *testing.T) {
72138
assert.Equal(t, EqUSP, EquipmentAlternative(EqP2000))
73139
assert.Equal(t, EqCZ, EquipmentAlternative(EqP250))

0 commit comments

Comments
 (0)