From a3f042ab69f2cc5978adc7dba0ba2a9aa37b95d2 Mon Sep 17 00:00:00 2001 From: Emil Valeev Date: Fri, 20 Sep 2024 14:13:14 +0500 Subject: [PATCH] fix(tests): improve tests output --- .../e2e_test.go | 23 ++++++++++++++++--- examples/for_loop_over_list/e2e_test.go | 13 +++++++++-- examples/select/e2e_test.go | 12 ++++++++-- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/e2e/incompat_types_with_untyped_port/e2e_test.go b/e2e/incompat_types_with_untyped_port/e2e_test.go index 751622d0..da4f5861 100644 --- a/e2e/incompat_types_with_untyped_port/e2e_test.go +++ b/e2e/incompat_types_with_untyped_port/e2e_test.go @@ -2,6 +2,7 @@ package test import ( "os/exec" + "strings" "testing" "github.com/stretchr/testify/require" @@ -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()) } diff --git a/examples/for_loop_over_list/e2e_test.go b/examples/for_loop_over_list/e2e_test.go index c469575c..40e182bc 100644 --- a/examples/for_loop_over_list/e2e_test.go +++ b/examples/for_loop_over_list/e2e_test.go @@ -1,6 +1,7 @@ package test import ( + "fmt" "os" "os/exec" "testing" @@ -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)) } } diff --git a/examples/select/e2e_test.go b/examples/select/e2e_test.go index f7db5aa0..09dbe05a 100644 --- a/examples/select/e2e_test.go +++ b/examples/select/e2e_test.go @@ -1,6 +1,7 @@ package test import ( + "fmt" "os" "os/exec" "testing" @@ -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)) } }