亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何為 sort.Slice() 編寫測試?

如何為 sort.Slice() 編寫測試?

Go
慕仙森 2023-01-03 16:17:39
鑒于如何sort.Slice(array, func(int, int) bool)在傳入函數中使用數組,您將如何為匿名函數編寫測試?來自go 文檔sort.Slice()的示例代碼是:package mainimport (    "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()自己是一種不好的形式;它隨語言一起提供,不應該(需要?)進行測試。
查看完整描述

2 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

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()自己是一種不好的形式;它隨語言一起提供,不應該(需要?)進行測試。


查看完整回答
反對 回復 2023-01-03
?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

胡思亂想后,我寫了這段代碼。它使用工廠來創建方法,以便切片包含在函數的閉包中。

我把它放在一個要點中以便于玩弄:https ://gist.github.com/docwhat/e3b13265d24471651e02f7d7a42e7d2c

// main.go

package main


import (

    "fmt"

    "sort"

)


type Person struct {

    Name string

    Age  int

}


func comparePeopleByName(people []Person) func(int, int) bool {

    return func(i, j int) bool {

        return people[i].Name < people[j].Name

    }

}


func comparePeopleByAge(people []Person) func(int, int) bool {

    return func(i, j int) bool {

        return people[i].Age < people[j].Age

    }

}


func main() {

    people := []Person{

        {"Gopher", 7},

        {"Alice", 55},

        {"Vera", 24},

        {"Bob", 75},

    }

    sort.Slice(people, comparePeopleByName(people))

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


    sort.Slice(people, comparePeopleByAge(people))

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

}

// main_test.go

package main


import "testing"


func TestComparePeopleByName(t *testing.T) {

    testCases := []struct {

        desc     string

        a, b     Person

        expected bool

    }{

        {"a < b", Person{"bob", 1}, Person{"krabs", 2}, true},

        {"a > b", Person{"krabs", 1}, Person{"bob", 2}, false},

        {"a = a", Person{"plankton", 1}, Person{"plankton", 2}, false},

    }


    for _, testCase := range testCases {

        t.Run(testCase.desc, func(t *testing.T) {

            people := []Person{testCase.a, testCase.b}

            got := comparePeopleByName(people)(0, 1)

            if testCase.expected != got {

                t.Errorf("expected %v, got %v", testCase.expected, got)

            }

        })

    }

}


func TestComparePeopleByAge(t *testing.T) {

    testCases := []struct {

        desc     string

        a, b     Person

        expected bool

    }{

        {"a < b", Person{"sandy", 10}, Person{"patrick", 20}, true},

        {"a > b", Person{"sandy", 30}, Person{"patrick", 20}, false},

        {"a = b", Person{"sandy", 90}, Person{"patrick", 90}, false},

    }


    for _, testCase := range testCases {

        t.Run(testCase.desc, func(t *testing.T) {

            people := []Person{testCase.a, testCase.b}

            got := comparePeopleByAge(people)(0, 1)

            if testCase.expected != got {

                t.Errorf("expected %v, got %v", testCase.expected, got)

            }

        })

    }

}


查看完整回答
反對 回復 2023-01-03
  • 2 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號