This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmarshal_test.go
107 lines (86 loc) · 3.13 KB
/
marshal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package csminify_test
import (
"bytes"
"encoding/json"
"io"
"os"
"testing"
"github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/events"
"github.com/stretchr/testify/assert"
"gopkg.in/vmihailenco/msgpack.v2"
min "github.com/markus-wa/cs-demo-minifier"
"github.com/markus-wa/cs-demo-minifier/protobuf"
rep "github.com/markus-wa/cs-demo-minifier/replay"
)
// Test data preservation of JSON marshalling & unmarshalling with a 'non-default' replay.
func TestJSONNonDefault(t *testing.T) {
testDataPreservation(nonDefaultReplay, marshalJSON, unmarshalJSON, t)
}
// Test data preservation of JSON marshalling & unmarshalling with a real, parsed replay.
func TestJSONDemo(t *testing.T) {
testDataPreservation(parsedReplay, marshalJSON, unmarshalJSON, t)
}
func marshalJSON(r rep.Replay, w io.Writer) error {
return json.NewEncoder(w).Encode(r)
}
func unmarshalJSON(r io.Reader, rp *rep.Replay) error {
return json.NewDecoder(r).Decode(rp)
}
// Test data preservation of MessagePack marshalling & unmarshalling with a 'non-default' replay.
func TestMsgPackNonDefault(t *testing.T) {
testDataPreservation(nonDefaultReplay, marshalMsgPack, unmarshalMsgPack, t)
}
// Test data preservation of MessagePack marshalling & unmarshalling with a real, parsed replay.
func TestMsgPackDemo(t *testing.T) {
testDataPreservation(parsedReplay, marshalMsgPack, unmarshalMsgPack, t)
}
func marshalMsgPack(r rep.Replay, w io.Writer) error {
return msgpack.NewEncoder(w).Encode(r)
}
func unmarshalMsgPack(r io.Reader, rp *rep.Replay) error {
return msgpack.NewDecoder(r).Decode(rp)
}
// Test data preservation of Protobuf marshalling & unmarshalling with a 'non-default' replay.
func TestProtobufNonDefault(t *testing.T) {
testDataPreservation(nonDefaultReplay, protobuf.MarshalReplay, protobuf.UnmarshalReplay, t)
}
// Test data preservation of Protobuf marshalling & unmarshalling with a real, parsed replay.
func TestProtobufDemo(t *testing.T) {
testDataPreservation(parsedReplay, protobuf.MarshalReplay, protobuf.UnmarshalReplay, t)
}
// Test data preservation of Protobuf marshalling & unmarshalling with a custom events & attributes.
func TestProtobufCustomEvents(t *testing.T) {
f, err := os.Open(demPath)
if err != nil {
panic(err.Error())
}
defer f.Close()
ec := new(min.EventCollector)
ec.AddHandler(func(events.BombPlanted) {
// Test access to parser
ec.Parser().GameState().IngameTick()
ec.AddEvent(rep.Event{
Name: "custom_plant_event",
Attributes: []rep.EventAttribute{{Key: "custom_attribute", StrVal: "test"}},
})
})
customReplay, err := min.ToReplayWithConfig(f, min.ReplayConfig{SnapshotFrequency: 0.5, EventCollector: ec})
if err != nil {
panic(err.Error())
}
testDataPreservation(customReplay, protobuf.MarshalReplay, protobuf.UnmarshalReplay, t)
}
type replayUnmarshaller func(io.Reader, *rep.Replay) error
func testDataPreservation(replay rep.Replay, marshal min.ReplayMarshaller, unmarshal replayUnmarshaller, t *testing.T) {
buf := new(bytes.Buffer)
err := marshal(replay, buf)
if err != nil {
t.Fatal(err)
}
var r rep.Replay
err = unmarshal(buf, &r)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, replay, r)
}