main_test.go (3003B)
1 package main 2 3 import "testing" 4 5 // TestTarget covers how the output format is chosen, since a wrong choice 6 // writes one format under another's name. 7 func TestTarget(t *testing.T) { 8 t.Parallel() 9 tests := []struct { 10 desc string 11 want string // --format 12 out string // output path 13 piping bool 14 got string // what the input turned out to be 15 expect string 16 }{ 17 {"the output path names it", "", "icon.ico", false, "png", ".ico"}, 18 {"an icns from artwork", "", "icon.icns", false, "png", ".icns"}, 19 {"a plain image out of a container", "", "icon.png", false, "icns", ".png"}, 20 {"the flag wins over the path", "ico", "icon.icns", false, "png", ".ico"}, 21 {"the flag takes a dot", ".jpg", "", false, "icns", ".jpg"}, 22 {"the flag takes capitals", "ICO", "", false, "png", ".ico"}, 23 {"a pipe packs artwork", "", "", true, "png", ".icns"}, 24 {"a pipe unpacks an icns", "", "", true, "icns", ".png"}, 25 {"a pipe unpacks an ico", "", "", true, "ico", ".png"}, 26 {"a bundle from the flag", "icon", "", false, "png", ".icon"}, 27 {"a bundle from the output path", "", "App.icon", false, "png", ".icon"}, 28 {"a binary unpacks to an image", "", "", true, "exe", ".png"}, 29 {"a pipe takes the flag", "ico", "", true, "png", ".ico"}, 30 {"an extension we cannot write, from artwork", "", "icon.gif", false, "png", ".icns"}, 31 {"an extension we cannot write, from a container", "", "icon.gif", false, "icns", ".png"}, 32 } 33 for _, tt := range tests { 34 t.Run(tt.desc, func(st *testing.T) { 35 if got := target(tt.want, tt.out, tt.piping, tt.got); got != tt.expect { 36 st.Errorf("target = %s, want %s", got, tt.expect) 37 } 38 }) 39 } 40 } 41 42 func TestSanitiseInputs(t *testing.T) { 43 t.Parallel() 44 tests := []struct { 45 desc string 46 in string 47 out string 48 format string 49 expect string 50 }{ 51 {"artwork defaults to icns", "icon.png", "", "", "icon.icns"}, 52 {"the format flag sets the default", "icon.png", "", "ico", "icon.ico"}, 53 {"an icns defaults to png", "icon.icns", "", "", "icon.png"}, 54 {"an ico defaults to png", "icon.ico", "", "", "icon.png"}, 55 {"an icns converts to an ico", "icon.icns", "", "ico", "icon.ico"}, 56 {"a name without an extension gains one", "icon.png", "out", "", "out.icns"}, 57 {"a name without an extension, unpacking", "icon.icns", "out", "", "out.png"}, 58 {"the output path is kept", "icon.png", "out.ico", "", "out.ico"}, 59 {"the output lands beside us", "path/to/icon.png", "", "", "icon.icns"}, 60 } 61 for _, tt := range tests { 62 t.Run(tt.desc, func(st *testing.T) { 63 _, out, _ := sanitiseInputs(tt.in, tt.out, tt.format, 5) 64 if out != tt.expect { 65 st.Errorf("output = %s, want %s", out, tt.expect) 66 } 67 }) 68 } 69 } 70 71 func TestSanitiseInputsClampsQuality(t *testing.T) { 72 t.Parallel() 73 for _, tt := range []struct { 74 resize int 75 expect int 76 }{{-1, 0}, {0, 0}, {3, 3}, {5, 5}, {9, 5}} { 77 if _, _, got := sanitiseInputs("icon.png", "", "", tt.resize); int(got) != tt.expect { 78 t.Errorf("resize %d became %d, want %d", tt.resize, got, tt.expect) 79 } 80 } 81 }