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

huangapple go评论78阅读模式
英文:

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

问题

我正在尝试使用Go语言解决Karate Chop kata作为练习,但在我的测试用例中遇到了这个编译器错误:

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

我将testing.T包装成一个带有额外方法的结构体,作为匿名结构体字段:

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}))
  ...
}

我期望this.Fail调用匿名的testing.T结构体字段上的Fail()方法,该方法接受一个字符串参数。为什么情况不是这样,并且this.T.common.Fail是从哪里来的?我在testing包的文档中找不到任何关于common的参考。

英文:

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.

答案1

得分: 3

以下是翻译好的内容:

> 源文件 src/testing/testing.go
>
>
> // Fail将函数标记为失败,但继续执行。
> func (c *common) Fail() {
> c.mu.Lock()
> defer c.mu.Unlock()
> c.failed = true
> }
>
> // common保存T和B之间的共同元素,并捕获Errorf等常用方法。
> type common struct {
> mu sync.RWMutex // 保护output和failed
> output []byte // 测试或基准测试生成的输出。
> failed bool // 测试或基准测试失败。
> skipped bool // 测试或基准测试已跳过。
> finished bool
>
> start time.Time // 测试或基准测试开始时间
> duration time.Duration
> self interface{} // 完成时要发送到信号通道的内容。
> signal chan interface{} // 串行测试的输出。
> }
>
> // T是传递给Test函数以管理测试状态并支持格式化测试日志的类型。
> // 在执行过程中,日志会累积,并在完成时转储到标准错误。
> type T struct {
> common
> name string // 测试的名称。
> startParallel chan bool // 并行测试将等待此通道。
> }
>
> func (*T) Fail
>
> func (c *T) Fail()
>
> Fail将函数标记为失败,但继续执行。

T.common.Fail()没有参数。

尝试使用Errorf

> func (*T) Errorf
>
> func (c *T) Errorf(format string, args ...interface{})
>
> Errorf等效于Logf,然后是Fail。

例如,

this.Errorf("断言失败:%v 不等于 %v", actual, expected)
英文:

> Source file src/testing/testing.go
>
>
> // Fail marks the function as having failed but continues execution.
> func (c *common) Fail() {
> c.mu.Lock()
> defer c.mu.Unlock()
> c.failed = true
> }
>
> // common holds the elements common between T and B and
> // captures common methods such as Errorf.
> type common struct {
> mu sync.RWMutex // guards output and failed
> output []byte // Output generated by test or benchmark.
> failed bool // Test or benchmark has failed.
> skipped bool // Test of benchmark has been skipped.
> finished bool
>
> start time.Time // Time test or benchmark started
> duration time.Duration
> self interface{} // To be sent on signal channel when done.
> signal chan interface{} // Output for serial tests.
> }
>
> // T is a type passed to Test functions to manage test state and support formatted test logs.
> // Logs are accumulated during execution and dumped to standard error when done.
> type T struct {
> common
> name string // Name of test.
> startParallel chan bool // Parallel tests will wait on this.
> }
>
> func (*T) Fail
>
> func (c *T) Fail()
>
> Fail marks the function as having failed but continues execution.

There are no arguments to T.common.Fail().

Try Errorf:

> func (*T) Errorf
>
> func (c *T) Errorf(format string, args ...interface{})
>
> Errorf is equivalent to Logf followed by Fail.

For example,

this.Errorf("Failed asserting that %v is %v", actual, expected)

huangapple
  • 本文由 发表于 2015年8月24日 06:21:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/32172132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定