我是 golang 的新手,正在完成编码练习,我在一个名为leap 的目录中拥有以下所有文件。我正在使用 gvm 运行 golang 可执行文件(版本 1.4),使用诸如“go test jump_test.go”之类的命令。
当我去测试leap_test.go时,我得到以下结果:
# command-line-arguments
leap_test.go:5:2: open /home/user/go/leap/leap: no such file or directory
FAIL command-line-arguments [setup failed]
如何包含 IsLeap() 函数以便测试正确运行。
为什么cases_test.go 甚至包括在内?似乎leap_test.go 就是您进行测试所需的全部内容。
case_test.go
package leap
// Source: exercism/x-common
// Commit: 945d08e Merge pull request #50 from soniakeys/master
var testCases = []struct {
year int
expected bool
description string
}{
{1996, true, "leap year"},
{1997, false, "non-leap year"},
{1998, false, "non-leap even year"},
{1900, false, "century"},
{2400, true, "fourth century"},
{2000, true, "Y2K"},
}
飞跃_test.go
package leap
import (
"testing"
"./leap"
)
var testCases = []struct {
year int
expected bool
description string
}{
{1996, true, "a vanilla leap year"},
{1997, false, "a normal year"},
{1900, false, "a century"},
{2400, true, "an exceptional century"},
}
func TestLeapYears(t *testing.T) {
for _, test := range testCases {
observed := IsLeap(test.year)
if observed != test.expected {
t.Fatalf("%v is %s", test.year, test.description)
}
}
}
飞跃
package leap
import(
"fmt"
)
func IsLeap(year int) bool {
return true
}
函数式编程
相关分类