Skip to content

Commit 45906ed

Browse files
committed
sendtables: fix Entity.PropertyValueMust() + add tests
1 parent 7a65748 commit 45906ed

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

pkg/demoinfocs/sendtables/entity.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ func (e *entity) Properties() (out []Property) {
4242
}
4343

4444
func (e *entity) property(name string) *property {
45-
return &e.props[e.serverClass.propNameToIndex[name]]
45+
i, ok := e.serverClass.propNameToIndex[name]
46+
if !ok {
47+
return nil
48+
}
49+
50+
return &e.props[i]
4651
}
4752

4853
// Property finds a property on the entity by name.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package sendtables
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
var testData = struct {
10+
entity entity
11+
}{
12+
entity: entity{
13+
props: []property{
14+
{value: PropertyValue{IntVal: 10}},
15+
{value: PropertyValue{IntVal: 20}},
16+
{value: PropertyValue{IntVal: 30}},
17+
},
18+
serverClass: &ServerClass{propNameToIndex: map[string]int{
19+
"myProp": 0,
20+
"test": 1,
21+
"anotherOne": 2,
22+
}},
23+
},
24+
}
25+
26+
func TestEntity_Properties(t *testing.T) {
27+
ent := entity{props: []property{{value: PropertyValue{IntVal: 1}}}}
28+
29+
assert.Equal(t, &ent.props[0], ent.Properties()[0])
30+
}
31+
32+
func TestEntity_ServerClass(t *testing.T) {
33+
assert.Equal(t, testData.entity.serverClass, testData.entity.ServerClass())
34+
}
35+
36+
func TestEntity_Property(t *testing.T) {
37+
assert.Equal(t, &testData.entity.props[1], testData.entity.Property("test"))
38+
}
39+
40+
func TestEntity_Property_Nil(t *testing.T) {
41+
assert.Nil(t, testData.entity.Property("not_found"))
42+
}
43+
44+
func TestEntity_Property_Value(t *testing.T) {
45+
val, ok := testData.entity.PropertyValue("test")
46+
47+
assert.True(t, ok)
48+
assert.Equal(t, PropertyValue{IntVal: 20}, val)
49+
}
50+
51+
func TestEntity_PropertyValue_NotFound(t *testing.T) {
52+
val, ok := testData.entity.PropertyValue("not_found")
53+
54+
assert.False(t, ok)
55+
assert.Empty(t, val)
56+
}
57+
58+
func TestEntity_PropertyValueMust_NotFound_Panics(t *testing.T) {
59+
f := func() {
60+
testData.entity.PropertyValueMust("not_found")
61+
}
62+
63+
assert.Panics(t, f)
64+
}
65+
66+
func TestProperty_Name(t *testing.T) {
67+
prop := property{entry: &flattenedPropEntry{name: "test"}}
68+
69+
assert.Equal(t, "test", prop.Name())
70+
}

pkg/demoinfocs/sendtables/propdecoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type PropertyValue struct {
8080
FloatVal float32
8181
}
8282

83-
// BoolVal returns true if IntVal == 1.
83+
// BoolVal returns true if IntVal > 0.
8484
func (v PropertyValue) BoolVal() bool {
8585
return v.IntVal > 0
8686
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package sendtables
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestPropertyValue_BoolVal(t *testing.T) {
10+
assert.True(t, PropertyValue{IntVal: 1}.BoolVal())
11+
assert.False(t, PropertyValue{IntVal: 0}.BoolVal())
12+
}
13+
14+
func TestDecodeProp_UnknownType(t *testing.T) {
15+
prop := &property{entry: &flattenedPropEntry{prop: &sendTableProperty{rawType: -1}}}
16+
17+
f := func() {
18+
propDecoder.decodeProp(prop, nil)
19+
}
20+
21+
assert.Panics(t, f)
22+
}

0 commit comments

Comments
 (0)