Skip to content

Commit

Permalink
Test coverage improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Galaco committed Mar 26, 2019
1 parent 91a5bef commit e9b860a
Show file tree
Hide file tree
Showing 96 changed files with 2,003 additions and 504 deletions.
6 changes: 3 additions & 3 deletions bsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ func (bsp *Bsp) GetHeader() Header {
return bsp.header
}

// GetLump gets the lump for a given index.
// GetLump gets the lump for a given id.
func (bsp *Bsp) GetLump(index LumpId) lumps.ILump {
return bsp.GetLumpRaw(index).GetContents()
}

// GetLumpRaw gets the lump for a given index.
// GetLumpRaw gets the lump for a given id.
func (bsp *Bsp) GetLumpRaw(index LumpId) *Lump {
return &bsp.lumps[int(index)]
}

// SetLump sets the lump data for a given index.
// SetLump sets the lump data for a given id.
func (bsp *Bsp) SetLump(index LumpId, lump Lump) {
bsp.lumps[int(index)] = lump
}
1 change: 0 additions & 1 deletion flags/constants.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/versions/v19.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/galaco/bsp/lumps"
)

// Returns an empty bsp v19 lump for the specified lump id
// Getv19Lump returns the corresponding v19 lump for provided index
func Getv19Lump(index int) (lumps.ILump, error) {
return Getv20Lump(index)
}
1 change: 1 addition & 0 deletions internal/versions/v20.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/galaco/bsp/lumps"
)

// Getv20Lump returns the corresponding v20 lump for provided index
func Getv20Lump(index int) (lumps.ILump, error) {
var ret lumps.ILump
var err error
Expand Down
2 changes: 1 addition & 1 deletion internal/versions/v21.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/galaco/bsp/lumps"
)

// Returns an empty bsp v19 lump for the specified lump id
// Getv21Lump returns the corresponding v21 lump for provided index
func Getv21Lump(index int) (lumps.ILump, error) {
return Getv20Lump(index)
}
2 changes: 1 addition & 1 deletion internal/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package versions

import "github.com/galaco/bsp/lumps"

// Returns an empty bsp lump for the specified bsp version and lump id
// GetLumpForVersion returns an empty bsp lump for the specified bsp version and lump id
// If a version is not 19,20,21 then a generic lump that holds
// raw bytes only ([]byte) is returned.
func GetLumpForVersion(bspVersion int, lumpId int) (lumps.ILump, error) {
Expand Down
28 changes: 15 additions & 13 deletions lump.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,61 @@ import (
"github.com/galaco/bsp/lumps"
)

// Container for a lump. Also includes metadata about the lump.
// Lump is a container for a lump. Also includes metadata about the lump.
// N.B. Some information mirrors the header's lump descriptor, but header information should not be trusted after
// import completion.
type Lump struct {
raw []byte
data lumps.ILump
length int32
index LumpId
id LumpId
loaded bool
}

// Get lump identifier
// Id is the lump type index (not the index for the order the lumps are stored)
// SetId Get lump identifier
// Id is the lump type id (not the id for the order the lumps are stored)
func (l *Lump) SetId(index LumpId) {
l.index = index
l.id = index
}

// Get the contents of a lump.
// GetContents Get the contents of a lump.
// NOTE: Will need to be cast to the relevant lumps
func (l *Lump) GetContents() lumps.ILump {
if !l.loaded {
l.data.Unmarshall(l.raw, int32(len(l.raw)))
if l.data.Unmarshall(l.raw) != nil {
return nil
}
l.loaded = true
}
return l.data
}

// Set content type of a lump.
// SetContents Set content type of a lump.
func (l *Lump) SetContents(data lumps.ILump) {
l.data = data
l.loaded = false
}

// Get the raw []byte contents of a lump.
// GetRawContents Get the raw []byte contents of a lump.
// N.B. This is the raw imported value. To get the raw value of a modified lump, use GetContents().Marshall()
func (l *Lump) GetRawContents() []byte {
return l.raw
}

// Set raw []byte contents of a lump.
// SetRawContents Set raw []byte contents of a lump.
func (l *Lump) SetRawContents(raw []byte) {
l.raw = raw
}

// Get length of a lump in bytes.
// GetLength Get length of a lump in bytes.
func (l *Lump) GetLength() int32 {
return l.length
}

// Return an instance of a Lump for a given offset.
// getReferenceLumpByIndex Return an instance of a Lump for a given offset.
func getReferenceLumpByIndex(index int, version int32) (lumps.ILump, error) {
if index < 0 || index > 63 {
return nil, fmt.Errorf("invalid lump index: %d provided", index)
return nil, fmt.Errorf("invalid lump id: %d provided", index)
}

return versions.GetLumpForVersion(int(version), index)
Expand Down
2 changes: 1 addition & 1 deletion lump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestLump_SetId(t *testing.T) {
sut := Lump{}
sut.SetId(LumpPakfile)

if sut.index != LumpPakfile {
if sut.id != LumpPakfile {
t.Error("incorrect lump id")
}
}
Expand Down
20 changes: 11 additions & 9 deletions lumps/area.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,31 @@ import (
"bytes"
"encoding/binary"
primitives "github.com/galaco/bsp/primitives/area"
"log"
"unsafe"
)

// Area is Lump 20: Areas
type Area struct {
LumpGeneric
Generic
data []primitives.Area
}

// Unmarshall Imports this lump from raw byte data
func (lump *Area) Unmarshall(raw []byte, length int32) {
lump.LumpInfo.SetLength(length)
func (lump *Area) Unmarshall(raw []byte) (err error) {
length := len(raw)
lump.Metadata.SetLength(length)
if length == 0 {
return
return nil
}

lump.data = make([]primitives.Area, length/int32(unsafe.Sizeof(primitives.Area{})))
err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data)
lump.data = make([]primitives.Area, length/int(unsafe.Sizeof(primitives.Area{})))
err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data)
if err != nil {
log.Fatal(err)
return err
}
lump.LumpInfo.SetLength(length)
lump.Metadata.SetLength(length)

return nil
}

// GetData gets internal format structure data
Expand Down
18 changes: 10 additions & 8 deletions lumps/areaportal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@ import (
"bytes"
"encoding/binary"
primitives "github.com/galaco/bsp/primitives/areaportal"
"log"
"unsafe"
)

// AreaPortal is Lump 21: Areaportals
type AreaPortal struct {
LumpGeneric
Generic
data []primitives.AreaPortal
}

// Unmarshall Imports this lump from raw byte data
func (lump *AreaPortal) Unmarshall(raw []byte, length int32) {
lump.LumpInfo.SetLength(length)
func (lump *AreaPortal) Unmarshall(raw []byte) (err error) {
length := len(raw)
lump.Metadata.SetLength(length)
if length == 0 {
return
return nil
}
lump.data = make([]primitives.AreaPortal, length/int32(unsafe.Sizeof(primitives.AreaPortal{})))
err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data)
lump.data = make([]primitives.AreaPortal, length/int(unsafe.Sizeof(primitives.AreaPortal{})))
err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data)
if err != nil {
log.Fatal(err)
return err
}

return nil
}

// GetData gets internal format structure data
Expand Down
16 changes: 9 additions & 7 deletions lumps/brush.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import (
"bytes"
"encoding/binary"
primitives "github.com/galaco/bsp/primitives/brush"
"log"
"unsafe"
)

// Brush is Lump 18: Brush
type Brush struct {
LumpGeneric
Generic
data []primitives.Brush
}

// Unmarshall Imports this lump from raw byte data
func (lump *Brush) Unmarshall(raw []byte, length int32) {
lump.data = make([]primitives.Brush, length/int32(unsafe.Sizeof(primitives.Brush{})))
err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data)
func (lump *Brush) Unmarshall(raw []byte) (err error) {
length := len(raw)
lump.data = make([]primitives.Brush, length/int(unsafe.Sizeof(primitives.Brush{})))
err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data)
if err != nil {
log.Fatal(err)
return err
}
lump.LumpInfo.SetLength(length)
lump.Metadata.SetLength(length)

return nil
}

