英文:
How do I test my interface functions?
问题
第三天学习Go语言,如果这是一个新手问题,请原谅;)。我正在创建一个简单的计算器,最终将具有许多不同的任务:加法、减法、乘法等等...每个任务都将有两个函数:First和Second。
我目前的组织结构如下:
github.com/
mytestproj/
calculator/
addition.go
addition_test.go
subtraction.go
subtraction_test.go
main/
main.go
在addition.go中,我有以下代码:
package calculator
type Add struct{}
func BasicAddition(x int) int { // 这个函数不会在最终版本中出现
return x + 2
}
func (h Add) First(x int) int {
x += 5
return x
}
func (h Add) Second(x int) int {
x += 10
return x
}
在addition_test.go中,我有以下代码:
package calculator
import "testing"
func TestBasicAddition(t *testing.T) {
x := 30
if y := BasicAddition(x); y != 32 {
t.Errorf("Mine is %v", y)
}
}
func TestFirst(t *testing.T) {
x := 10
if y := First(x); y != 15 {
t.Errorf("First is %v", y)
}
}
当我运行测试时,出现错误:
# github.com/mytestproj/calculator
./addition_test.go:15: undefined: First
FAIL github.com/mytestproj/calculator [build failed]
我的问题是:如何测试"First"函数?
如果我完全删除对First的测试,那么测试就会正常运行并通过。
次要问题:我的想法是构建一个具有许多不同功能的计算器应用程序。如果有更好的代码组织方式,请告诉我。
英文:
Third day in Go so pardon if this is a newb question ;). I am creating a simple calculator that will eventually have many different tasks: addition, subtraction, multiplication, etc, etc...Each of the tasks will have 2 functions: First and Second.
package main
import (
"github.com/mytestproj/calculator"
)
type Calc interface {
First(x int) int
Second(x int) int
}
func main() {
x := 16
var i Calc
a := calculator.Add{}
i = a
i.First(x)
i.Second(x)
}
I currently have everything organized as:
github.com/
mytestproj/
calculator/
addition.go
addition_test.go
subtraction.go
subtraction_test.go
main/
main.go
In addition.go I have:
package calculator
type Add struct{}
func BasicAddition(x int) int { // this won't be in the final release
return x + 2
}
func (h Add) First(x int) int {
x += 5
return x
}
func (h Add) Second(x int) int {
x += 10
return x
}
And in addition_test.go I have:
package calculator
import "testing"
func TestBasicAddition(t *testing.T) {
x := 30
if y := BasicAddition(x); y != 32 {
t.Errorf("Mine is %v", y)
}
}
func TestFirst(t *testing.T) {
x := 10
if y := First(x); y != 15 {
t.Errorf("First is %v", y)
}
}
When I run my test I get an error:
# github.com/mytestproj/calculator
./addition_test.go:15: undefined: First
FAIL github.com/mytestproj/calculator [build failed]
My question: How do I test "First?"
If I remove the test for First completely then the tests run fine and it passes.
Secondary question: The idea is to build a calculator app with many different functions. If there's a better way to organize the code then please let me know.
答案1
得分: 1
First
是Add
类型上的一个方法。例如,
a := Add{}
a.First(x)
addition.go
package calculator
type Add struct{}
func BasicAddition(x int) int { // 这个不会在最终版本中
return x + 2
}
func (h Add) First(x int) int {
x += 5
return x
}
func (h Add) Second(x int) int {
x += 10
return x
}
addition_test.go
package calculator
import "testing"
func TestBasicAddition(t *testing.T) {
x := 30
if y := BasicAddition(x); y != 32 {
t.Errorf("Mine is %v", y)
}
}
func TestFirst(t *testing.T) {
x := 10
a := Add{}
if y := a.First(x); y != 15 {
t.Errorf("First is %v", y)
}
}
输出:
$ go test -v
=== RUN TestBasicAddition
--- PASS: TestBasicAddition (0.00s)
=== RUN TestFirst
--- PASS: TestFirst (0.00s)
PASS
ok so/calculator 0.002s
$
英文:
First
is a method on type Add
. For example,
a := Add{}
a.First(x)
addition.go
package calculator
type Add struct{}
func BasicAddition(x int) int { // this won't be in the final release
return x + 2
}
func (h Add) First(x int) int {
x += 5
return x
}
func (h Add) Second(x int) int {
x += 10
return x
}
addition_test.go
package calculator
import "testing"
func TestBasicAddition(t *testing.T) {
x := 30
if y := BasicAddition(x); y != 32 {
t.Errorf("Mine is %v", y)
}
}
func TestFirst(t *testing.T) {
x := 10
a := Add{}
if y := a.First(x); y != 15 {
t.Errorf("First is %v", y)
}
}
Output:
$ go test -v
=== RUN TestBasicAddition
--- PASS: TestBasicAddition (0.00s)
=== RUN TestFirst
--- PASS: TestFirst (0.00s)
PASS
ok so/calculator 0.002s
$
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论