Skip to content

Commit

Permalink
feat: update the test.Now the actual is what the output would be like…
Browse files Browse the repository at this point in the history
… in the terminal.
  • Loading branch information
chengxilo committed Sep 22, 2024
1 parent efe17f0 commit 3d93361
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 9 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ require (
)

require (
github.com/chengxilo/virtualterm v1.0.3 // indirect
github.com/chengxilo/virtualterm v1.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.25.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
github.com/chengxilo/virtualterm v1.0.3 h1:Vycm/mKGeHuLXA4zK3XsaseOW7VMY6jJ88/9+XHSNcA=
github.com/chengxilo/virtualterm v1.0.3/go.mod h1:wjAbIDvnp6Vc8hQoM7tt6fcdk0NiSaQBSoSRwMIpphs=
github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM=
github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 h1:qGQQKEcAR99REcMpsXCp3lJ03zYT1PkRd3kQGPn9GVg=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
88 changes: 80 additions & 8 deletions progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/chengxilo/virtualterm"
"io"
"log"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -126,11 +127,12 @@ func TestSpinnerFinish(t *testing.T) {
bar.Add(10)
time.Sleep(1 * time.Second)
bar.Finish()
result := buf.String()
expect := "" +
"\r- (10 B, 10 B/s, 10 it/s) [1s] " +
"\r \r" +
"\r| (10 B, 5 B/s, 5 it/s) [2s] "
result, err := virtualterm.Process(buf.String())
if err != nil {
t.Error(err)
}
// the "\r \r"
expect := "| (10 B, 5 B/s, 5 it/s) [2s] "
if result != expect {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
Expand Down Expand Up @@ -213,15 +215,21 @@ func ExampleOptionShowIts_spinner() {
/*
Spinner test with iteration count and iteration rate
*/
vt := virtualterm.NewDefault()
bar := NewOptions(-1,
OptionSetWidth(10),
OptionShowIts(),
OptionShowCount(),
OptionSetWriter(&vt),
)
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(5)

s, err := vt.String()
if err != nil {
log.Fatal(err)
}
fmt.Print(s)
// Output:
// - (5/-, 5 it/s) [1s]
}
Expand Down Expand Up @@ -318,17 +326,20 @@ func ExampleOptionShowBytes_spinner() {
/*
Spinner test with iterations and count
*/
buf := strings.Builder{}
bar := NewOptions(-1,
OptionSetWidth(10),
OptionShowBytes(true),
OptionSetWriter(&buf),
)

bar.Reset()
time.Sleep(1 * time.Second)
// since 10 is the width and we don't know the max bytes
// it will do a infinite scrolling.
bar.Add(11)

result, _ := virtualterm.Process(buf.String())
fmt.Print(result)
// Output:
// - (11 B/s) [1s]
}
Expand Down Expand Up @@ -494,7 +505,11 @@ func TestOptionSetElapsedTime_spinner(t *testing.T) {
bar.Reset()
time.Sleep(1 * time.Second)
bar.Add(5)
result := strings.TrimSpace(buf.String())
result, err := virtualterm.Process(buf.String())
result = strings.TrimSpace(result)
if err != nil {
t.Fatal(err)
}
expect := "- (5/-, 5 it/s)"
if result != expect {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
Expand Down Expand Up @@ -928,3 +943,60 @@ func TestProgressBar_StartWithoutRender(t *testing.T) {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
}

func TestOptionSetSpinnerChangeInterval(t *testing.T) {
interval := 1000 * time.Millisecond
vt := virtualterm.NewDefault()
actuals := make([]string, 0, 8)
expecteds := []string{
"◐ test [0s]",
"◓ test [1s]",
"◑ test [2s]",
"◒ test [3s]",
"◐ test [4s]",
"◓ test [5s]",
"◑ test [6s]",
"◒ test [7s]",
}
bar := NewOptions(-1,
OptionSetDescription("test"),
OptionSpinnerType(7),
OptionSetWriter(&vt),
OptionSetSpinnerChangeInterval(interval))
bar.Add(1)
for i := 0; i < 8; i++ {
s, _ := vt.String()
s = strings.TrimSpace(s)
actuals = append(actuals, s)
// sleep 50 ms more to make sure to go to next interval each time
time.Sleep(1050 * time.Millisecond)
}
for i := range actuals {
assert.Equal(t, expecteds[i], actuals[i])
}
}

func TestOptionSetSpinnerChangeIntervalZero(t *testing.T) {
vt := virtualterm.NewDefault()
bar := NewOptions(-1,
OptionSetDescription("test"),
OptionSpinnerType(7),
OptionSetWriter(&vt),
OptionSetSpinnerChangeInterval(0))
actuals := make([]string, 0, 5)
expected := []string{
"◐ test [0s]",
"◓ test [1s]",
"◑ test [2s]",
"◒ test [3s]",
"◐ test [4s]",
}
for i := 0; i < 5; i++ {
bar.Add(1)
s, _ := vt.String()
s = strings.TrimSpace(s)
}
for i := range actuals {
assert.Equal(t, expected[i], actuals[i])
}
}

0 comments on commit 3d93361

Please sign in to comment.