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 8efbd34 commit 9075781
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ if self.Signature == BIG: # where big is a constant or a eq pattern type value
if self.Signature == 5:
...
# example from bmp.yml
# bit pattern matching example from bmp.yml
u32 HeaderSize:
eq 0000_000c: V2 # V2 automatically becomes a constant
eq 0000_0028: V3
Expand Down
9 changes: 9 additions & 0 deletions mapper/filelayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,15 @@ func (fl *FileLayout) presentStruct(layout *Struct, cfg *PresentFileLayoutConfig
return res
}

// used for tests, buffered, returns when presentation is fully rendered.
func (fl *FileLayout) PresentFullString(cfg *PresentFileLayoutConfig) string {
s := ""
for _, layout := range fl.Structs {
s += fl.presentStruct(layout, cfg) + "\n"
}
return s
}

func (fl *FileLayout) Present(cfg *PresentFileLayoutConfig) {
if fl == nil {
panic("Probably input yaml error, look for properly escaped strings and \" characters")
Expand Down
52 changes: 52 additions & 0 deletions mapper/mapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package mapper

import (
"strings"
"testing"

"github.com/martinlindhe/feng/template"
"github.com/stretchr/testify/assert"
)

var testPResentConfig = PresentFileLayoutConfig{
ShowRaw: true,
}

func TestPresentData1(t *testing.T) {
templateData := `
structs:
header:
u8 Flags:
bit b0001: B0
bit b0110: Rest
default: invalid
#if Flags == 6: # FIXME should error out on "unknown variable name 'Flags'"
if self.Flags == 6:
u8 Child: ??
layout:
- header Header
`
in := []byte{0x06, 0x03}
expected := `
Header
[000000] Flags u8 6 06
- B0 bit 0:1 0
- Rest bit 1:2 3
[000001] Child u8 3 03
`

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))
}
4 changes: 3 additions & 1 deletion template/datastructures.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// structure of a evaluated ./templates/ yaml file. see Template for the raw structure corresponding to the yaml file
type DataStructure struct {

// constants derived from eq & bit matches
// constants derived from eq & bit pattern matches
Constants []Constant

// evaluated file structs
Expand Down Expand Up @@ -61,11 +61,13 @@ func NewDataStructureFrom(template *Template, basename string) (*DataStructure,
if DEBUG_PATTERNS {
log.Print("NewDataStructureFrom", basename)
}

constants, err := template.evaluateConstants()
if err != nil {
log.Warn().Err(err).Msgf("%s: evaluateConstants failed", basename)
return nil, err
}

structs, err := template.evaluateStructs()
if err != nil {
log.Warn().Err(err).Msgf("%s: evaluateStructs failed", basename)
Expand Down
5 changes: 2 additions & 3 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/martinlindhe/feng/value"
)

// evaluate all templates, validate some fields
func TestEvaluateAllTemplates(t *testing.T) {
// evaluate all templates to catch template parse errors, and validate some fields
func TestValidateAllTemplates(t *testing.T) {

fs.WalkDir(feng.Templates, ".", func(path string, d fs.DirEntry, err2 error) error {
// cannot happen
Expand Down Expand Up @@ -108,5 +108,4 @@ layout:
{Kind: "other_segment", Range: "", Slice: true, Label: "other_segments"},
},
}, ds)

}

0 comments on commit 9075781

Please sign in to comment.