Skip to content

Commit

Permalink
d2sstatus: add unittest (fix #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed May 20, 2021
1 parent 8a77343 commit 5730e71
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion d2s.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func Load(data []byte) (*D2S, error) {
}

status := sr.GetByte()
result.Status.Unmarshal(status)
result.Status.Load(status)

/*
from: https://user.xmission.com/~trevin/DiabloIIv1.09_File_Format.shtml
Expand Down
6 changes: 3 additions & 3 deletions d2sstatus/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Status struct {
Died, // it is true, if you hav died in some point in past
Unknown4,
Expansion,
Ladder,
Ladder, // uncertain
Unknown7 bool
}

Expand All @@ -24,8 +24,8 @@ func New() *Status {
return result
}

// Unmarshal loads data into status structure
func (s *Status) Unmarshal(data byte) {
// Load loads data into status structure
func (s *Status) Load(data byte) {
bm := datautils.CreateBitMuncher([]byte{data}, 0)
s.Unknown0 = bm.GetBit() == 1
s.Unknown1 = bm.GetBit() == 1
Expand Down
51 changes: 51 additions & 0 deletions d2sstatus/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package d2sstatus

import (
"testing"

"github.com/stretchr/testify/assert"
)

func testdata() map[byte]*Status {
return map[byte]*Status{
255: { // averything true
Unknown0: true,
Unknown1: true,
Hardcore: true,
Died: true,
Unknown4: true,
Expansion: true,
Ladder: true,
Unknown7: true,
},
104: {
Unknown0: false,
Unknown1: false,
Hardcore: false,
Died: true,
Unknown4: false,
Expansion: true,
Ladder: true,
Unknown7: false,
},
}
}

func Test_Load(t *testing.T) {
td := testdata()
for key, value := range td {
s := New()
s.Load(key)

assert.Equal(t, value, s, "unexpected status structure loaded")
}
}

func Test_Encode(t *testing.T) {
td := testdata()
for key, value := range td {
d := value.Encode()

assert.Equal(t, key, d, "unexpected status encoded")
}
}

0 comments on commit 5730e71

Please sign in to comment.