| 12 | ) |
| 13 | |
| 14 | func TestTimer_StartStopElapsed(t *testing.T) { |
| 15 | tm := NewTimer() |
| 16 | if tm.IsRunning() { |
| 17 | t.Fatal("new timer should not be running") |
| 18 | } |
| 19 | |
| 20 | var ticks int |
| 21 | tm.Start(context.Background(), func(time.Duration) { ticks++ }) |
| 22 | if !tm.IsRunning() { |
| 23 | t.Fatal("timer should be running after Start") |
| 24 | } |
| 25 | if tm.Elapsed() < 0 { |
| 26 | t.Error("elapsed should be non-negative while running") |
| 27 | } |
| 28 | time.Sleep(20 * time.Millisecond) |
| 29 | |
| 30 | d := tm.Stop() |
| 31 | if d <= 0 { |
| 32 | t.Errorf("stopped duration should be positive, got %v", d) |
| 33 | } |
| 34 | if tm.IsRunning() { |
| 35 | t.Error("timer should not be running after Stop") |
| 36 | } |
| 37 | // Under `go test` stdout is a pipe, not a TTY, so the animation gate keeps |
| 38 | // the displayFunc from firing — no spinner noise leaks to captured output. |
| 39 | if ticks != 0 { |
| 40 | t.Errorf("displayFunc must not fire when stdout is not a terminal, got %d", ticks) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestTimer_StopWhenNotRunning(t *testing.T) { |
| 45 | tm := NewTimer() |