TestPercentageDecor
Vladimir Bauer
1 year, 10 months ago
| 55 | 55 | }) |
| 56 | 56 | } |
| 57 | 57 | } |
| 58 | ||
| 59 | func TestPercentageDecor(t *testing.T) { | |
| 60 | cases := []struct { | |
| 61 | name string | |
| 62 | fmt string | |
| 63 | current int64 | |
| 64 | total int64 | |
| 65 | expected string | |
| 66 | }{ | |
| 67 | { | |
| 68 | name: "tot:100 cur:0 fmt:none", | |
| 69 | fmt: "", | |
| 70 | current: 0, | |
| 71 | total: 100, | |
| 72 | expected: "0 %", | |
| 73 | }, | |
| 74 | { | |
| 75 | name: "tot:100 cur:10 fmt:none", | |
| 76 | fmt: "", | |
| 77 | current: 10, | |
| 78 | total: 100, | |
| 79 | expected: "10 %", | |
| 80 | }, | |
| 81 | { | |
| 82 | name: "tot:100 cur:10 fmt:%.2f", | |
| 83 | fmt: "%.2f", | |
| 84 | current: 10, | |
| 85 | total: 100, | |
| 86 | expected: "10.00%", | |
| 87 | }, | |
| 88 | { | |
| 89 | name: "tot:99 cur:10 fmt:%.2f", | |
| 90 | fmt: "%.2f", | |
| 91 | current: 11, | |
| 92 | total: 99, | |
| 93 | expected: "11.11%", | |
| 94 | }, | |
| 95 | } | |
| 96 | for _, tc := range cases { | |
| 97 | t.Run(tc.name, func(t *testing.T) { | |
| 98 | decor := NewPercentage(tc.fmt) | |
| 99 | stat := Statistics{ | |
| 100 | Total: tc.total, | |
| 101 | Current: tc.current, | |
| 102 | } | |
| 103 | res, _ := decor.Decor(stat) | |
| 104 | if res != tc.expected { | |
| 105 | t.Fatalf("expected: %q, got: %q\n", tc.expected, res) | |
| 106 | } | |
| 107 | }) | |
| 108 | } | |
| 109 | } |