package door

import (
	"fmt"
	"testing"
)

func TestLine(t *testing.T) {
	line := Line{Text: "Test Me"}
	output := line.Output()
	expect := "Test Me"

	if output != expect {
		t.Errorf("Line: Expected %#v, got %#v", expect, output)
	}

	if line.Update() {
		t.Error("Line: No updater, should return false")
	}

	line.DefaultColor = Color(0)
	output = line.Output()
	expect = "\x1b[0mTest Me"

	if output != expect {
		t.Errorf("Line: Expected %#v, got %#v", expect, output)
	}

	// leave the default color, it is ignored when there's a render function
	// line.DefaultColor = ""
	line.RenderF = RenderBlueYellow
	output = line.Output()
	blue := ColorText("BOLD BLUE")
	yellow := ColorText("BOLD YELLOW")

	expect = blue + "T" + yellow + "est " + blue + "M" + yellow + "e"
	if output != expect {
		t.Errorf("Line: Expected %#v, got %#v", expect, output)
	}

}

func TestLineUpdate(t *testing.T) {
	counter := 0
	uf := func() string {
		return fmt.Sprintf("Count: %d", counter)
	}
	line := Line{Text: "", UpdateF: uf}
	line.Update()
	output := line.Output()
	expect := "Count: 0"

	if output != expect {
		t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
	}

	if line.Update() {
		t.Error("Unexpected Update: should have returned false. (no change)")
	}

	counter++

	if !line.Update() {
		t.Error("Missing Update: value was changed, Text should have changed")
	}

	output = line.Output()
	expect = "Count: 1"
	if output != expect {
		t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
	}

}