Skip to content

Commit

Permalink
Add support for unnamed matrix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Nov 25, 2024
1 parent ee01bf7 commit c4389bd
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 18 deletions.
6 changes: 3 additions & 3 deletions internal/loader/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
44 changes: 43 additions & 1 deletion internal/loader/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
15 changes: 14 additions & 1 deletion internal/loader/fileloader/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
56 changes: 47 additions & 9 deletions internal/loader/fileloader/testdata/without-prefix/.expect
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INPUT 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INPUT 2

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OUTPUT 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OUTPUT 2

This file was deleted.

7 changes: 6 additions & 1 deletion internal/loader/internal/loadertest/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion internal/loader/markdownloader/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down

0 comments on commit c4389bd

Please sign in to comment.