From c4389bdd9f87b09d3ba5126cba223248ccd9dde3 Mon Sep 17 00:00:00 2001 From: James Harris Date: Tue, 26 Nov 2024 06:40:47 +1000 Subject: [PATCH] Add support for unnamed matrix tests. --- internal/loader/builder.go | 6 +- internal/loader/content.go | 44 ++++++++++++++- internal/loader/fileloader/content.go | 15 ++++- .../testdata/without-prefix/.expect | 56 ++++++++++++++++--- .../testdata/without-prefix/input.one | 1 + .../testdata/without-prefix/input.two | 1 + .../testdata/without-prefix/input.txt | 1 - .../testdata/without-prefix/output.one | 1 + .../testdata/without-prefix/output.two | 1 + .../testdata/without-prefix/output.txt | 1 - internal/loader/internal/loadertest/render.go | 7 ++- internal/loader/markdownloader/content.go | 5 +- 12 files changed, 121 insertions(+), 18 deletions(-) create mode 100644 internal/loader/fileloader/testdata/without-prefix/input.one create mode 100644 internal/loader/fileloader/testdata/without-prefix/input.two delete mode 100644 internal/loader/fileloader/testdata/without-prefix/input.txt create mode 100644 internal/loader/fileloader/testdata/without-prefix/output.one create mode 100644 internal/loader/fileloader/testdata/without-prefix/output.two delete mode 100644 internal/loader/fileloader/testdata/without-prefix/output.txt diff --git a/internal/loader/builder.go b/internal/loader/builder.go index 69c75a3..3f8a2ef 100644 --- a/internal/loader/builder.go +++ b/internal/loader/builder.go @@ -41,7 +41,7 @@ func (b *TestBuilder) AddContent(env ContentEnvelope) error { return nil } - if env.Content.Group == "" { + if env.Content.Group == nil { return b.addAnonymousContent(env) } @@ -58,10 +58,10 @@ func (b *TestBuilder) addContent(env ContentEnvelope) error { switch env.Content.Role { case Input: - g := b.group(env.Content.Group) + g := b.group(groupName(env.Content.Group)) g.Inputs = append(g.Inputs, env) case Output: - g := b.group(env.Content.Group) + g := b.group(groupName(env.Content.Group)) g.Outputs = append(g.Outputs, env) } diff --git a/internal/loader/content.go b/internal/loader/content.go index 6e1a031..9c4f1c0 100644 --- a/internal/loader/content.go +++ b/internal/loader/content.go @@ -28,7 +28,7 @@ type Content struct { // Group is the name of the group to which the content belongs. Inputs and // outputs in the same group form a matrix of test cases. - Group string + Group *Group // Caption is an optional disambiguating name, title or short description of // the content. @@ -103,3 +103,45 @@ func SeparateContentByRole(content []ContentEnvelope) (inputs, outputs []Content return inputs, outputs } + +// Group is the group to which content belongs. Inputs and outputs in the same +// group form a matrix of test cases. +type Group struct { + name string +} + +// IsNamed returns true if the group has a name. +func (g *Group) IsNamed() bool { + return g.name != "" +} + +// Name returns the name of the group. +// +// It panics if the group is unnamed. +func (g *Group) Name() string { + if !g.IsNamed() { + panic("group is un-named") + } + return g.name +} + +// NamedGroup returns a [Group] with the given name. +func NamedGroup(name string) *Group { + if name == "" { + panic("group name must not be empty") + } + return &Group{name} +} + +// UnnamedGroup returns a [Group] with no name. +func UnnamedGroup() *Group { + return &Group{} +} + +// groupName returns the name of g as a string. +func groupName(g *Group) string { + if g == nil { + return "" + } + return g.name +} diff --git a/internal/loader/fileloader/content.go b/internal/loader/fileloader/content.go index 88ed2e2..5e44a13 100644 --- a/internal/loader/fileloader/content.go +++ b/internal/loader/fileloader/content.go @@ -43,10 +43,23 @@ func LoadContent(name string, f fs.File) (loader.Content, error) { lang = atoms[n] } + var group *loader.Group + if i == 0 { + // If we're in the first "atom" of the filename it means there is no + // named portion (the filename starts with the input/output marker), + // but we still want to group the inputs and outputs into a test + // matrix. + group = loader.UnnamedGroup() + } else { + group = loader.NamedGroup( + strings.Join(atoms[:i], "."), + ) + } + return loader.Content{ Language: lang, Data: data, - Group: strings.Join(atoms[:i], "."), + Group: group, Role: role, }, nil } diff --git a/internal/loader/fileloader/testdata/without-prefix/.expect b/internal/loader/fileloader/testdata/without-prefix/.expect index 80f067b..af53212 100644 --- a/internal/loader/fileloader/testdata/without-prefix/.expect +++ b/internal/loader/fileloader/testdata/without-prefix/.expect @@ -1,13 +1,51 @@ test "without-prefix" { - test "anonymous test in output.txt" { - assertion { - input "testdata/without-prefix/input.txt" { - lang = "txt" - data = "INPUT\n" - } - output "testdata/without-prefix/output.txt" { - lang = "txt" - data = "OUTPUT\n" + test { + test "one...one" { + assertion { + input "testdata/without-prefix/input.one" { + lang = "one" + data = "INPUT 1\n" + } + output "testdata/without-prefix/output.one" { + lang = "one" + data = "OUTPUT 1\n" + } + } + } + test "two...one" { + assertion { + input "testdata/without-prefix/input.two" { + lang = "two" + data = "INPUT 2\n" + } + output "testdata/without-prefix/output.one" { + lang = "one" + data = "OUTPUT 1\n" + } + } + } + test "one...two" { + assertion { + input "testdata/without-prefix/input.one" { + lang = "one" + data = "INPUT 1\n" + } + output "testdata/without-prefix/output.two" { + lang = "two" + data = "OUTPUT 2\n" + } + } + } + test "two...two" { + assertion { + input "testdata/without-prefix/input.two" { + lang = "two" + data = "INPUT 2\n" + } + output "testdata/without-prefix/output.two" { + lang = "two" + data = "OUTPUT 2\n" + } } } } diff --git a/internal/loader/fileloader/testdata/without-prefix/input.one b/internal/loader/fileloader/testdata/without-prefix/input.one new file mode 100644 index 0000000..eb40127 --- /dev/null +++ b/internal/loader/fileloader/testdata/without-prefix/input.one @@ -0,0 +1 @@ +INPUT 1 diff --git a/internal/loader/fileloader/testdata/without-prefix/input.two b/internal/loader/fileloader/testdata/without-prefix/input.two new file mode 100644 index 0000000..bb8a811 --- /dev/null +++ b/internal/loader/fileloader/testdata/without-prefix/input.two @@ -0,0 +1 @@ +INPUT 2 diff --git a/internal/loader/fileloader/testdata/without-prefix/input.txt b/internal/loader/fileloader/testdata/without-prefix/input.txt deleted file mode 100644 index 25494b5..0000000 --- a/internal/loader/fileloader/testdata/without-prefix/input.txt +++ /dev/null @@ -1 +0,0 @@ -INPUT diff --git a/internal/loader/fileloader/testdata/without-prefix/output.one b/internal/loader/fileloader/testdata/without-prefix/output.one new file mode 100644 index 0000000..5fd1059 --- /dev/null +++ b/internal/loader/fileloader/testdata/without-prefix/output.one @@ -0,0 +1 @@ +OUTPUT 1 diff --git a/internal/loader/fileloader/testdata/without-prefix/output.two b/internal/loader/fileloader/testdata/without-prefix/output.two new file mode 100644 index 0000000..4a8fc63 --- /dev/null +++ b/internal/loader/fileloader/testdata/without-prefix/output.two @@ -0,0 +1 @@ +OUTPUT 2 diff --git a/internal/loader/fileloader/testdata/without-prefix/output.txt b/internal/loader/fileloader/testdata/without-prefix/output.txt deleted file mode 100644 index 026a600..0000000 --- a/internal/loader/fileloader/testdata/without-prefix/output.txt +++ /dev/null @@ -1 +0,0 @@ -OUTPUT diff --git a/internal/loader/internal/loadertest/render.go b/internal/loader/internal/loadertest/render.go index 5414a06..bab7c4f 100644 --- a/internal/loader/internal/loadertest/render.go +++ b/internal/loader/internal/loadertest/render.go @@ -11,11 +11,16 @@ import ( // a test. func RenderTest(t test.Test) []byte { var w bytes.Buffer + w.WriteString("test") + + if t.Name != "" { + fmt.Fprintf(&w, " %q", t.Name) + } - fmt.Fprintf(&w, "test %q", t.Name) if t.Skip { w.WriteString(" [skipped]") } + w.WriteString(" {\n") for _, s := range t.SubTests { diff --git a/internal/loader/markdownloader/content.go b/internal/loader/markdownloader/content.go index c86bee6..4e24f67 100644 --- a/internal/loader/markdownloader/content.go +++ b/internal/loader/markdownloader/content.go @@ -70,12 +70,15 @@ func LoadContent( } c := loader.Content{ - Group: group, Language: lang, Attributes: attrs, Data: []byte(code), } + if group != "" { + c.Group = loader.NamedGroup(group) + } + if len(headings) > 0 { c.Caption = headings[len(headings)-1] }