Skip to content

Commit

Permalink
fix failing test about error
Browse files Browse the repository at this point in the history
This line
// {"error: %s", []interface{}{errorToBePrinted}, "error: can I be printed?"},

gave panic: reflect: reflect.Value.SetString using value obtained using unexported field

Seems to have to do with the for loop using []interface{} and
what was being done in the body of the for.

in gnovm/stdlibs/error, exporting s in errorString fixed the error:

type errorString struct { S /* <- this */ string }

But moved the test in its own function, this passes:

```
func TestPrintErrors(t *testing.T) {
got := Sprintf("error: %s", errors.New("can I be printed?"))
expectedOutput := "error: can I be printed?"
if got != expectedOutput {
    t.Errorf("got %q, want %q.", got, expectedOutput)
}
}
```
  • Loading branch information
grepsuzette committed Jul 10, 2024
1 parent 1667431 commit b9c31a5
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions examples/gno.land/p/demo/ufmt/ufmt_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func (stringer) String() string {
}

func TestSprintf(t *testing.T) {
errorToBePrinted := errors.New("can I be printed?")
tru := true
cases := []struct {
format string
Expand Down Expand Up @@ -42,7 +41,6 @@ func TestSprintf(t *testing.T) {
{"â", nil, "â"},
{"Hello, World! 😊", nil, "Hello, World! 😊"},
{"unicode formatting: %s", []interface{}{"😊"}, "unicode formatting: 😊"},
{"error: %s", []interface{}{errorToBePrinted}, "error: can I be printed?"},
// mismatch printing
{"%s", []interface{}{nil}, "%!s(<nil>)"},
{"%s", []interface{}{421}, "%!s(int=421)"},
Expand Down Expand Up @@ -129,6 +127,14 @@ func TestErrorf(t *testing.T) {
}
}

func TestPrintErrors(t *testing.T) {
got := Sprintf("error: %s", errors.New("can I be printed?"))
expectedOutput := "error: can I be printed?"
if got != expectedOutput {
t.Errorf("got %q, want %q.", got, expectedOutput)
}
}

// NOTE: Currently, there is no way to get the output of Println without using os.Stdout,
// so we can only test that it doesn't panic and print arguments well.
func TestPrintln(t *testing.T) {
Expand Down

0 comments on commit b9c31a5

Please sign in to comment.