Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Aug 5, 2024
1 parent d654f21 commit 168bf71
Show file tree
Hide file tree
Showing 23 changed files with 364 additions and 166 deletions.
115 changes: 91 additions & 24 deletions cmd/dialects-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,15 @@ var tplTest = template.Must(template.New("").Parse(
package dialects
import (
"testing"
"encoding"
"reflect"
"testing"
"encoding"
"reflect"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/require"
"github.com/bluenviron/gomavlib/v3/pkg/dialect"
{{- range .Dialects }}
"github.com/bluenviron/gomavlib/v3/pkg/dialects/{{ . }}"
{{- end }}
"github.com/bluenviron/gomavlib/v3/pkg/dialects/common"
)
func TestDialects(t *testing.T) {
{{- range .Dialects }}
func() {
_, err := dialect.NewReadWriter({{ . }}.Dialect)
require.NoError(t, err)
}()
{{- end }}
}
var casesEnum = []struct {
name string
dec encoding.TextMarshaler
Expand Down Expand Up @@ -78,6 +66,42 @@ func TestEnumMarshalText(t *testing.T) {
}
`))

var tplDialectTest = template.Must(template.New("").Parse(
`//autogenerated:yes
//nolint:revive
package {{ .PkgName }}
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/bluenviron/gomavlib/v3/pkg/dialect"
)
func TestDialect(t *testing.T) {
_, err := dialect.NewReadWriter(Dialect)
require.NoError(t, err)
}
`))

var tplEnumTest = template.Must(template.New("").Parse(
`//autogenerated:yes
//nolint:revive,govet,errcheck
package {{ .PkgName }}
import (
"testing"
)
func TestEnum_{{ .Name }}(t *testing.T) {
var e {{ .Name }}
e.UnmarshalText([]byte{})
e.MarshalText()
e.String()
}
`))

func writeTemplate(fpath string, tpl *template.Template, args map[string]interface{}) error {
f, err := os.Create(fpath)
if err != nil {
Expand Down Expand Up @@ -121,6 +145,55 @@ func processDialect(commit string, name string) error {
return err
}

pkgName := strings.ToLower(strings.ReplaceAll(name, "_", ""))

err = writeTemplate(
"./"+pkgName+"/dialect_test.go",
tplDialectTest,
map[string]interface{}{
"PkgName": pkgName,
})
if err != nil {
return err
}

entries, err := os.ReadDir(pkgName)
if err != nil {
return err
}

for _, f := range entries {
if !strings.HasPrefix(f.Name(), "enum_") {
continue
}

buf, err := os.ReadFile(filepath.Join(pkgName, f.Name()))
if err != nil {
return err
}
str := string(buf)

if !strings.Contains(str, "MarshalText(") {
continue
}

enumName := f.Name()
enumName = enumName[len("enum_"):]
enumName = enumName[:len(enumName)-len(".go")]
enumName = strings.ToUpper(enumName)

err = writeTemplate(
"./"+pkgName+"/"+strings.ReplaceAll(f.Name(), ".go", "_test.go"),
tplEnumTest,
map[string]interface{}{
"PkgName": pkgName,
"Name": enumName,
})
if err != nil {
return err
}
}

fmt.Fprintf(os.Stderr, "\n")
return nil
}
Expand Down Expand Up @@ -151,8 +224,6 @@ func run() error {
return err
}

var dialects []string //nolint:prealloc

for _, f := range files {
if !strings.HasSuffix(f.Name, ".xml") {
continue
Expand All @@ -163,16 +234,12 @@ func run() error {
if err != nil {
return err
}

dialects = append(dialects, strings.ReplaceAll(strings.ToLower(name), "_", ""))
}

err = writeTemplate(
"package_test.go",
tplTest,
map[string]interface{}{
"Dialects": dialects,
})
map[string]interface{}{})
if err != nil {
return err
}
Expand Down
114 changes: 57 additions & 57 deletions pkg/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ var tplDialect = template.Must(template.New("").Parse(
package {{ .PkgName }}
import (
"github.com/bluenviron/gomavlib/v3/pkg/message"
"github.com/bluenviron/gomavlib/v3/pkg/dialect"
"github.com/bluenviron/gomavlib/v3/pkg/message"
"github.com/bluenviron/gomavlib/v3/pkg/dialect"
)
// Dialect contains the dialect definition.
var Dialect = dial
// dial is not exposed directly in order not to display it in godoc.
var dial = &dialect.Dialect{
Version: {{.Version}},
Messages: []message.Message{
Version: {{.Version}},
Messages: []message.Message{
{{- range .Defs }}
// {{ .Name }}
// {{ .Name }}
{{- range .Messages }}
&Message{{ .Name }}{},
&Message{{ .Name }}{},
{{- end }}
{{- end }}
},
},
}
`))

