File: draw_test.go

package info (click to toggle)
golang-github-coreos-ioprogress 0.0~git20151023.0.4637e49-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 68 kB
  • ctags: 31
  • sloc: makefile: 2
file content (90 lines) | stat: -rw-r--r-- 1,541 bytes parent folder | download | duplicates (3)
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
87
88
89
90
package ioprogress

import (
	"bytes"
	"testing"
)

func TestDrawTerminal(t *testing.T) {
	var buf bytes.Buffer
	fn := DrawTerminal(&buf)
	if err := fn(0, 100); err != nil {
		t.Fatalf("err: %s", err)
	}
	if err := fn(20, 100); err != nil {
		t.Fatalf("err: %s", err)
	}
	if err := fn(-1, -1); err != nil {
		t.Fatalf("err: %s", err)
	}

	if buf.String() != drawTerminalStr {
		t.Fatalf("bad:\n\n%#v", buf.String())
	}
}

func TestDrawTextFormatBar(t *testing.T) {
	var actual, expected string
	f := DrawTextFormatBar(10)

	actual = f(5, 10)
	expected = "[====    ]"
	if actual != expected {
		t.Fatalf("bad: %s", actual)
	}

	actual = f(2, 10)
	expected = "[=       ]"
	if actual != expected {
		t.Fatalf("bad: %s", actual)
	}

	actual = f(10, 10)
	expected = "[========]"
	if actual != expected {
		t.Fatalf("bad: %s", actual)
	}

	actual = f(0, 0)
	expected = "[        ]"
	if actual != expected {
		t.Fatalf("bad: %s", actual)
	}

	actual = f(-10, 10)
	expected = "[        ]"
	if actual != expected {
		t.Fatalf("bad: %s", actual)
	}

	actual = f(20, 10)
	expected = "[        ]"
	if actual != expected {
		t.Fatalf("bad: %s", actual)
	}
}

func TestDrawTextFormatBytes(t *testing.T) {
	cases := []struct {
		P, T   int64
		Output string
	}{
		{
			100, 500,
			"100 B/500 B",
		},
		{
			1500, 5000,
			"1.5 KB/5 KB",
		},
	}

	for _, tc := range cases {
		output := DrawTextFormatBytes(tc.P, tc.T)
		if output != tc.Output {
			t.Fatalf("Input: %d, %d\n\n%s", tc.P, tc.T, output)
		}
	}
}

const drawTerminalStr = "0/100\n20/100\n\n"