gotest.tools/gotestsum@v1.11.0/testjson/dotformat_test.go (about) 1 package testjson 2 3 import ( 4 "bytes" 5 "math/rand" 6 "runtime" 7 "testing" 8 "testing/quick" 9 "time" 10 "unicode/utf8" 11 12 "gotest.tools/gotestsum/internal/dotwriter" 13 "gotest.tools/gotestsum/internal/text" 14 "gotest.tools/v3/assert" 15 "gotest.tools/v3/assert/cmp" 16 "gotest.tools/v3/golden" 17 "gotest.tools/v3/skip" 18 ) 19 20 func TestScanTestOutput_WithDotsFormatter(t *testing.T) { 21 skip.If(t, runtime.GOOS == "windows") 22 23 out := new(bytes.Buffer) 24 dotfmt := &dotFormatter{ 25 pkgs: make(map[string]*dotLine), 26 writer: dotwriter.New(out), 27 termWidth: 80, 28 } 29 shim := newFakeHandler(dotfmt, "input/go-test-json") 30 _, err := ScanTestOutput(shim.Config(t)) 31 assert.NilError(t, err) 32 33 actual := text.ProcessLines(t, out, text.OpRemoveSummaryLineElapsedTime) 34 golden.Assert(t, actual, "format/dots-v2.out") 35 golden.Assert(t, shim.err.String(), "input/go-test-json.err") 36 } 37 38 func TestFmtDotElapsed(t *testing.T) { 39 var testcases = []struct { 40 cached bool 41 elapsed time.Duration 42 expected string 43 }{ 44 { 45 elapsed: 999 * time.Microsecond, 46 expected: " 999µs ", 47 }, 48 { 49 elapsed: 7 * time.Millisecond, 50 expected: " 7ms ", 51 }, 52 { 53 cached: true, 54 elapsed: time.Millisecond, 55 expected: " 🖴 ", 56 }, 57 { 58 elapsed: 3 * time.Hour, 59 expected: " ⏳ ", 60 }, 61 { 62 elapsed: 14 * time.Millisecond, 63 expected: " 14ms ", 64 }, 65 { 66 elapsed: 333 * time.Millisecond, 67 expected: " 333ms ", 68 }, 69 { 70 elapsed: 1337 * time.Millisecond, 71 expected: " 1.33s ", 72 }, 73 { 74 elapsed: 14821 * time.Millisecond, 75 expected: " 14.8s ", 76 }, 77 { 78 elapsed: time.Minute + 59*time.Second, 79 expected: " 1m59s ", 80 }, 81 { 82 elapsed: 59*time.Minute + 59*time.Second, 83 expected: " 59m0s ", 84 }, 85 { 86 elapsed: 148213 * time.Millisecond, 87 expected: " 2m28s ", 88 }, 89 { 90 elapsed: 1482137 * time.Millisecond, 91 expected: " 24m0s ", 92 }, 93 } 94 95 for _, tc := range testcases { 96 t.Run(tc.expected, func(t *testing.T) { 97 pkg := &Package{ 98 cached: tc.cached, 99 elapsed: tc.elapsed, 100 } 101 actual := fmtDotElapsed(pkg) 102 assert.Check(t, cmp.Equal(utf8.RuneCountInString(actual), 7)) 103 assert.Equal(t, actual, tc.expected) 104 }) 105 } 106 } 107 108 func TestFmtDotElapsed_RuneCountProperty(t *testing.T) { 109 f := func(d time.Duration) bool { 110 pkg := &Package{ 111 Passed: []TestCase{{Elapsed: d}}, 112 } 113 actual := fmtDotElapsed(pkg) 114 width := utf8.RuneCountInString(actual) 115 if width == 7 { 116 return true 117 } 118 t.Logf("actual %v (width %d)", actual, width) 119 return false 120 } 121 122 seed := time.Now().Unix() 123 t.Log("seed", seed) 124 assert.Assert(t, quick.Check(f, &quick.Config{ 125 MaxCountScale: 2000, 126 Rand: rand.New(rand.NewSource(seed)), 127 })) 128 } 129 130 func TestNewDotFormatter(t *testing.T) { 131 buf := new(bytes.Buffer) 132 ef := newDotFormatter(buf, FormatOptions{}) 133 134 d, ok := ef.(*dotFormatter) 135 skip.If(t, !ok, "no terminal width") 136 assert.Assert(t, d.termWidth != 0) 137 }