1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| package util
import "testing"
var commTestData []commStruct
type commStruct struct { Group string SizeStr string ExpectSize int64 ExpectSizeStr string }
func initCommonData() { commTestData = []commStruct{ {"B", "1b", B, "1B"}, {"B", "100b", 100 * B, "100B"}, {"KB", "1kb", KB, "1KB"}, {"KB", "100kb", 100 * KB, "100KB"}, {"MB", "1Mb", MB, "1MB"}, {"MB", "1000Mb", 1000 * MB, "1000MB"}, {"TB", "1Tb", TB, "1TB"}, {"PB", "1Pb", PB, "1PB"}, {"unknown", "1G", B, "1B"}, } }
func TestMain(m *testing.M) { initCommonData() m.Run() }
func TestParseSize(t *testing.T) { t.Parallel() testData := commTestData for _, data := range testData { size, sizeStr := ParseSize(data.SizeStr) if size != data.ExpectSize || sizeStr != data.ExpectSizeStr { t.Errorf("不符合预期:%+v\n", data) } } }
func TestParseSizeSub(t *testing.T) { t.Parallel() if testing.Short() { t.Skip("short mod 跳过该测试用例") } testdata := make(map[string][]commStruct, 0) for _, item := range commTestData { group := item.Group _, ok := testdata[group] if !ok { testdata[group] = make([]commStruct, 0) } testdata[group] = append(testdata[group], item) } for k, item := range testdata { t.Run(k, func(t1 *testing.T) { for _, data := range item { size, sizeStr := ParseSize(data.SizeStr) if size != data.ExpectSize || sizeStr != data.ExpectSizeStr { t1.Errorf("不符合预期:%+v\n", data) } } }) } }
|