Golang标准库:testing包使用示例一
  0SnbOly3LC5t 2023年12月07日 22 0


package testing_test

import (
	"bytes"
	. "chapter09/testing"
	"html/template"
	"testing"
)

var pairs = []struct {
	k string
	v string
}{
	{"polaris", "张新华"},
	{"studygolang", "Go语言中文网"},
	{"stdlib", "Go语言标准库"},
	{"polaris1", "张新华1"},
	{"studygolang1", "Go语言中文网1"},
	{"stdlib1", "Go语言标准库1"},
	{"polaris2", "张新华2"},
	{"studygolang2", "Go语言中文网2"},
	{"stdlib2", "Go语言标准库2"},
	{"polaris3", "张新华3"},
	{"studygolang3", "Go语言中文网3"},
	{"stdlib3", "Go语言标准库3"},
	{"polaris4", "张新华4"},
	{"studygolang4", "Go语言中文网4"},
	{"stdlib4", "Go语言标准库4"},
}

// 注意 TestWriteToMap 需要在 TestReadFromMap 之前
func TestWriteToMap(t *testing.T) {
	t.Parallel()
	for _, tt := range pairs {
		WriteToMap(tt.k, tt.v)
	}
}

func TestReadFromMap(t *testing.T) {
	t.Parallel()
	for _, tt := range pairs {
		actual := ReadFromMap(tt.k)
		if actual != tt.v {
			t.Errorf("the value of key(%s) is %s, expected: %s", tt.k, actual, tt.v)
		}
	}
}

func BenchmarkTemplateParallel(b *testing.B) {
	templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))
	b.RunParallel(func(pb *testing.PB) {
		var buf bytes.Buffer
		for pb.Next() {
			buf.Reset()
			templ.Execute(&buf, "World")
		}
	})
}


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月07日 0

暂无评论

推荐阅读
  uGYzDadp0Cs7   2024年04月18日   79   0   0 Go
  hyrB1Ag4eVs8   2024年04月15日   71   0   0 Go
  dHUS172Lkv6A   2024年05月08日   175   0   0 Go
  YFCZjJLTjJgW   2024年05月04日   52   0   0 Go
  YFCZjJLTjJgW   2024年05月17日   59   0   0 Go
  uGYzDadp0Cs7   2024年04月16日   119   0   0 Go
  YFCZjJLTjJgW   2024年05月17日   59   0   0 Go
0SnbOly3LC5t