返回具有测试期望的结构体映射。

huangapple go评论75阅读模式
英文:

Return map of structs with testing expectations Go

问题

如何声明一个结构体的映射(map)?以下是firstFactorial_tests.go文件的代码示例:

package firstFactorial

import (
    "testing"

    "github.com/google/go-cmp/cmp"
)

func useCases() map[string]struct {
    input int
    want  int
} {
    tests := map[string]struct {
        input int
        want  int
    }{
        "regular_number": {input: 3, want: 6},
        "high_number":    {input: 18, want: 6402373705728000},
        "zero":           {input: 0, want: 1},
        "one":            {input: 1, want: 1},
    }

    return tests
}

func TestRegular(t *testing.T) {
    for name, tc := range useCases() {
        t.Run(name, func(t *testing.T) {
            got := firstFactorial(tc.input)
            if diff := cmp.Diff(tc.want, got); diff != "" {
                t.Fatalf("%s: expected: %v, got %v", name, tc.want, got)
            }
        })
    }
}

func TestRecursive(t *testing.T) {
    for name, tc := range useCases() {
        t.Run(name, func(t *testing.T) {
            got := firstFactorialRecursive(tc.input, 1)
            if diff := cmp.Diff(tc.want, got); diff != "" {
                t.Fatalf("%s: expected: %v, got %v", name, tc.want, got)
            }
        })
    }
}

运行结果如下:

$ go test
# github.com/PZ01/coding-problems-go/firstFactorial
firstFactorial_test.go:10:11: expected type, found ':='
FAIL    github.com/PZ01/coding-problems-go/firstFactorial [setup failed]

源文件如下:

package firstFactorial

func firstFactorial(num int) int {
    factorial := 1

    for num > 0 {
        factorial *= num
        num--
    }

    return factorial
}

func firstFactorialRecursive(num int, factorial int) int {
    if num <= 0 {
        return factorial
    }

    factorial *= num
    num--

    return firstFactorialRecursive(num, factorial)
}

另外,如果有关于如何使代码更紧凑的建议,我会很乐意提供帮助。

英文:

How do you declare a map of structs? The following firstFactorial_tests.go file:

package firstFactorial

import (
    &quot;testing&quot;

    &quot;github.com/google/go-cmp/cmp&quot;
)

func useCases() map[string]struct {
    tests := map[string]struct {
        input int
        want int
    }{
        &quot;regular_number&quot;: {input: 3, want: 6},
        &quot;high_number&quot;: {input: 18, want: 6.402373705728e+15},
        &quot;zero&quot;: {input: 0, want: 1},
        &quot;one&quot;: {input: 1, want: 1},
    }

    return tests
}

func TestRegular(t *testing.T) {
    for name, tc := range useCases() {
        t.Run(name, func(t *testing.T) {
            got := firstFactorial(tc.input)
            if diff := cmp.Diff(tc.want, got); diff != &quot;&quot; {
                t.Fatalf(&quot;%s: expected: %v, got %v&quot;, name, tc.want, got)
            }
        })
    }
}

func TestRecursive(t *testing.T) {
    for name, tc := range useCases() {
        t.Run(name, func(t *testing.T) {
            got := firstFactorialRecursive(tc.input, 1)
            if diff := cmp.Diff(tc.want, got); diff != &quot;&quot; {
                t.Fatalf(&quot;%s: expected: %v, got %v&quot;, name, tc.want, got)
            }
        })
    }
}

Returns the following:

$ go test
# github.com/PZ01/coding-problems-go/firstFactorial
firstFactorial_test.go:10:11: expected type, found &#39;:=&#39;
FAIL    github.com/PZ01/coding-problems-go/firstFactorial [setup failed]

Source file:

package firstFactorial

func firstFactorial(num int) int {
    factorial := 1

    for num &gt; 0 {
        factorial *= num
        num--
    }

    return factorial
}

func firstFactorialRecursive(num int, factorial int) int {
    if num &lt;= 0 {
        return factorial
    }

    factorial *= num
    num--

    return firstFactorialRecursive(num, factorial);
}

Also, any suggestion on making this more compact is appreciated.


Golang 1.7+

答案1

得分: 1

使用匿名结构体作为map值的定义。

func useCases() map[string]struct{ input, want int } {
	return map[string]struct{ input, want int }{
		"regular_number": {input: 3, want: 6},
		"high_number":    {input: 18, want: 6.402373705728e+15},
		"zero":           {input: 0, want: 1},
		"one":            {input: 1, want: 1},
	}
}

PLAYGROUND

英文:

Use a anonymous structs as map value definition.

func useCases() map[string]struct{ input, want int } {
	return map[string]struct{ input, want int }{
		&quot;regular_number&quot;: {input: 3, want: 6},
		&quot;high_number&quot;:    {input: 18, want: 6.402373705728e+15},
		&quot;zero&quot;:           {input: 0, want: 1},
		&quot;one&quot;:            {input: 1, want: 1},
	}
}

<kbd>PLAYGROUND</kbd>

huangapple
  • 本文由 发表于 2021年10月12日 02:38:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/69531021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定