|
| 1 | +package guard |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/donislawdev/TestingFilesGenerator/internal/core" |
| 8 | +) |
| 9 | + |
| 10 | +// Sizes count in 1024s, on purpose and against the standards. |
| 11 | +// |
| 12 | +// Measured: Windows Explorer shows 10 485 760 bytes as "10 MB" and 10 000 000 |
| 13 | +// bytes as "9,53 MB". ls -lh shows "10M" and "9.6M". A tester who asks for |
| 14 | +// 10mb and then checks the file has to see the number they asked for, or the |
| 15 | +// tool looks broken - and looking broken costs more than following the |
| 16 | +// standard buys. |
| 17 | +// |
| 18 | +// Someone who wants exactly ten million bytes writes the number. |
| 19 | + |
| 20 | +func TestSizesCountIn1024s(t *testing.T) { |
| 21 | + cases := []struct { |
| 22 | + in string |
| 23 | + want int64 |
| 24 | + }{ |
| 25 | + {"1024", 1024}, |
| 26 | + {"0", 0}, |
| 27 | + {"1b", 1}, |
| 28 | + {"1kb", 1024}, |
| 29 | + {"1kib", 1024}, |
| 30 | + {"1k", 1024}, |
| 31 | + {"1mb", 1024 * 1024}, |
| 32 | + {"1mib", 1024 * 1024}, |
| 33 | + {"10mb", 10 * 1024 * 1024}, |
| 34 | + {"1gb", 1024 * 1024 * 1024}, |
| 35 | + {"1gib", 1024 * 1024 * 1024}, |
| 36 | + {"1.5gib", 1610612736}, |
| 37 | + {"1tb", 1024 * 1024 * 1024 * 1024}, |
| 38 | + |
| 39 | + // Case does not matter, and neither does a space. |
| 40 | + {"700kB", 700 * 1024}, |
| 41 | + {"10 MB", 10 * 1024 * 1024}, |
| 42 | + {"10MiB", 10 * 1024 * 1024}, |
| 43 | + |
| 44 | + // The size the whole project keeps as its example. |
| 45 | + {"10485761", 10485761}, |
| 46 | + } |
| 47 | + |
| 48 | + for _, c := range cases { |
| 49 | + got, err := core.ParseSize(c.in) |
| 50 | + if err != nil { |
| 51 | + t.Errorf("%q failed: %v", c.in, err) |
| 52 | + continue |
| 53 | + } |
| 54 | + if got != c.want { |
| 55 | + t.Errorf("%q gave %d B, expected %d B", c.in, got, c.want) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestTheTwoSpellingsAgree(t *testing.T) { |
| 61 | + pairs := [][2]string{ |
| 62 | + {"5kb", "5kib"}, |
| 63 | + {"5mb", "5mib"}, |
| 64 | + {"5gb", "5gib"}, |
| 65 | + {"5tb", "5tib"}, |
| 66 | + } |
| 67 | + for _, p := range pairs { |
| 68 | + a, errA := core.ParseSize(p[0]) |
| 69 | + b, errB := core.ParseSize(p[1]) |
| 70 | + if errA != nil || errB != nil { |
| 71 | + t.Fatalf("%q or %q failed: %v %v", p[0], p[1], errA, errB) |
| 72 | + } |
| 73 | + if a != b { |
| 74 | + t.Errorf("%q gave %d B and %q gave %d B - the two spellings mean the same thing", p[0], a, p[1], b) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestASizeThatIsNotAWholeByteIsRefused(t *testing.T) { |
| 80 | + // Rounding is the one thing this tool must never do quietly. In a batch |
| 81 | + // of ten thousand files a warning is lost, and the user silently receives |
| 82 | + // data they did not order. |
| 83 | + for _, in := range []string{"1.5", "0.5b", "1.0001kb"} { |
| 84 | + got, err := core.ParseSize(in) |
| 85 | + if err == nil { |
| 86 | + t.Errorf("%q was accepted as %d B instead of being refused", in, got) |
| 87 | + continue |
| 88 | + } |
| 89 | + // The message has to say what a workable value looks like, not only |
| 90 | + // that the input was wrong. |
| 91 | + if !strings.Contains(err.Error(), "whole number of bytes") { |
| 92 | + t.Errorf("%q was refused without explaining why: %v", in, err) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestNonsenseSizesAreRefusedWithAUsefulMessage(t *testing.T) { |
| 98 | + for _, in := range []string{"", "banana", "mb", "10qb", "-5mb"} { |
| 99 | + if got, err := core.ParseSize(in); err == nil { |
| 100 | + t.Errorf("%q was accepted as %d B", in, got) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments