Questions tagged «table-driven»

8
如何在Golang中测试地图的等效性?
我有一个像这样的表驱动测试用例: func CountWords(s string) map[string]int func TestCountWords(t *testing.T) { var tests = []struct { input string want map[string]int }{ {"foo", map[string]int{"foo":1}}, {"foo bar foo", map[string]int{"foo":2,"bar":1}}, } for i, c := range tests { got := CountWords(c.input) // TODO test whether c.want == got } } 我可以检查长度是否相同,并编写一个循环来检查每个键值对是否相同。但是,当我想将其用于其他类型的地图时(例如map[string]string),我必须再次编写此检查。 我最终要做的是,将地图转换为字符串并比较了字符串: func checkAsStrings(a,b interface{}) …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.