// GetData gets internal format structure data
Expand Down
16 changes: 9 additions & 7 deletions lumps/brushside.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import (
"bytes"
"encoding/binary"
primitives "github.com/galaco/bsp/primitives/brushside"
"log"
"unsafe"
)

// BrushSide is Lump 19: BrushSide
type BrushSide struct {
LumpGeneric
Generic
data []primitives.BrushSide // MAX_MAP_BRUSHSIDES = 65536
}

// Unmarshall Imports this lump from raw byte data
func (lump *BrushSide) Unmarshall(raw []byte, length int32) {
lump.data = make([]primitives.BrushSide, length/int32(unsafe.Sizeof(primitives.BrushSide{})))
err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data)
func (lump *BrushSide) Unmarshall(raw []byte) (err error) {
length := len(raw)
lump.data = make([]primitives.BrushSide, length/int(unsafe.Sizeof(primitives.BrushSide{})))
err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data)
if err != nil {
log.Fatal(err)
return err
}
lump.LumpInfo.SetLength(length)
lump.Metadata.SetLength(length)

return nil
}

// GetData gets internal format structure data
Expand Down
15 changes: 8 additions & 7 deletions lumps/clipportalverts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import (
"bytes"
"encoding/binary"
"github.com/go-gl/mathgl/mgl32"
"log"
"unsafe"
)

// ClipPortalVerts is Lump 41: ClipPortalVerts
type ClipPortalVerts struct {
LumpGeneric
Generic
data []mgl32.Vec3
}

// Unmarshall Imports this lump from raw byte data
func (lump *ClipPortalVerts) Unmarshall(raw []byte, length int32) {
lump.data = make([]mgl32.Vec3, length/int32(unsafe.Sizeof(mgl32.Vec3{})))
err := binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data)
func (lump *ClipPortalVerts) Unmarshall(raw []byte) (err error) {
length := len(raw)
lump.data = make([]mgl32.Vec3, length/int(unsafe.Sizeof(mgl32.Vec3{})))
err = binary.Read(bytes.NewBuffer(raw), binary.LittleEndian, &lump.data)
if err != nil {
log.Fatal(err)
return err
}
lump.LumpInfo.SetLength(length)
lump.Metadata.SetLength(length)
return nil
}

// GetData gets internal format structure data
Expand Down
Loading

0 comments on commit e9b860a

Please sign in to comment.