wings/internal/progress/progress_test.go

51 lines
1.3 KiB
Go
Raw Normal View History

2022-11-15 01:25:01 +00:00
package progress_test
import (
"bytes"
"testing"
2022-11-15 01:25:01 +00:00
"github.com/franela/goblin"
"github.com/pterodactyl/wings/internal/progress"
)
func TestProgress(t *testing.T) {
2022-11-15 01:25:01 +00:00
g := goblin.Goblin(t)
g.Describe("Progress", func() {
g.It("properly initializes", func() {
2022-11-15 01:25:01 +00:00
total := uint64(1000)
p := progress.NewProgress(total)
g.Assert(p).IsNotNil()
g.Assert(p.Total()).Equal(total)
2022-11-15 01:25:01 +00:00
g.Assert(p.Written()).Equal(uint64(0))
})
g.It("increments written when Write is called", func() {
v := []byte("hello")
2022-11-15 01:25:01 +00:00
p := progress.NewProgress(1000)
_, err := p.Write(v)
g.Assert(err).IsNil()
2022-11-15 01:25:01 +00:00
g.Assert(p.Written()).Equal(uint64(len(v)))
})
g.It("renders a progress bar", func() {
v := bytes.Repeat([]byte{' '}, 100)
2022-11-15 01:25:01 +00:00
p := progress.NewProgress(1000)
_, err := p.Write(v)
g.Assert(err).IsNil()
2022-11-15 01:25:01 +00:00
g.Assert(p.Written()).Equal(uint64(len(v)))
g.Assert(p.Progress(25)).Equal("[== ] 100 B / 1000 B")
})
g.It("renders a progress bar when written exceeds total", func() {
v := bytes.Repeat([]byte{' '}, 1001)
2022-11-15 01:25:01 +00:00
p := progress.NewProgress(1000)
_, err := p.Write(v)
g.Assert(err).IsNil()
2022-11-15 01:25:01 +00:00
g.Assert(p.Written()).Equal(uint64(len(v)))
g.Assert(p.Progress(25)).Equal("[=========================] 1001 B / 1000 B")
})
})
}