Skip to content

Commit

Permalink
fix(tests): improve tests output
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Sep 20, 2024
1 parent 186d636 commit a3f042a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
23 changes: 20 additions & 3 deletions e2e/incompat_types_with_untyped_port/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"os/exec"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,11 +14,27 @@ func Test(t *testing.T) {
out, err := cmd.CombinedOutput()
require.NoError(t, err)

require.Equal(
// TODO simplify when struct fields will have order: https://github.com/nevalang/neva/issues/698

expectedErrorPrefix := "main/main.neva:16:8 Incompatible types: in:in -> add: Subtype and supertype must both be either literals or instances, except if supertype is union: expression any, constraint {"
expectedErrorSuffix := "}\n"
expectedFields := []string{"data int", "idx int", "last bool"}

actualOutput := string(out)
require.True(
t,
"main/main.neva:16:8 Incompatible types: in:in -> add: Subtype and supertype must both be either literals or instances, except if supertype is union: expression any, constraint { data int, idx int, last bool }\n",
string(out),
strings.HasPrefix(actualOutput, expectedErrorPrefix),
"Error message should start with expected prefix",
)
require.True(
t,
strings.HasSuffix(actualOutput, expectedErrorSuffix),
"Error message should end with expected suffix")

// Check if all expected fields are present in the error message
for _, field := range expectedFields {
require.Contains(t, actualOutput, field, "Error message should contain field: "+field)
}

require.Equal(t, 0, cmd.ProcessState.ExitCode())
}
13 changes: 11 additions & 2 deletions examples/for_loop_over_list/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"fmt"
"os"
"os/exec"
"testing"
Expand All @@ -21,12 +22,20 @@ func Test(t *testing.T) {
for i := 0; i < 100; i++ {
cmd := exec.Command("neva", "run", "for_loop_over_list")
out, err := cmd.CombinedOutput()
require.NoError(t, err)
if err != nil {
exitError, ok := err.(*exec.ExitError)
if ok {
t.Fatalf("Command failed with exit code %d. Error output:\n%s", exitError.ExitCode(), string(out))
} else {
t.Fatalf("Command failed with error: %v. Output:\n%s", err, string(out))
}
}
require.Equal(
t,
"1\n2\n3\n",
string(out),
fmt.Sprintf("Unexpected output on iteration %d", i),
)
require.Equal(t, 0, cmd.ProcessState.ExitCode())
require.Equal(t, 0, cmd.ProcessState.ExitCode(), fmt.Sprintf("Unexpected exit code on iteration %d", i))
}
}
12 changes: 10 additions & 2 deletions examples/select/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"fmt"
"os"
"os/exec"
"testing"
Expand All @@ -19,13 +20,20 @@ func Test(t *testing.T) {
for i := 0; i < 100; i++ {
cmd := exec.Command("neva", "run", "select")
out, err := cmd.CombinedOutput()
require.NoError(t, err)
if err != nil {
exitError, ok := err.(*exec.ExitError)
if ok {
t.Fatalf("Command failed with exit code %d. Error output:\n%s", exitError.ExitCode(), string(out))
} else {
t.Fatalf("Command failed with error: %v. Output:\n%s", err, string(out))
}
}
require.Equal(
t,
"a\nb\nc\nd\n",
string(out),
"iteration %d failed\n", i,
)
require.Equal(t, 0, cmd.ProcessState.ExitCode())
require.Equal(t, 0, cmd.ProcessState.ExitCode(), fmt.Sprintf("Unexpected exit code on iteration %d", i))
}
}

0 comments on commit a3f042a

Please sign in to comment.