File: format_test.go

package info (click to toggle)
golang-gopkg-cheggaaa-pb.v1 1.0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, buster-backports, experimental, sid
  • size: 160 kB
  • sloc: makefile: 2
file content (95 lines) | stat: -rw-r--r-- 2,159 bytes parent folder | download
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
91
92
93
94
95
package pb

import (
	"fmt"
	"strconv"
	"testing"
	"time"
)

func Test_DefaultsToInteger(t *testing.T) {
	value := int64(1000)
	expected := strconv.Itoa(int(value))
	actual := Format(value).String()

	if actual != expected {
		t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
	}
}

func Test_CanFormatAsInteger(t *testing.T) {
	value := int64(1000)
	expected := strconv.Itoa(int(value))
	actual := Format(value).To(U_NO).String()

	if actual != expected {
		t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
	}
}

func Test_CanFormatAsBytes(t *testing.T) {
	inputs := []struct {
		v int64
		e string
	}{
		{v: 1000, e: "1000 B"},
		{v: 1024, e: "1.00 KiB"},
		{v: 3*MiB + 140*KiB, e: "3.14 MiB"},
		{v: 2 * GiB, e: "2.00 GiB"},
		{v: 2048 * GiB, e: "2.00 TiB"},
	}

	for _, input := range inputs {
		actual := Format(input.v).To(U_BYTES).String()
		if actual != input.e {
			t.Error(fmt.Sprintf("Expected {%s} was {%s}", input.e, actual))
		}
	}
}

func Test_CanFormatAsBytesDec(t *testing.T) {
	inputs := []struct {
		v int64
		e string
	}{
		{v: 999, e: "999 B"},
		{v: 1024, e: "1.02 KB"},
		{v: 3*MB + 140*KB, e: "3.14 MB"},
		{v: 2 * GB, e: "2.00 GB"},
		{v: 2048 * GB, e: "2.05 TB"},
	}

	for _, input := range inputs {
		actual := Format(input.v).To(U_BYTES_DEC).String()
		if actual != input.e {
			t.Error(fmt.Sprintf("Expected {%s} was {%s}", input.e, actual))
		}
	}
}

func Test_CanFormatDuration(t *testing.T) {
	value := 10 * time.Minute
	expected := "10m00s"
	actual := Format(int64(value)).To(U_DURATION).String()
	if actual != expected {
		t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
	}
}

func Test_CanFormatLongDuration(t *testing.T) {
	value := 62 * time.Hour + 13 * time.Second
	expected := "2d14h00m13s"
	actual := Format(int64(value)).To(U_DURATION).String()
	if actual != expected {
		t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
	}
}

func Test_DefaultUnitsWidth(t *testing.T) {
	value := 10
	expected := "     10"
	actual := Format(int64(value)).Width(7).String()
	if actual != expected {
		t.Error(fmt.Sprintf("Expected {%s} was {%s}", expected, actual))
	}
}