1package xtermcolor 2 3import ( 4 "image/color" 5 "testing" 6) 7 8// RGBA Tests 9type rgbaTest struct { 10 in color.RGBA 11 exp uint8 12} 13 14var rgbaTests = []rgbaTest{ 15 {color.RGBA{255, 0, 255, 255}, 13}, 16 {color.RGBA{127, 127, 127, 255}, 8}, 17 {color.RGBA{63, 254, 128, 255}, 84}, 18 {color.RGBA{215, 0, 95, 255}, 161}, 19 {color.RGBA{204, 102, 255, 255}, 171}, 20 {color.RGBA{17, 156, 17, 255}, 34}, 21} 22 23func TestRGBA(t *testing.T) { 24 for _, test := range rgbaTests { 25 act := FromColor(test.in) 26 if act != test.exp { 27 t.Errorf("FromRGB(%v) should be %d (%v); got %d (%v)", test.in, test.exp, Colors[test.exp], act, Colors[act]) 28 } 29 } 30} 31 32// Int Tests 33type intTest struct { 34 in uint32 35 exp uint8 36} 37 38var intTests = []intTest{ 39 {0x000000FF, 0}, 40 {0xFF0000FF, 9}, 41 {0xCC66FFFF, 171}, 42} 43 44func TestInts(t *testing.T) { 45 for _, test := range intTests { 46 act := FromInt(test.in) 47 if act != test.exp { 48 t.Errorf("FromInt(0x%08.X) (%v) should be %d (%v); got %d (%v)", test.in, intToRGBA(test.in), test.exp, Colors[test.exp], act, Colors[act]) 49 } 50 } 51} 52 53// HexStr Tests 54type hexStrTest struct { 55 in string 56 exp uint8 57 err error 58} 59 60var hexStrTests = []hexStrTest{ 61 {"#000000", 0, nil}, 62 {"FF0000", 9, nil}, 63 {"#CC66FF", 171, nil}, 64 {"", 0, ErrorEmptyHexStr}, 65 {"#nothex", 0, ErrorHexParse}, 66} 67 68func TestHexStr(t *testing.T) { 69 for _, test := range hexStrTests { 70 act, err := FromHexStr(test.in) 71 72 if err != test.err { 73 t.Errorf("FromHexStr error value should have been %s but was %s", test.err, err) 74 } 75 if act != test.exp { 76 t.Errorf("FromHexStr(%s) should be %d (%v); got %d (%v)", test.in, test.exp, Colors[test.exp], act, Colors[act]) 77 } 78 } 79} 80