package mpb_test

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"strings"
	"testing"

	"github.com/vbauerster/mpb"
)

func TestPrependName(t *testing.T) {
	var buf bytes.Buffer
	p := mpb.New().SetOut(&buf)
	wantName := "TestBar"
	bar := p.AddBar(100).PrependName(wantName, 0, 0)
	for i := 0; i < 100; i++ {
		bar.Incr(1)
	}
	p.Stop()
	if !strings.Contains(buf.String(), wantName) {
		t.Errorf("%q not found in bar\n", wantName)
	}
}

func TestPrependCounters(t *testing.T) {
	var buf bytes.Buffer
	p := mpb.New().SetOut(&buf)

	reader := strings.NewReader(content)

	total := int64(len(content))
	bar := p.AddBar(total).TrimLeftSpace().TrimRightSpace().
		PrependCounters("%3s / %3s", mpb.UnitBytes, 0, 0)
	preader := bar.ProxyReader(reader)

	_, err := io.Copy(ioutil.Discard, preader)
	if err != nil {
		t.Errorf("Error copying from reader: %+v\n", err)
	}

	p.Stop()

	out := buf.String()
	out = out[:strings.IndexRune(out, '[')]
	want := fmt.Sprintf("%[1]db / %[1]db", total)
	if out != want {
		t.Errorf("Expected: %s, got %s\n", want, out)
	}
}
