GoExpertProgramming icon indicating copy to clipboard operation
GoExpertProgramming copied to clipboard

Example测试疑似bug

Open RainbowMango opened this issue 4 years ago • 1 comments

源码:

func RangeString() {
	s := "Hello World"
	for i, v := range s {
		fmt.Printf("index: %d, value: %c\n", i, v)
	}
}

Example:

func ExampleRangeString() {
	RangeString()
	// Output:
	// index: 0, value: H
	// index: 1, value: e
	// index: 2, value: l
	// index: 3, value: l
	// index: 4, value: o
	// index: 5, value:
	// index: 6, value: W
	// index: 7, value: o
	// index: 8, value: r
	// index: 9, value: l
	// index: 10, value: d

结果:

--- FAIL: ExampleRangeString (0.00s)
got:
index: 0, value: H
index: 1, value: e
index: 2, value: l
index: 3, value: l
index: 4, value: o
index: 5, value:
index: 6, value: W
index: 7, value: o
index: 8, value: r
index: 9, value: l
index: 10, value: d
want:
index: 0, value: H
index: 1, value: e
index: 2, value: l
index: 3, value: l
index: 4, value: o
index: 5, value:
index: 6, value: W
index: 7, value: o
index: 8, value: r
index: 9, value: l
index: 10, value: d

如果字符串中包含空格,测试将失败。原因未明。

RainbowMango avatar Apr 09 '20 08:04 RainbowMango

func StringDoubleQuotationMarks() {
	s := "Hi, \nthis is \"RainbowMango\"."
	fmt.Println(s)
}
func ExampleStringDoubleQuotationMarks() {
	StringDoubleQuotationMarks()
	// Output:
	// Hi,
	// this is "RainbowMango".
}

这个测试也会fail。

RainbowMango avatar Apr 13 '20 03:04 RainbowMango