Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlindhe committed Jan 11, 2024
1 parent 9075781 commit 560793b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func MapReader(cfg *MapReaderConfig) (*FileLayout, error) {
if err != nil && !errors.Is(err, ErrParseStop) {
if !errors.Is(err, io.ErrUnexpectedEOF) && !errors.Is(err, io.EOF) {
log.Error().Err(err).Msgf("mapLayout error processing %s at %06x", df.Label, fl.offset)
return &fl, err
}
return &fl, nil
}
Expand Down
56 changes: 56 additions & 0 deletions mapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
"testing"

"github.com/martinlindhe/feng"
"github.com/martinlindhe/feng/template"
"github.com/stretchr/testify/assert"
)
Expand All @@ -12,6 +13,10 @@ var testPResentConfig = PresentFileLayoutConfig{
ShowRaw: true,
}

func init() {
feng.InitLogging()
}

func TestPresentData1(t *testing.T) {
templateData := `
structs:
Expand Down Expand Up @@ -50,3 +55,54 @@ Header
data := fl.PresentFullString(&testPResentConfig)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(data))
}

func TestBitMatching(t *testing.T) {
templateData := `
endian: big
structs:
header:
u8 Flags8:
bit b0000_1111: Lo8
bit b1111_0000: Hi8
u16 Flags16:
bit b0000_0000_0000_1111: Lo16
bit b1111_1111_1111_0000: Hi16
u32 Flags32:
bit b0000_0000_0000_0000_0000_0000_0000_1111: Lo32
bit b1111_1111_1111_1111_1111_1111_1111_0000: Hi32
layout:
- header Header
`
in := []byte{
0x11,
0x00, 0x11,
0x00, 0x00, 0x00, 0x11,
}
expected := `
Header
[000000] Flags8 u8 17 11
- Lo8 bit 0:4 1
- Hi8 bit 4:4 1
[000001] Flags16 u16 be 17 00 11
- Lo16 bit 0:4 1
- Hi16 bit 4:12 1
[000003] Flags32 u32 be 17 00 00 00 11
- Lo32 bit 0:4 1
- Hi32 bit 4:28 1
`

ds, err := template.UnmarshalTemplateIntoDataStructure([]byte(templateData), "")
assert.Equal(t, nil, err)

f := mockFile(t, "in", in)

fl, err := MapReader(&MapReaderConfig{
F: f,
DS: ds,
})
assert.Equal(t, nil, err)

data := fl.PresentFullString(&testPResentConfig)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(data))
}

0 comments on commit 560793b

Please sign in to comment.