|
| 1 | +@testable import RepoBar |
| 2 | +import Testing |
| 3 | + |
| 4 | +struct StatValueFormatterTests { |
| 5 | + @Test |
| 6 | + func `passes through values below one thousand`() { |
| 7 | + #expect(StatValueFormatter.compact(0) == "0") |
| 8 | + #expect(StatValueFormatter.compact(42) == "42") |
| 9 | + #expect(StatValueFormatter.compact(999) == "999") |
| 10 | + } |
| 11 | + |
| 12 | + @Test |
| 13 | + func `keeps one decimal below ten thousand`() { |
| 14 | + #expect(StatValueFormatter.compact(1000) == "1K") |
| 15 | + #expect(StatValueFormatter.compact(1500) == "1.5K") |
| 16 | + #expect(StatValueFormatter.compact(1999) == "2K") |
| 17 | + #expect(StatValueFormatter.compact(9999) == "10K") |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + func `rounds thousands to nearest instead of truncating`() { |
| 22 | + #expect(StatValueFormatter.compact(10000) == "10K") |
| 23 | + #expect(StatValueFormatter.compact(10500) == "11K") |
| 24 | + #expect(StatValueFormatter.compact(19999) == "20K") |
| 25 | + #expect(StatValueFormatter.compact(99500) == "100K") |
| 26 | + #expect(StatValueFormatter.compact(123_456) == "123K") |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + func `rolls rounded thousands up into millions`() { |
| 31 | + #expect(StatValueFormatter.compact(999_499) == "999K") |
| 32 | + #expect(StatValueFormatter.compact(999_500) == "1M") |
| 33 | + #expect(StatValueFormatter.compact(999_999) == "1M") |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + func `keeps one decimal below ten million`() { |
| 38 | + #expect(StatValueFormatter.compact(1_000_000) == "1M") |
| 39 | + #expect(StatValueFormatter.compact(1_500_000) == "1.5M") |
| 40 | + #expect(StatValueFormatter.compact(9_999_999) == "10M") |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + func `rounds millions and caps oversized counts`() { |
| 45 | + #expect(StatValueFormatter.compact(10_500_000) == "11M") |
| 46 | + #expect(StatValueFormatter.compact(99_999_999) == "100M") |
| 47 | + #expect(StatValueFormatter.compact(999_000_000) == "999M") |
| 48 | + #expect(StatValueFormatter.compact(1_000_000_000) == "999M") |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + func `caps Int.max without integer overflow`() { |
| 53 | + #expect(StatValueFormatter.compact(Int.max) == "999M") |
| 54 | + } |
| 55 | +} |
0 commit comments