|
| 1 | +package goavatar |
| 2 | + |
| 3 | +import ( |
| 4 | + "image/color" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestMake(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + input string |
| 12 | + opts Options |
| 13 | + width int |
| 14 | + height int |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "Default settings", |
| 18 | + |
| 19 | + opts: Options{}, // empty options for use the defaults |
| 20 | + width: 256, |
| 21 | + height: 256, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "Custom width and height", |
| 25 | + input: "custom-size", |
| 26 | + opts: Options{Width: 512, Height: 512}, |
| 27 | + width: 512, height: 512, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "Custom background color", |
| 31 | + input: "custom-bg", |
| 32 | + opts: Options{BgColor: color.RGBA{255, 0, 0, 255}}, // Red background |
| 33 | + width: 256, height: 256, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "QuantumNomad42", |
| 37 | + input: "QuantumNomad42", |
| 38 | + opts: Options{Width: 512, Height: 512}, |
| 39 | + width: 512, |
| 40 | + height: 512, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "EchoFrost7", |
| 44 | + input: "EchoFrost7", |
| 45 | + opts: Options{Width: 512, Height: 512}, |
| 46 | + width: 512, |
| 47 | + height: 512, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + for _, tt := range tests { |
| 52 | + |
| 53 | + img := Make(tt.input, tt.opts) |
| 54 | + |
| 55 | + // check if the return image is not nill |
| 56 | + if img == nil { |
| 57 | + t.Fatalf("Make() returned nil for input %q", tt.input) |
| 58 | + } |
| 59 | + |
| 60 | + // check if dimensions is matched the expected size |
| 61 | + bounds := img.Bounds() |
| 62 | + if bounds.Dx() != tt.width || bounds.Dy() != tt.height { |
| 63 | + t.Errorf("Unexepected image size for %q: go %dx%d, want %dx%d", |
| 64 | + tt.input, |
| 65 | + bounds.Dx(), |
| 66 | + bounds.Dy(), |
| 67 | + tt.width, |
| 68 | + tt.height, |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + // check if the top left pixel matches the BG color |
| 73 | + // check BgColor is set not an empty |
| 74 | + if tt.opts.BgColor != (color.RGBA{}) { |
| 75 | + expectedBgColor := tt.opts.BgColor |
| 76 | + actualColor := img.At(0, 0).(color.RGBA) // get the top left pixel color |
| 77 | + |
| 78 | + if actualColor != expectedBgColor { |
| 79 | + t.Errorf("Unexepected background color for %q: got %v, want %v", tt.input, actualColor, expectedBgColor) |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + } |
| 85 | +} |
0 commit comments