How to test the equivalence of maps in Golang?

前端 未结 8 1337
情歌与酒
情歌与酒 2021-01-30 00:55

I have a table-driven test case like this one:

func CountWords(s string) map[string]int

func TestCountWords(t *testing.T) {
  var tests = []struct {
    input s         


        
8条回答
  •  离开以前
    2021-01-30 01:20

    Simplest way:

        assert.InDeltaMapValues(t, got, want, 0.0, "Word count wrong. Got %v, want %v", got, want)
    

    Example:

    import (
        "github.com/stretchr/testify/assert"
        "testing"
    )
    
    func TestCountWords(t *testing.T) {
        got := CountWords("hola hola que tal")
    
        want := map[string]int{
            "hola": 2,
            "que": 1,
            "tal": 1,
        }
    
        assert.InDeltaMapValues(t, got, want, 0.0, "Word count wrong. Got %v, want %v", got, want)
    }
    

提交回复
热议问题