Skip to content

Commit

Permalink
Ensure convert directory appears in Dirs
Browse files Browse the repository at this point in the history
This is attempting to track down a bug where convert.h isn't generated
properly in some cases.
Sadly, this test passes, so it isn't caused by a missing directory.
  • Loading branch information
MattWindsor91 committed May 24, 2023
1 parent 798578b commit a10b169
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/gen/config/cpp/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ type Config struct {
Catkin *catkin.Config `xml:"catkin"` // Catkin contains Catkin generator configurations.
}

// New constructs a Config programmatically with the given variant and options.
func New(variant Variant, options ...Option) *Config {
var cfg Config

cfg.Variant = variant

pcfg := &cfg

for _, option := range options {
option(pcfg)
}

return pcfg
}

// Option is a functional option for building a Config.
type Option func(*Config)

// WithChannel binds channel name to type ty in the configuration.
func WithChannel(name, ty string) Option {
return func(config *Config) {
config.Channels = append(config.Channels, Channel{Name: name, Type: ty})
}
}

// ChannelMap gets the Channels field of this Config as a map from channel names to type overrides.
func (c *Config) ChannelMap() map[string]string {
cmap := make(map[string]string, len(c.Channels))
Expand Down
62 changes: 62 additions & 0 deletions internal/gen/cpp/cpp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cpp_test

import (
"path/filepath"
"reflect"
"sort"
"testing"

cfg "github.com/UoY-RoboStar/rtcg/internal/gen/config/cpp"
"github.com/UoY-RoboStar/rtcg/internal/gen/cpp"
"github.com/UoY-RoboStar/rtcg/internal/gen/gencommon"
"github.com/UoY-RoboStar/rtcg/internal/stm"
)

// TestOnSuite_Dirs tests the Dirs method of OnSuite.
func TestOnSuite_Dirs(t *testing.T) {
t.Parallel()

rtcgConvertDir := filepath.Join("out", "src", "convert")
rtcgSrcDir := filepath.Join("out", "src", "rtcg")

for name, input := range map[string]struct {
config *cfg.Config
want []string
}{
"empty-animate": {
config: cfg.New(cfg.VariantAnimate),
want: []string{rtcgSrcDir},
},
"empty-animate-convert": {
config: cfg.New(cfg.VariantAnimate, cfg.WithChannel("foo", "bar")),
want: []string{rtcgConvertDir, rtcgSrcDir},
},
// TODO: add tests for non-empty cases
} {
config := input.config
want := input.want

t.Run(name, func(t *testing.T) {
t.Parallel()

dirs := gencommon.DirSet{Input: "in", Output: "out"}

gen, err := cpp.New(config, dirs)
if err != nil {
t.Fatalf("unexpected error creating generator: %s", err)
}

var suite stm.Suite
onSuite := gen.OnSuite(&suite)

got := onSuite.Dirs()

sort.Strings(got)
sort.Strings(want)

if !reflect.DeepEqual(got, want) {
t.Errorf("unexpected directories: got %v, want %v", got, want)
}
})
}
}

0 comments on commit a10b169

Please sign in to comment.