github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/cmd/geth/log_context_test.go (about) 1 package main 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestParseDuration(t *testing.T) { 9 cases := []struct { 10 value string 11 err bool 12 expected time.Duration 13 }{ 14 // valid ones 15 {"123", false, 123 * time.Second}, 16 {"10h", false, 10 * time.Hour}, 17 {"3d", false, 3 * 24 * time.Hour}, 18 {"4w", false, 4 * 7 * 24 * time.Hour}, 19 // same with whitespace 20 {" 123", false, 123 * time.Second}, 21 {"10h ", false, 10 * time.Hour}, 22 {"3d ", false, 3 * 24 * time.Hour}, 23 {" 4w ", false, 4 * 7 * 24 * time.Hour}, 24 {" 4 w ", false, 4 * 7 * 24 * time.Hour}, 25 // upper case 26 {"10H", false, 10 * time.Hour}, 27 {"3D", false, 3 * 24 * time.Hour}, 28 {"4W", false, 4 * 7 * 24 * time.Hour}, 29 // invalid cases 30 {"1y", true, 0}, 31 {"one two", true, 0}, 32 {"d", true, 0}, 33 {"1week", true, 0}, 34 {"10www", true, 0}, 35 {"", true, 0}, 36 {"r2d2", true, 0}, 37 {"2d2d", true, 0}, 38 } 39 40 for _, test := range cases { 41 t.Run(test.value, func(t *testing.T) { 42 actual, err := parseDuration(test.value) 43 if test.err != (err != nil) { 44 t.Errorf("expected error: %v, found error: %v", test.err, err) 45 } 46 if test.expected != actual { 47 t.Errorf("expected: %v, actual: %v", test.expected, actual) 48 } 49 }) 50 } 51 } 52 53 func TestParseSize(t *testing.T) { 54 cases := []struct { 55 value string 56 err bool 57 expected uint64 58 }{ 59 // valid ones 60 {"123", false, 123}, 61 {"10k", false, 10 * 1024}, 62 {"3m", false, 3 * 1024 * 1024}, 63 {"4g", false, 4 * 1024 * 1024 * 1024}, 64 // same with whitespace 65 {"123 ", false, 123}, 66 {" 10k", false, 10 * 1024}, 67 {" 3m", false, 3 * 1024 * 1024}, 68 {" 4g ", false, 4 * 1024 * 1024 * 1024}, 69 {" 4 g ", false, 4 * 1024 * 1024 * 1024}, 70 // upper case 71 {"10K", false, 10 * 1024}, 72 {"3M", false, 3 * 1024 * 1024}, 73 {"4G", false, 4 * 1024 * 1024 * 1024}, 74 // invalid cases 75 {"1t", true, 0}, 76 {"one two", true, 0}, 77 {"d", true, 0}, 78 {"2gigs", true, 0}, 79 {"321petabytes", true, 0}, 80 {"", true, 0}, 81 {"r2d2", true, 0}, 82 {"2d2d", true, 0}, 83 } 84 85 for _, test := range cases { 86 t.Run(test.value, func(t *testing.T) { 87 actual, err := parseSize(test.value) 88 if test.err != (err != nil) { 89 t.Errorf("expected error: %v, found error: %v", test.err, err) 90 } 91 if test.expected != actual { 92 t.Errorf("expected: %v, actual: %v", test.expected, actual) 93 } 94 }) 95 } 96 }