2018-04-13 13:27:01 +03:00
|
|
|
// +build !rpctest
|
|
|
|
|
2017-12-16 02:06:20 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestParseHexColor(t *testing.T) {
|
2018-11-04 05:00:19 +03:00
|
|
|
var colorTestCases = []struct {
|
|
|
|
test string
|
|
|
|
valid bool // If valid format
|
|
|
|
R byte
|
|
|
|
G byte
|
|
|
|
B byte
|
|
|
|
}{
|
|
|
|
{"#123", false, 0, 0, 0},
|
|
|
|
{"#1234567", false, 0, 0, 0},
|
|
|
|
{"$123456", false, 0, 0, 0},
|
|
|
|
{"#12345+", false, 0, 0, 0},
|
|
|
|
{"#fFGG00", false, 0, 0, 0},
|
|
|
|
{"", false, 0, 0, 0},
|
|
|
|
{"#123456", true, 0x12, 0x34, 0x56},
|
|
|
|
{"#C0FfeE", true, 0xc0, 0xff, 0xee},
|
2017-12-16 02:06:20 +03:00
|
|
|
}
|
|
|
|
|
2018-11-04 05:00:19 +03:00
|
|
|
// Perform the table driven tests.
|
|
|
|
for _, ct := range colorTestCases {
|
2017-12-16 02:06:20 +03:00
|
|
|
|
2018-11-04 05:00:19 +03:00
|
|
|
color, err := parseHexColor(ct.test)
|
|
|
|
if !ct.valid && err == nil {
|
|
|
|
t.Fatalf("Invalid color string: %s, should return "+
|
|
|
|
"error, but did not", ct.test)
|
|
|
|
}
|
2017-12-16 02:06:20 +03:00
|
|
|
|
2018-11-04 05:00:19 +03:00
|
|
|
if ct.valid && err != nil {
|
|
|
|
t.Fatalf("Color %s valid to parse: %s", ct.test, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the string to hex decoding is working properly.
|
|
|
|
if color.R != ct.R || color.G != ct.G || color.B != ct.B {
|
|
|
|
t.Fatalf("Color %s incorrectly parsed as %v", ct.test, color)
|
|
|
|
}
|
2017-12-16 02:06:20 +03:00
|
|
|
}
|
|
|
|
}
|