File: example_single_bar_test.go

package info (click to toggle)
tiup 1.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,384 kB
  • sloc: sh: 1,988; makefile: 138; sql: 16
file content (86 lines) | stat: -rw-r--r-- 1,474 bytes parent folder | download
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
77
78
79
80
81
82
83
84
85
86
package progress_test

import (
	"errors"
	"strconv"
	"testing"
	"time"

	"github.com/pingcap/tiup/pkg/tui/progress"
)

func ExampleSingleBar() {
	b := progress.NewSingleBar("Prefix")

	b.UpdateDisplay(&progress.DisplayProps{
		Prefix: "Prefix",
		Suffix: "Suffix",
	})

	n := 3

	go func() {
		time.Sleep(time.Second)
		for i := range n {
			b.UpdateDisplay(&progress.DisplayProps{
				Prefix: "Prefix" + strconv.Itoa(i),
				Suffix: "Suffix" + strconv.Itoa(i),
			})
			time.Sleep(time.Second)
		}
	}()

	b.StartRenderLoop()

	time.Sleep(time.Second * time.Duration(n+1))

	b.UpdateDisplay(&progress.DisplayProps{
		Mode: progress.ModeDone,
		// Mode:   progress.ModeError,
		Prefix: "Prefix",
	})

	b.StopRenderLoop()
}

func ExampleSingleBar_err() {
	b := progress.NewSingleBar("Prefix")

	b.UpdateDisplay(&progress.DisplayProps{
		Prefix: "Prefix",
		Suffix: "Suffix",
	})

	n := 3

	go func() {
		time.Sleep(time.Second)
		for i := range n {
			b.UpdateDisplay(&progress.DisplayProps{
				Prefix: "Prefix" + strconv.Itoa(i),
				Suffix: "Suffix" + strconv.Itoa(i),
			})
			time.Sleep(time.Second)
		}
	}()

	b.StartRenderLoop()

	time.Sleep(time.Second * time.Duration(n+1))

	b.UpdateDisplay(&progress.DisplayProps{
		Mode:   progress.ModeError,
		Prefix: "Prefix",
		Detail: errors.New("expected failure").Error(),
	})

	b.StopRenderLoop()
}

func TestExampleOutput(t *testing.T) {
	if !testing.Verbose() {
		return
	}
	ExampleSingleBar()
	ExampleSingleBar_err()
}