Commit c54358f1 authored by santiaago's avatar santiaago
Browse files

add test for background and foreground #55

parent 09895178
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -77,8 +77,11 @@ func Background(req *http.Request) (color.RGBA, error) {
	if len(bg) == 0 {
		return color.RGBA{}, fmt.Errorf("background: empty input")
	}
	r, g, b, err := hexToRGB(bg)
	return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(255)}, err
	if r, g, b, err := hexToRGB(bg); err != nil {
		return color.RGBA{}, fmt.Errorf("background: wrong input")
	} else {
		return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(255)}, nil
	}
}

// Foreground extract hexadecimal code foreground from HTTP request and return color.RGBA
@@ -87,8 +90,11 @@ func Foreground(req *http.Request) (color.RGBA, error) {
	if len(fg) == 0 {
		return color.RGBA{}, fmt.Errorf("background: empty input")
	}
	r, g, b, err := hexToRGB(fg)
	return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(255)}, err
	if r, g, b, err := hexToRGB(fg); err != nil {
		return color.RGBA{}, fmt.Errorf("background: wrong input")
	} else {
		return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(255)}, nil
	}
}

// ExtraColors returns a background and foreground color.RGBA is specified.
+43 −33
Original line number Diff line number Diff line
@@ -5,8 +5,6 @@ import (
	"net/http"
	"net/url"
	"testing"

	tgColors "github.com/taironas/tinygraphs/colors"
)

func TestUserColors(t *testing.T) {
@@ -46,45 +44,62 @@ func TestUserColors(t *testing.T) {
	}
}

func TestGColors(t *testing.T) {
	colorMap := tgColors.MapOfColorThemes()
func TestBackground(t *testing.T) {

	tests := []struct {
		title string
		url   string
		gColors []color.RGBA
		bg    color.RGBA
	}{
		{
			"test wrong input",
			"http://www.tg.c?colors=foo&colors=bar",
			colorMap["base"],
			"http://www.tg.c?bg=foo",
			color.RGBA{},
		},
		{
			"test no input",
			"http://www.tg.c",
			colorMap["base"],
			color.RGBA{},
		},
		{
			"test good input",
			"http://www.tg.c?colors=aaaaaa&colors=bbbbbb",
			[]color.RGBA{
			"http://www.tg.c?bg=aaaaaa",
			color.RGBA{170, 170, 170, 255},
				color.RGBA{187, 187, 187, 255},
		},
	}

	for _, test := range tests {
		t.Log(test.title)
		r := &http.Request{Method: "GET"}
		r.URL, _ = url.Parse(test.url)
		bg, _ := Background(r)
		if bg != test.bg {
			t.Errorf("expected %+v got %+v", test.bg, bg)
		}
	}
}

func TestForeground(t *testing.T) {

	tests := []struct {
		title string
		url   string
		fg    color.RGBA
	}{
		{
			"test wrong input",
			"http://www.tg.c?fg=foo",
			color.RGBA{},
		},
		{
			"test good input",
			"http://www.tg.c?colors=ffffff&colors=000000&colors=000000",
			[]color.RGBA{
				color.RGBA{255, 255, 255, 255},
				color.RGBA{0, 0, 0, 255},
				color.RGBA{0, 0, 0, 255},
			}[1:3],
			"test no input",
			"http://www.tg.c",
			color.RGBA{},
		},
		{
			"test with theme",
			"http://www.tg.c?theme=frogideas",
			colorMap["frogideas"][1:3],
			"test good input",
			"http://www.tg.c?fg=aaaaaa",
			color.RGBA{170, 170, 170, 255},
		},
	}

@@ -92,14 +107,9 @@ func TestGColors(t *testing.T) {
		t.Log(test.title)
		r := &http.Request{Method: "GET"}
		r.URL, _ = url.Parse(test.url)
		gColors := GColors(r)
		if len(gColors) != len(test.gColors) {
			t.Errorf("expected %d array got %d", len(test.gColors), len(gColors))
		}
		for i := 0; i < len(test.gColors); i++ {
			if test.gColors[i] != gColors[i] {
				t.Errorf("expected %+v array got %+v", test.gColors[i], gColors[i])
			}
		fg, _ := Foreground(r)
		if fg != test.fg {
			t.Errorf("expected %+v got %+v", test.fg, fg)
		}
	}
}