| 1 |
package standard |
| 2 |
|
| 3 |
import ( |
| 4 |
"image/color" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/assert" |
| 8 |
) |
| 9 |
|
| 10 |
func Test_WithBuiltinImageEncoder(t *testing.T) { |
| 11 |
oo := defaultOutputImageOption() |
| 12 |
|
| 13 |
assert.IsType(t, jpegEncoder{}, oo.imageEncoder) |
| 14 |
WithBuiltinImageEncoder(JPEG_FORMAT).apply(oo) |
| 15 |
assert.IsType(t, jpegEncoder{}, oo.imageEncoder) |
| 16 |
WithBuiltinImageEncoder(PNG_FORMAT).apply(oo) |
| 17 |
assert.IsType(t, pngEncoder{}, oo.imageEncoder) |
| 18 |
} |
| 19 |
|
| 20 |
func Test_WithCustomImageEncoder(t *testing.T) { |
| 21 |
oo := defaultOutputImageOption() |
| 22 |
|
| 23 |
assert.IsType(t, jpegEncoder{}, oo.imageEncoder) |
| 24 |
WithCustomImageEncoder(nil).apply(oo) |
| 25 |
assert.IsType(t, jpegEncoder{}, oo.imageEncoder) |
| 26 |
} |
| 27 |
|
| 28 |
func Test_BgColor_FgColor(t *testing.T) { |
| 29 |
oo := defaultOutputImageOption() |
| 30 |
|
| 31 |
// check |
| 32 |
assert.Equal(t, color_WHITE, oo.bgColor) |
| 33 |
assert.Equal(t, color_BLACK, oo.qrColor) |
| 34 |
|
| 35 |
// apply color |
| 36 |
WithBgColor(color_BLACK).apply(oo) |
| 37 |
assert.Equal(t, color_BLACK, oo.bgColor) |
| 38 |
assert.Equal(t, color_BLACK, oo.qrColor) |
| 39 |
|
| 40 |
// apply color |
| 41 |
WithBgColor(color_WHITE).apply(oo) |
| 42 |
WithFgColor(color_WHITE).apply(oo) |
| 43 |
assert.Equal(t, color_WHITE, oo.bgColor) |
| 44 |
assert.Equal(t, color_WHITE, oo.qrColor) |
| 45 |
|
| 46 |
WithFgColor(color_BLACK).apply(oo) |
| 47 |
assert.Equal(t, color_WHITE, oo.bgColor) |
| 48 |
assert.Equal(t, color_BLACK, oo.qrColor) |
| 49 |
} |
| 50 |
|
| 51 |
func Test_defaultOutputOption(t *testing.T) { |
| 52 |
oo := defaultOutputImageOption() |
| 53 |
|
| 54 |
// Apply |
| 55 |
rgba := color.RGBA{ |
| 56 |
R: 123, |
| 57 |
G: 123, |
| 58 |
B: 123, |
| 59 |
A: 123, |
| 60 |
} |
| 61 |
WithBgColor(rgba).apply(oo) |
| 62 |
// assert |
| 63 |
assert.Equal(t, rgba, oo.bgColor) |
| 64 |
|
| 65 |
// check default |
| 66 |
oo2 := defaultOutputImageOption() |
| 67 |
assert.NotEqual(t, oo2.bgColor, oo.bgColor) |
| 68 |
} |
| 69 |
|
| 70 |
func Test_WithBorderWidth(t *testing.T) { |
| 71 |
oo := defaultOutputImageOption() |
| 72 |
|
| 73 |
// zero parameter |
| 74 |
WithBorderWidth().apply(oo) |
| 75 |
assert.Equal(t, [4]int{_defaultPadding, _defaultPadding, _defaultPadding, _defaultPadding}, oo.borderWidths) |
| 76 |
|
| 77 |
// one parameter |
| 78 |
WithBorderWidth(1).apply(oo) |
| 79 |
assert.Equal(t, [4]int{1, 1, 1, 1}, oo.borderWidths) |
| 80 |
|
| 81 |
// two parameters |
| 82 |
WithBorderWidth(1, 2).apply(oo) |
| 83 |
assert.Equal(t, [4]int{1, 2, 1, 2}, oo.borderWidths) |
| 84 |
|
| 85 |
// three parameters |
| 86 |
WithBorderWidth(1, 2, 3).apply(oo) |
| 87 |
assert.Equal(t, [4]int{1, 2, 1, 2}, oo.borderWidths) |
| 88 |
|
| 89 |
// four parameters |
| 90 |
WithBorderWidth(1, 2, 3, 4).apply(oo) |
| 91 |
assert.Equal(t, [4]int{1, 2, 3, 4}, oo.borderWidths) |
| 92 |
} |
| 93 |
|
| 94 |
func Test_WithBgTransparent(t *testing.T) { |
| 95 |
oo := defaultOutputImageOption() |
| 96 |
|
| 97 |
// check default |
| 98 |
assert.False(t, oo.bgTransparent) |
| 99 |
|
| 100 |
// apply |
| 101 |
WithBgTransparent().apply(oo) |
| 102 |
assert.True(t, oo.bgTransparent) |
| 103 |
} |
| 104 |
|
| 105 |
func Test_WithLogoSizeMultiplier(t *testing.T) { |
| 106 |
oo := defaultOutputImageOption() |
| 107 |
|
| 108 |
// check default |
| 109 |
assert.Equal(t, 5, oo.logoSizeMultiplier) |
| 110 |
|
| 111 |
// apply |
| 112 |
WithLogoSizeMultiplier(2).apply(oo) |
| 113 |
assert.Equal(t, 2, oo.logoSizeMultiplier) |
| 114 |
} |