Expand All @@ -58,7 +58,7 @@ package {{ .PkgName }}
{{- if .Link }}
import (
"github.com/bluenviron/gomavlib/v3/pkg/dialects/{{ .Enum.DefName }}"
"github.com/bluenviron/gomavlib/v3/pkg/dialects/{{ .Enum.DefName }}"
)
{{- range .Enum.Description }}
Expand All @@ -70,20 +70,20 @@ const (
{{- $en := .Enum }}
{{- range .Enum.Values }}
{{- range .Description }}
// {{ . }}
// {{ . }}
{{- end }}
{{ .Name }} {{ $en.Name }} = {{ $en.DefName }}.{{ .Name }}
{{ .Name }} {{ $en.Name }} = {{ $en.DefName }}.{{ .Name }}
{{- end }}
)
{{- else }}
import (
"strconv"
"strconv"
{{- if .Enum.Bitmask }}
"strings"
"strings"
{{- end }}
"fmt"
"fmt"
)
{{- range .Enum.Description }}
Expand All @@ -95,77 +95,77 @@ const (
{{- $pn := .Enum.Name }}
{{- range .Enum.Values }}
{{- range .Description }}
// {{ . }}
// {{ . }}
{{- end }}
{{ .Name }} {{ $pn }} = {{ .Value }}
{{ .Name }} {{ $pn }} = {{ .Value }}
{{- end }}
)
var labels_{{ .Enum.Name }} = map[{{ .Enum.Name }}]string{
{{- range .Enum.Values }}
{{ .Name }}: "{{ .Name }}",
{{ .Name }}: "{{ .Name }}",
{{- end }}
}
var values_{{ .Enum.Name }} = map[string]{{ .Enum.Name }}{
{{- range .Enum.Values }}
"{{ .Name }}": {{ .Name }},
"{{ .Name }}": {{ .Name }},
{{- end }}
}
// MarshalText implements the encoding.TextMarshaler interface.
func (e {{ .Enum.Name }}) MarshalText() ([]byte, error) {
{{- if .Enum.Bitmask }}
if e == 0 {
return []byte("0"), nil
}
var names []string
for i := 0; i < {{ len .Enum.Values }}; i++ {
mask := {{ .Enum.Name }}(1 << i)
if e&mask == mask {
names = append(names, labels_{{ .Enum.Name }}[mask])
}
}
return []byte(strings.Join(names, " | ")), nil
if e == 0 {
return []byte("0"), nil
}
var names []string
for i := 0; i < {{ len .Enum.Values }}; i++ {
mask := {{ .Enum.Name }}(1 << i)
if e&mask == mask {
names = append(names, labels_{{ .Enum.Name }}[mask])
}
}
return []byte(strings.Join(names, " | ")), nil
{{- else }}
if name, ok := labels_{{ .Enum.Name }}[e]; ok {
return []byte(name), nil
}
return []byte(strconv.Itoa(int(e))), nil
if name, ok := labels_{{ .Enum.Name }}[e]; ok {
return []byte(name), nil
}
return []byte(strconv.Itoa(int(e))), nil
{{- end }}
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (e *{{ .Enum.Name }}) UnmarshalText(text []byte) error {
{{- if .Enum.Bitmask }}
labels := strings.Split(string(text), " | ")
var mask {{ .Enum.Name }}
for _, label := range labels {
if value, ok := values_{{ .Enum.Name }}[label]; ok {
mask |= value
} else if value, err := strconv.Atoi(label); err == nil {
mask |= {{ .Enum.Name }}(value)
} else {
return fmt.Errorf("invalid label '%s'", label)
}
}
labels := strings.Split(string(text), " | ")
var mask {{ .Enum.Name }}
for _, label := range labels {
if value, ok := values_{{ .Enum.Name }}[label]; ok {
mask |= value
} else if value, err := strconv.Atoi(label); err == nil {
mask |= {{ .Enum.Name }}(value)
} else {
return fmt.Errorf("invalid label '%s'", label)
}
}
*e = mask
{{- else }}
if value, ok := values_{{ .Enum.Name }}[string(text)]; ok {
*e = value
} else if value, err := strconv.Atoi(string(text)); err == nil {
*e = {{ .Enum.Name }}(value)
} else {
return fmt.Errorf("invalid label '%s'", text)
}
if value, ok := values_{{ .Enum.Name }}[string(text)]; ok {
*e = value
} else if value, err := strconv.Atoi(string(text)); err == nil {
*e = {{ .Enum.Name }}(value)
} else {
return fmt.Errorf("invalid label '%s'", text)
}
{{- end }}
return nil
return nil
}
// String implements the fmt.Stringer interface.
func (e {{ .Enum.Name }}) String() string {
val, _ := e.MarshalText()
return string(val)
val, _ := e.MarshalText()
return string(val)
}
{{- end }}
`))
Expand All @@ -178,7 +178,7 @@ package {{ .PkgName }}
{{- if .Link }}
import (
"github.com/bluenviron/gomavlib/v3/pkg/dialects/{{ .Msg.DefName }}"
"github.com/bluenviron/gomavlib/v3/pkg/dialects/{{ .Msg.DefName }}"
)
{{- range .Msg.Description }}
Expand All @@ -194,15 +194,15 @@ type Message{{ .Msg.Name }} = {{ .Msg.DefName }}.Message{{ .Msg.Name }}
type Message{{ .Msg.Name }} struct {
{{- range .Msg.Fields }}
{{- range .Description }}
// {{ . }}
// {{ . }}
{{- end }}
{{ .Line }}
{{ .Line }}
{{- end }}
}
// GetID implements the message.Message interface.
func (*Message{{ .Msg.Name }}) GetID() uint32 {
return {{ .Msg.ID }}
return {{ .Msg.ID }}
}
{{- end }}
Expand Down
Loading

0 comments on commit 168bf71

Please sign in to comment.