如何在Go中编写针对用户输入的测试

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

How to write tests against user input in Go

问题

如何对来自fmt.Scan/Scanf/Scanln的用户输入进行测试?

例如,我如何测试函数输入是否接受“4 5\n”和“1 2 3 4\n”并从STDIN返回n == 5和array == [1, 2, 3, 4]。

package main

import (
	"fmt"
)

// input从用户获取一个数组。
func input() (m int, array []int) {
	fmt.Print("输入数组的大小n和差值m:")
	var n int
	_, err := fmt.Scanf("%d %d", &n, &m)
	if err != nil {
		panic(err)
	}

	fmt.Print("输入数组作为以空格分隔的字符串:")
	array = make([]int, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Scan(&array[i])
	}

	return m, array
}

func main() {
	m, array := input()
	fmt.Println(m, array)
}
英文:

How would I test against user input from fmt.Scan/Scanf/Scanln?

For example how could I test that the function input will accept "4 5\n" and "1 2 3 4\n" from STDIN and return n == 5 and array == [1, 2, 3, 4].

package main

import (
	&quot;fmt&quot;
)

// input gets an array from the user.
func input() (m int, array []int) {
	fmt.Print(&quot;Enter the size of the array, n, and the difference, m: &quot;)
	var n int
	_, err := fmt.Scanf(&quot;%d %d&quot;, &amp;n, &amp;m)
	if err != nil {
		panic(err)
	}

	fmt.Print(&quot;Enter the array as a space seperated string: &quot;)
	array = make([]int, n)
	for i := 0; i &lt; n; i++ {
		_, _ = fmt.Scan(&amp;array[i])
	}

	return m, array
}

func main() {
	m, array := input()
	fmt.Println(m, array)
}

答案1

得分: 20

这是一个非常初步的草稿,用于说明原则。

program.go

package main

import (
	"fmt"
	"os"
)

// input从用户获取一个数组。
func input(in *os.File) (m int, array []int) {
	if in == nil {
		in = os.Stdin
	}

	fmt.Print("输入数组的大小n和差值m:")
	var n int
	_, err := fmt.Fscanf(in, "%d %d", &n, &m)
	if err != nil {
		panic(err)
	}

	fmt.Print("输入数组,以空格分隔:")
	array = make([]int, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Fscan(in, &array[i])
	}

	return m, array
}

func main() {
	m, array := input(nil)
	fmt.Println(m, array)
}

program_test.go

package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"testing"
)

func TestInput(t *testing.T) {
	var (
		n, m  int
		array []int
	)

	in, err := ioutil.TempFile("", "")
	if err != nil {
		t.Fatal(err)
	}
	defer in.Close()

	_, err = io.WriteString(in, "4 5\n"+"1 2 3 4\n")
	if err != nil {
		t.Fatal(err)
	}

	_, err = in.Seek(0, os.SEEK_SET)
	if err != nil {
		t.Fatal(err)
	}

	n, array = input(in)
	if n != 5 || fmt.Sprintf("%v", array) != fmt.Sprintf("%v", []int{1, 2, 3, 4}) {
		t.Error("unexpected results:", n, m, array)
	}
}

输出:

$ go test
ok      command-line-arguments  0.010s
英文:

Here's a very rough draft to illustrate the principle.

program.go

package main

import (
	&quot;fmt&quot;
	&quot;os&quot;
)

// input gets an array from the user.
func input(in *os.File) (m int, array []int) {
	if in == nil {
		in = os.Stdin
	}

	fmt.Print(&quot;Enter the size of the array, n, and the difference, m: &quot;)
	var n int
	_, err := fmt.Fscanf(in, &quot;%d %d&quot;, &amp;n, &amp;m)
	if err != nil {
		panic(err)
	}

	fmt.Print(&quot;Enter the array as a space seperated string: &quot;)
	array = make([]int, n)
	for i := 0; i &lt; n; i++ {
		_, _ = fmt.Fscan(in, &amp;array[i])
	}

	return m, array
}

func main() {
	m, array := input(nil)
	fmt.Println(m, array)
}

program_test.go

package main

import (
	&quot;fmt&quot;
	&quot;io&quot;
	&quot;io/ioutil&quot;
	&quot;os&quot;
	&quot;testing&quot;
)

func TestInput(t *testing.T) {
	var (
		n, m  int
		array []int
	)

	in, err := ioutil.TempFile(&quot;&quot;, &quot;&quot;)
	if err != nil {
		t.Fatal(err)
	}
	defer in.Close()

	_, err = io.WriteString(in, &quot;4 5\n&quot;+&quot;1 2 3 4\n&quot;)
	if err != nil {
		t.Fatal(err)
	}

	_, err = in.Seek(0, os.SEEK_SET)
	if err != nil {
		t.Fatal(err)
	}

	n, array = input(in)
	if n != 5 || fmt.Sprintf(&quot;%v&quot;, array) != fmt.Sprintf(&quot;%v&quot;, []int{1, 2, 3, 4}) {
		t.Error(&quot;unexpected results:&quot;, n, m, array)
	}
}

Output:

$ go test
ok      command-line-arguments  0.010s

答案2

得分: -10

你不能。至少不是那么容易,以至于值得付出努力。

英文:

You can't. At least not so easily, such that, it would be worth the effort.

huangapple
  • 本文由 发表于 2013年7月4日 03:49:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/17456901.html
匿名

发表评论

匿名网友

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

确定