คำถามติดแท็ก 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) …
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.