如何为 sort.Slice() 编写测试?

鉴于如何sort.Slice(array, func(int, int) bool)在传入函数中使用数组,您将如何为匿名函数编写测试?

来自go 文档sort.Slice()的示例代码是:

package main


import (

    "fmt"

    "sort"

)


func main() {

    people := []struct {

        Name string

        Age  int

    }{

        {"Gopher", 7},

        {"Alice", 55},

        {"Vera", 24},

        {"Bob", 75},

    }

    sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name })

    fmt.Println("By name:", people)


    sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age })

    fmt.Println("By age:", people)

}

如果将匿名函数分解为两个单独的函数(例如,comparePeopleByName(i, j int)和comparePeopleByAge(i, j, int)),您将如何测试它们?


将函数绑定到闭包上似乎是个奇怪的想法。


注意:我认为测试sort.Slice()自己是一种不好的形式;它随语言一起提供,不应该(需要?)进行测试。


慕仙森
浏览 96回答 2
2回答

慕斯709654

package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "sort")func main() {&nbsp; &nbsp; people := []struct {&nbsp; &nbsp; &nbsp; &nbsp; Name string&nbsp; &nbsp; &nbsp; &nbsp; Age&nbsp; int&nbsp; &nbsp; }{&nbsp; &nbsp; &nbsp; &nbsp; {"Gopher", 7},&nbsp; &nbsp; &nbsp; &nbsp; {"Alice", 55},&nbsp; &nbsp; &nbsp; &nbsp; {"Vera", 24},&nbsp; &nbsp; &nbsp; &nbsp; {"Bob", 75},&nbsp; &nbsp; }&nbsp; &nbsp; sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name })&nbsp; &nbsp; fmt.Println("By name:", people)&nbsp; &nbsp; sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age })&nbsp; &nbsp; fmt.Println("By age:", people)}如果将匿名函数分解为两个单独的函数(例如,comparePeopleByName(i, j int)和comparePeopleByAge(i, j, int)),您将如何测试它们?将函数绑定到闭包上似乎是个奇怪的想法。注意:我认为测试sort.Slice()自己是一种不好的形式;它随语言一起提供,不应该(需要?)进行测试。

桃花长相依

胡思乱想后,我写了这段代码。它使用工厂来创建方法,以便切片包含在函数的闭包中。我把它放在一个要点中以便于玩弄:https ://gist.github.com/docwhat/e3b13265d24471651e02f7d7a42e7d2c// main.gopackage mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "sort")type Person struct {&nbsp; &nbsp; Name string&nbsp; &nbsp; Age&nbsp; int}func comparePeopleByName(people []Person) func(int, int) bool {&nbsp; &nbsp; return func(i, j int) bool {&nbsp; &nbsp; &nbsp; &nbsp; return people[i].Name < people[j].Name&nbsp; &nbsp; }}func comparePeopleByAge(people []Person) func(int, int) bool {&nbsp; &nbsp; return func(i, j int) bool {&nbsp; &nbsp; &nbsp; &nbsp; return people[i].Age < people[j].Age&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; people := []Person{&nbsp; &nbsp; &nbsp; &nbsp; {"Gopher", 7},&nbsp; &nbsp; &nbsp; &nbsp; {"Alice", 55},&nbsp; &nbsp; &nbsp; &nbsp; {"Vera", 24},&nbsp; &nbsp; &nbsp; &nbsp; {"Bob", 75},&nbsp; &nbsp; }&nbsp; &nbsp; sort.Slice(people, comparePeopleByName(people))&nbsp; &nbsp; fmt.Println("By name:", people)&nbsp; &nbsp; sort.Slice(people, comparePeopleByAge(people))&nbsp; &nbsp; fmt.Println("By age:", people)}// main_test.gopackage mainimport "testing"func TestComparePeopleByName(t *testing.T) {&nbsp; &nbsp; testCases := []struct {&nbsp; &nbsp; &nbsp; &nbsp; desc&nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; a, b&nbsp; &nbsp; &nbsp;Person&nbsp; &nbsp; &nbsp; &nbsp; expected bool&nbsp; &nbsp; }{&nbsp; &nbsp; &nbsp; &nbsp; {"a < b", Person{"bob", 1}, Person{"krabs", 2}, true},&nbsp; &nbsp; &nbsp; &nbsp; {"a > b", Person{"krabs", 1}, Person{"bob", 2}, false},&nbsp; &nbsp; &nbsp; &nbsp; {"a = a", Person{"plankton", 1}, Person{"plankton", 2}, false},&nbsp; &nbsp; }&nbsp; &nbsp; for _, testCase := range testCases {&nbsp; &nbsp; &nbsp; &nbsp; t.Run(testCase.desc, func(t *testing.T) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; people := []Person{testCase.a, testCase.b}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; got := comparePeopleByName(people)(0, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if testCase.expected != got {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.Errorf("expected %v, got %v", testCase.expected, got)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }}func TestComparePeopleByAge(t *testing.T) {&nbsp; &nbsp; testCases := []struct {&nbsp; &nbsp; &nbsp; &nbsp; desc&nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; a, b&nbsp; &nbsp; &nbsp;Person&nbsp; &nbsp; &nbsp; &nbsp; expected bool&nbsp; &nbsp; }{&nbsp; &nbsp; &nbsp; &nbsp; {"a < b", Person{"sandy", 10}, Person{"patrick", 20}, true},&nbsp; &nbsp; &nbsp; &nbsp; {"a > b", Person{"sandy", 30}, Person{"patrick", 20}, false},&nbsp; &nbsp; &nbsp; &nbsp; {"a = b", Person{"sandy", 90}, Person{"patrick", 90}, false},&nbsp; &nbsp; }&nbsp; &nbsp; for _, testCase := range testCases {&nbsp; &nbsp; &nbsp; &nbsp; t.Run(testCase.desc, func(t *testing.T) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; people := []Person{testCase.a, testCase.b}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; got := comparePeopleByAge(people)(0, 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if testCase.expected != got {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.Errorf("expected %v, got %v", testCase.expected, got)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go