Skip to content

Commit

Permalink
Run test cases as subtests, add benchmarks
Browse files Browse the repository at this point in the history
This vastly improves the Go test output. The benchmark is also added for
the next commit which changes the behavior of this library and may
potentially be marginally slower.
  • Loading branch information
diamondburned committed Aug 12, 2024
1 parent 57832ad commit 94af27c
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,40 +359,41 @@ func runCases(t *testing.T, runType RunType, cases []Case) {
invMsg = "JSON back to YAML"
}

for _, c := range cases {
// Convert the string.
t.Logf("converting %s\n", c.input)
output, err := f([]byte(c.input))
if err != nil {
t.Errorf("Failed to convert %s, input: `%s`, err: %v", msg, c.input, err)
}

// Check it against the expected output.
if string(output) != c.output {
t.Errorf("Failed to convert %s, input: `%s`, expected `%s`, got `%s`",
msg, c.input, c.output, string(output))
}

// Set the string that we will compare the reversed output to.
reverse := c.input
// If a special reverse string was specified, use that instead.
if c.reverse != nil {
reverse = *c.reverse
}

// Reverse the output.
input, err := invF(output)
if err != nil {
t.Errorf("Failed to convert %s, input: `%s`, err: %v", invMsg, string(output), err)
}

// Check the reverse is equal to the input (or to *c.reverse).
if string(input) != reverse {
t.Errorf("Failed to convert %s, input: `%s`, expected `%s`, got `%s`",
invMsg, string(output), reverse, string(input))
}
for i, c := range cases {
t.Run(fmt.Sprintf("%s_%d", msg, i), func(t *testing.T) {
// Convert the string.
t.Logf("converting %s\n", c.input)
output, err := f([]byte(c.input))
if err != nil {
t.Errorf("Failed to convert %s, input: `%s`, err: %v", msg, c.input, err)
}

// Check it against the expected output.
if string(output) != c.output {
t.Errorf("Failed to convert %s, input: `%s`, expected `%s`, got `%s`",
msg, c.input, c.output, string(output))
}

// Set the string that we will compare the reversed output to.
reverse := c.input
// If a special reverse string was specified, use that instead.
if c.reverse != nil {
reverse = *c.reverse
}

// Reverse the output.
input, err := invF(output)
if err != nil {
t.Errorf("Failed to convert %s, input: `%s`, err: %v", invMsg, string(output), err)
}

// Check the reverse is equal to the input (or to *c.reverse).
if string(input) != reverse {
t.Errorf("Failed to convert %s, input: `%s`, expected `%s`, got `%s`",
invMsg, string(output), reverse, string(input))
}
})
}

}

// To be able to easily fill in the *Case.reverse string above.
Expand All @@ -406,3 +407,17 @@ func TestYAMLToJSONDuplicateFields(t *testing.T) {
t.Error("expected YAMLtoJSON to fail on duplicate field names")
}
}

func BenchmarkJSONToYAML(b *testing.B) {
data := []byte(`{"t":"a"}`)
for i := 0; i < b.N; i++ {
JSONToYAML(data)
}
}

func BenchmarkYAMLToJSON(b *testing.B) {
data := []byte("t: a\n")
for i := 0; i < b.N; i++ {
YAMLToJSON(data)
}
}

0 comments on commit 94af27c

Please sign in to comment.