Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-wa committed Nov 17, 2019
1 parent 0108149 commit aef755a
Show file tree
Hide file tree
Showing 7 changed files with 978 additions and 21 deletions.
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ deploy:
tags: true
condition: $TRAVIS_GO_VERSION =~ ^1\.11

- provider: releases
api_key: $GITHUB_TOKEN
file:
- test/results/example.json
- test/results/example.mp
- test/results/example.pb
skip_cleanup: true
on:
tags: true

notifications:
webhooks:
urls:
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ Usage of csminify:
Output file path (default stdout)
```

#### Supported Formats
### Supported Formats

| Format | Command Line (`-format` Flag) | Structure |
| --- | --- | --- |
| JSON | `json` | [schema.json](schema.json) |
| MessagePack | `msgpack`, `mp` | [schema.json](schema.json) |
| Protocol Buffers | `protobuf`, `proto`, `pb` | [replay.proto](protobuf/gen/proto/replay.proto) |
| Format | Command Line (`-format` Flag) | Structure | Minimal Example | Full Example |
| --- | --- | --- | --- | --- |
| JSON | `json` | [schema.json](schema.json) | [minimal.json](examples/minimal.json) | see [releases](https://github.com/markus-wa/cs-demo-minifier/releases) page |
| MessagePack | `msgpack`, `mp` | [schema.json](schema.json) | [minimal.mp](examples/minimal.mp) | see [releases](https://github.com/markus-wa/cs-demo-minifier/releases) page |
| Protocol Buffers | `protobuf`, `proto`, `pb` | [replay.proto](protobuf/gen/proto/replay.proto) | [minimal.pb](examples/minimal.pb) | see [releases](https://github.com/markus-wa/cs-demo-minifier/releases) page |

Events and attributes are documented in [events.md](events.md).
The minimal examples contain an extract of a demo with each event being included at least once.
Events and attributes are also are documented in [events.md](events.md).

More formats can be added programmatically by implementing the `ReplayMarshaller` interface.

Expand Down
2 changes: 1 addition & 1 deletion cmd/csminify/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
var (
demPath = "../../test/cs-demos/default.dem"
outDir = "../../test/results"
outPath = outDir + "/demo.min"
outPath = outDir + "/example"
)

func TestMain(m *testing.M) {
Expand Down
97 changes: 84 additions & 13 deletions csminify_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package csminify_test

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"github.com/alecthomas/jsonschema"
"github.com/markus-wa/cs-demo-minifier/protobuf"
"github.com/stretchr/testify/assert"
"gopkg.in/vmihailenco/msgpack.v2"
"io/ioutil"
"os"
"strings"
Expand All @@ -21,7 +24,7 @@ var (
chatDemoPath = "test/cs-demos/set/2017-05-17-ECSSeason3NA-liquid-vs-renegades-cobblestone.dem"
)

var nonDefaultReplay, parsedReplay rep.Replay
var nonDefaultReplay, parsedReplay, minimalExampleReplay rep.Replay

func TestMain(m *testing.M) {
nonDefaultReplay = nondefaultrep.GetNonDefaultReplay()
Expand All @@ -46,6 +49,31 @@ func initParsedReplay() {
if err != nil {
panic(err.Error())
}

minimalExampleReplay = exampleReplay()
}

func exampleReplay() rep.Replay {
var distinctEventTicks []rep.Tick

knownEvents := make(map[string]struct{})
for i := range parsedReplay.Ticks {
for j := range parsedReplay.Ticks[i].Events {
eventName := parsedReplay.Ticks[i].Events[j].Name
if _, alreadyKnown := knownEvents[eventName]; !alreadyKnown {
knownEvents[eventName] = struct{}{}
distinctEventTicks = append(distinctEventTicks, parsedReplay.Ticks[i])
break
}
}
}

return rep.Replay{
Header: parsedReplay.Header,
Entities: parsedReplay.Entities,
Snapshots: parsedReplay.Snapshots[0:2],
Ticks: distinctEventTicks,
}
}

func TestMinify(t *testing.T) {
Expand Down Expand Up @@ -170,27 +198,70 @@ func TestDemoSet(t *testing.T) {
}
}

var update = flag.Bool("updateDocs", false, "update schema documentation")
var updateGolden = flag.Bool("updateGolden", false, "update schema documentation")

const jsonSchemaFile = "schema.json"

func TestDoc(t *testing.T) {
schema, err := json.MarshalIndent(jsonschema.Reflect(&rep.Replay{}), "", "\t")
assert.NoError(t, err)

if *update {
f, err := os.OpenFile(jsonSchemaFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
assert.NoError(t, err)

_, err = f.Write(schema)
assert.NoError(t, err)
if *updateGolden {
updateGoldenFile(t, jsonSchemaFile, schema)
} else {
f, err := os.Open(jsonSchemaFile)
assert.NoError(t, err)

b, err := ioutil.ReadAll(f)
assert.NoError(t, err)
b := readFile(t, jsonSchemaFile)

assert.Equal(t, b, schema)
}
}

func TestExample_Json_Minimal(t *testing.T) {
data, err := json.MarshalIndent(&minimalExampleReplay, "", "\t")
assert.NoError(t, err)

testGoldenOrUpdate(t, "examples/minimal.json", data, unmarshalJSON)
}

func TestExample_MsgPack_Minimal(t *testing.T) {
data, err := msgpack.Marshal(&minimalExampleReplay)
assert.NoError(t, err)

testGoldenOrUpdate(t, "examples/minimal.mp", data, unmarshalMsgPack)
}

func TestExample_Protobuf_Minimal(t *testing.T) {
data := bytes.Buffer{}
err := protobuf.MarshalReplay(minimalExampleReplay, &data)
assert.NoError(t, err)

testGoldenOrUpdate(t, "examples/minimal.pb", data.Bytes(), protobuf.UnmarshalReplay)
}

func testGoldenOrUpdate(t *testing.T, fileName string, data []byte, unmarshaller replayUnmarshaller) {
if *updateGolden {
updateGoldenFile(t, fileName, data)
} else {
goldenBytes := readFile(t, fileName)

// just check if it's the same length as the order of the elements might be different
assert.Len(t, data, len(goldenBytes))
}
}

func readFile(t *testing.T, fileName string) []byte {
f, err := os.Open(fileName)
assert.NoError(t, err)

b, err := ioutil.ReadAll(f)
assert.NoError(t, err)

return b
}

func updateGoldenFile(t *testing.T, fileName string, bytes []byte) {
f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
assert.NoError(t, err)

_, err = f.Write(bytes)
assert.NoError(t, err)
}
Loading

0 comments on commit aef755a

Please sign in to comment.