dimanche 23 août 2015

Using testing.T as anonymous struct field: "too many arguments in call to this.T.common.Fail"

I am trying to solve the Karate Chop kata in Go as an exercise and stuck with this compiler error from my test case:

too many arguments in call to this.T.common.Fail

I wrapped testing.T into a struct with additional methods, as an anonymous struct field:

package main

import (
    "fmt"
    "testing"
)

type assertions struct {
    *testing.T
}
func (this assertions) assert_equal(expected int, actual int) {
    if (expected != actual) {
        this.Fail(fmt.Sprintf("Failed asserting that %v is %v", actual, expected));
    }
}

func TestChop(t *testing.T) {
  test := assertions{t}

  test.assert_equal(-1, Chop(3, []int{}))
  test.assert_equal(-1, Chop(3, []int{1}))
  ...
}

I expect this.Fail to call Fail() on the anonymous testing.T struct field, which takes a string parameter. Why isn't this the case and where does this.T.common.Fail come from? I cannot find any reference to common in the testing package documentation.

Aucun commentaire:

Enregistrer un commentaire