Codebase list golang-github-vbauerster-mpb / 3443b0d draw_test.go
3443b0d

Tree @3443b0d (Download .tar.gz)

draw_test.go @3443b0draw · history · blame

package mpb

import (
	"reflect"
	"testing"
)

func TestFillBar(t *testing.T) {
	tests := []struct {
		termWidth int
		barWidth  int
		total     int64
		current   int64
		barRefill *Refill
		want      []byte
	}{
		{
			termWidth: 1,
			barWidth:  100,
			want:      []byte{},
		},
		{
			termWidth: 2,
			barWidth:  100,
			total:     100,
			current:   20,
			want:      []byte("[]"),
		},
		{
			termWidth: 20,
			barWidth:  100,
			total:     100,
			current:   20,
			want:      []byte("[===>--------------]"),
		},
		{
			termWidth: 50,
			barWidth:  100,
			total:     100,
			current:   20,
			want:      []byte("[=========>--------------------------------------]"),
		},
		{
			termWidth: 100,
			barWidth:  100,
			total:     100,
			current:   0,
			want:      []byte("[--------------------------------------------------------------------------------------------------]"),
		},
		{
			termWidth: 100,
			barWidth:  100,
			total:     100,
			current:   1,
			want:      []byte("[>-------------------------------------------------------------------------------------------------]"),
		},
		{
			termWidth: 100,
			barWidth:  100,
			total:     100,
			current:   40,
			want:      []byte("[======================================>-----------------------------------------------------------]"),
		},
		{
			termWidth: 100,
			barWidth:  100,
			total:     100,
			current:   40,
			barRefill: &Refill{'+', 32},
			want:      []byte("[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]"),
		},
		{
			termWidth: 100,
			barWidth:  100,
			total:     100,
			current:   99,
			want:      []byte("[================================================================================================>-]"),
		},
		{
			termWidth: 100,
			barWidth:  100,
			total:     100,
			current:   100,
			want:      []byte("[==================================================================================================]"),
		},
	}

	prependWs := newWidthSync(nil, 1, 0)
	appendWs := newWidthSync(nil, 1, 0)
	for _, test := range tests {
		s := newTestState()
		s.width = test.barWidth
		s.total = test.total
		s.current = test.current
		if test.barRefill != nil {
			s.refill = test.barRefill
		}
		got := draw(s, test.termWidth, prependWs, appendWs)
		if !reflect.DeepEqual(test.want, got) {
			t.Errorf("Want: %q, Got: %q\n", test.want, got)
		}
	}
}

func newTestState() *state {
	s := &state{
		trimLeftSpace:  true,
		trimRightSpace: true,
	}
	s.updateFormat("[=>-]")
	return s
}