| 1 |
package standard |
| 2 |
|
| 3 |
import ( |
| 4 |
"image/color" |
| 5 |
"reflect" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/yeqown/go-qrcode/v2" |
| 9 |
) |
| 10 |
|
| 11 |
func Test_stateRGBA(t *testing.T) { |
| 12 |
oo := defaultOutputImageOption() |
| 13 |
|
| 14 |
type args struct { |
| 15 |
v qrcode.QRValue |
| 16 |
} |
| 17 |
tests := []struct { |
| 18 |
name string |
| 19 |
args args |
| 20 |
want color.RGBA |
| 21 |
}{ |
| 22 |
{ |
| 23 |
name: "case 1", |
| 24 |
args: args{v: qrcode.QRValue_DATA_V0}, |
| 25 |
want: oo.bgColor, |
| 26 |
}, |
| 27 |
{ |
| 28 |
name: "case 2", |
| 29 |
args: args{v: qrcode.QRValue_INIT_V0}, |
| 30 |
want: oo.bgColor, |
| 31 |
}, |
| 32 |
{ |
| 33 |
name: "case 3", |
| 34 |
args: args{v: qrcode.QRValue_DATA_V1}, |
| 35 |
want: oo.qrColor, |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "case 4", |
| 39 |
args: args{v: qrcode.QRValue_FORMAT_V1}, |
| 40 |
want: oo.qrColor, |
| 41 |
}, |
| 42 |
{ |
| 43 |
name: "case 5", |
| 44 |
args: args{v: qrcode.QRValue_VERSION_V1}, |
| 45 |
want: oo.qrColor, |
| 46 |
}, |
| 47 |
{ |
| 48 |
name: "case 6", |
| 49 |
args: args{v: qrcode.QRValue(0x0f)}, |
| 50 |
want: oo.qrColor, |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "case 7", |
| 54 |
args: args{v: qrcode.QRValue_FINDER_V1}, |
| 55 |
want: oo.qrColor, |
| 56 |
}, |
| 57 |
} |
| 58 |
for _, tt := range tests { |
| 59 |
t.Run(tt.name, func(t *testing.T) { |
| 60 |
if got := oo.translateToRGBA(tt.args.v); !reflect.DeepEqual(got, tt.want) { |
| 61 |
t.Errorf("translateToRGBA() = %v, want %v", got, tt.want) |
| 62 |
} |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
func Test_hexToRGBA(t *testing.T) { |
| 69 |
type args struct { |
| 70 |
s string |
| 71 |
} |
| 72 |
tests := []struct { |
| 73 |
name string |
| 74 |
args args |
| 75 |
want color.RGBA |
| 76 |
}{ |
| 77 |
{ |
| 78 |
name: "case 1", |
| 79 |
args: args{s: "#112233"}, |
| 80 |
want: color.RGBA{R: 17, G: 34, B: 51, A: 255}, |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "case 2", |
| 84 |
args: args{s: "#112"}, |
| 85 |
want: color.RGBA{R: 17, G: 17, B: 34, A: 255}, |
| 86 |
}, |
| 87 |
//{ |
| 88 |
// name: "case 3", |
| 89 |
// args: args{s: "#1122331"}, |
| 90 |
// want: color.RGBA{}, |
| 91 |
//}, // panic |
| 92 |
//{ |
| 93 |
// name: "case 4", |
| 94 |
// args: args{s: "#11"}, |
| 95 |
// want: color.RGBA{}, |
| 96 |
//}, // panic |
| 97 |
} |
| 98 |
for _, tt := range tests { |
| 99 |
t.Run(tt.name, func(t *testing.T) { |
| 100 |
if got := parseFromHex(tt.args.s); !reflect.DeepEqual(got, tt.want) { |
| 101 |
t.Errorf("parseFromHex() = %v, want %v", got, tt.want) |
| 102 |
} |
| 103 |
}) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
func Test_parseFromColor(t *testing.T) { |
| 108 |
type args struct { |
| 109 |
c color.Color |
| 110 |
} |
| 111 |
tests := []struct { |
| 112 |
name string |
| 113 |
args args |
| 114 |
want color.RGBA |
| 115 |
}{ |
| 116 |
{ |
| 117 |
name: "case 0", |
| 118 |
args: args{ |
| 119 |
c: color.RGBA{R: 17, G: 34, B: 51, A: 255}, |
| 120 |
}, |
| 121 |
want: color.RGBA{R: 17, G: 34, B: 51, A: 255}, |
| 122 |
}, |
| 123 |
{ |
| 124 |
name: "case 1", |
| 125 |
args: args{ |
| 126 |
c: color.Gray16{Y: 17}, |
| 127 |
}, |
| 128 |
want: color.RGBA{R: 17, G: 17, B: 17, A: 255}, |
| 129 |
}, |
| 130 |
} |
| 131 |
for _, tt := range tests { |
| 132 |
t.Run(tt.name, func(t *testing.T) { |
| 133 |
if got := parseFromColor(tt.args.c); !reflect.DeepEqual(got, tt.want) { |
| 134 |
t.Errorf("parseFromColor() = %v, want %v", got, tt.want) |
| 135 |
} |
| 136 |
}) |
| 137 |
} |
| 138 |
} |