登录
首页 >  Golang >  Go问答

编写 sort.Slice() 的单元测试方法

来源:stackoverflow

时间:2024-02-28 16:54:26 217浏览 收藏

从现在开始,努力学习吧!本文《编写 sort.Slice() 的单元测试方法》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

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

sort.slice() 的 go 文档中的示例代码是:

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() 本身是一种不好的形式;它随语言一起提供,不应该(需要?)进行测试。


正确答案


将要测试的功能移至命名函数。测试该功能。

type person struct {
    name string
    age  int
}

func personlessbyname(people []person, i, j int) bool { 
        return people[i].name < people[j].name 
}

func personlessbyage(people []person, i, j int) bool {
    return people[i].age < people[j].age
}

在调用 sort.slice 时使用该函数:

sort.slice(people, func(i, j int) bool { return personlessbyname(people, i, j) })

sort.slice(people, func(i, j int) bool { return personlessbyage(people, i, j) })

这是一个测试:

func testcomparepeople(t *testing.t) {
    cases := []struct {
        desc     string
        a, b     person
        fn       func([]person, int, int) bool
        expected bool
    }{
        {"name a < b", person{"bill", 10}, person{"melinda", 20}, personlessbyname, true},
        {"name a > b", person{"melinda", 30}, person{"bill", 20}, personlessbyname, false},
        {"name a = b", person{"melinda", 90}, person{"melinda", 90}, personlessbyname, false},
        {"age a < b", person{"melinda", 10}, person{"bill", 20}, personlessbyage, true},
        {"age a > b", person{"melinda", 30}, person{"bill", 20}, personlessbyage, false},
        {"age a = b", person{"melinda", 90}, person{"bill", 90}, personlessbyage, false},
    }
    for _, c := range cases {
        people := []person{c.a, c.b}
        got := c.fn(people, 0, 1)
        if c.expected != got {
            t.errorf("%s: expected %v, got %v", c.desc, c.expected, got)
        }
    }
}

经过一番折腾,我写了这段代码。它使用工厂来创建方法,以便切片包含在函数的闭包中。

我把它放在一个要点中以便于使用: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)
            }
        })
    }
}

本篇关于《编写 sort.Slice() 的单元测试方法》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>