diff --git a/README.md b/README.md index 5c30e1e..127844b 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,36 @@ If the confgiuration file path isn't specified, the file named `.tengo-tester.ym There are some configuration which are file paths. If a file path is a relative path, the base directory of the relative path is the directory where the configuration file exists. +## The type of map key should be string in entry.params and test.equal + +`entry.params` and `test.equal` is a variable of a Tengo script. +The type of map key of Tengo script should be string. + +https://github.com/d5/tengo/blob/master/docs/tutorial.md#values-and-value-types + +So the test fails if the map key whose type isn't string is found. + +ex. + +```yaml +--- +entries: +- name: main + script_file: foo.tengo + params: + foo: + true: 1 # the key is boolean + tests: + - name: test + var_name: result + equal: hello +``` + +``` +$ tengo-tester run +FATA[0000] format config: convert entry.params: entry_name: main: key: foo: the map key should be string : true +``` + ## LICENSE [MIT](LICENSE) diff --git a/pkg/config/config.go b/pkg/config/config.go index 71bca87..910ccb7 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,5 +1,9 @@ package config +import ( + "fmt" +) + type Config struct { Entries []Entry LogLevel string `yaml:"log_level"` @@ -21,3 +25,55 @@ type Test struct { IsNil bool `yaml:"is_nil"` IsNotNil bool `yaml:"is_not_nil"` } + +func ConvertConfig(cfg Config) error { + for i, entry := range cfg.Entries { + for k, v := range entry.Params { + p, err := ConvertMapKey(v) + if err != nil { + return fmt.Errorf("convert entry.params: entry_name: %s: key: %s: %w", entry.Name, k, err) + } + entry.Params[k] = p + } + for j, test := range entry.Tests { + p, err := ConvertMapKey(test.Equal) + if err != nil { + return fmt.Errorf("convert test.Equal: entry_name: %s: test_name: %s: %w", entry.Name, test.Name, err) + } + test.Equal = p + entry.Tests[j] = test + } + cfg.Entries[i] = entry + } + return nil +} + +func ConvertMapKey(data interface{}) (interface{}, error) { + switch t := data.(type) { + case map[interface{}]interface{}: + m := make(map[string]interface{}, len(t)) + for k, v := range t { + s, ok := k.(string) + if !ok { + return nil, fmt.Errorf("the map key should be string: %+v", k) + } + val, err := ConvertMapKey(v) + if err != nil { + return nil, fmt.Errorf("key: %s: %w", s, err) + } + m[s] = val + } + return m, nil + case []interface{}: + for i, v := range t { + val, err := ConvertMapKey(v) + if err != nil { + return nil, fmt.Errorf("index: %d: %w", i, err) + } + t[i] = val + } + return t, nil + default: + return data, nil + } +} diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index e63cacb..0b783eb 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -147,6 +147,9 @@ func (ctrl Controller) Run(ctx context.Context, wd string) error { if err != nil { return err } + if err := config.ConvertConfig(ctrl.Config); err != nil { + return fmt.Errorf("format config: %w", err) + } for _, entry := range ctrl.Config.Entries { err := ctrl.testEntry(wd, entry, testErrorTpl) if err != nil {