Jelajahi Sumber

Working tests, 100% coverage in convert and line.

Steve Thielemann 3 tahun lalu
induk
melakukan
85d2821e19
5 mengubah file dengan 192 tambahan dan 6 penghapusan
  1. 51 0
      door/color_test.go
  2. 2 2
      door/convert.go
  3. 62 0
      door/convert_test.go
  4. 4 4
      door/input.go
  5. 73 0
      door/line_test.go

+ 51 - 0
door/color_test.go

@@ -0,0 +1,51 @@
+package door
+
+import (
+	"testing"
+)
+
+func TestColor(t *testing.T) {
+
+	ColorMap := map[string][]int{"\x1b[0m": {0},
+		"\x1b[1m":         {1},
+		"\x1b[0;1;32;45m": {0, 1, 32, 45},
+	}
+
+	for text, code := range ColorMap {
+		ct := Color(code...)
+		if text != ct {
+			t.Errorf("Color: Expected %#v (%#v), got %#v", text, code, ct)
+		}
+	}
+}
+
+func TestColorText(t *testing.T) {
+
+	ColorMap := map[string][]int{"BRI WHI": {0, 1, 37},
+		"RED ON GREEN":                {0, 31, 42},
+		"GREEN ON RED":                {0, 32, 41},
+		"GREE ON MAG":                 {0, 32, 45},
+		"WHI ON BLACK":                {0, 37, 40}, // BUG?  Should be {0}
+		"WHITE ON BLACK":              {0, 37, 40}, // ^ {0}
+		"BRIGHT WHITE":                {0, 1, 37},
+		"BOLD RED":                    {0, 1, 31},
+		"BLUE ON WHITE":               {0, 34, 47},
+		"CYAN ON MAG":                 {0, 36, 45},
+		"MAGENTA":                     {0, 35},
+		"BRI YELL":                    {0, 1, 33},
+		"BRI YELLOW ON BLUE":          {0, 1, 33, 44},
+		"BROWN ON CYAN":               {0, 33, 46},
+		"BLACK ON YELLOW":             {0, 30, 43},
+		"BLINK RED ON BLUE":           {0, 5, 31, 44},
+		"BRIGHT BLINKY BLUE ON GREEN": {0, 1, 5, 34, 42},
+	}
+
+	for text, code := range ColorMap {
+		ct := ColorText(text)
+		cc := Color(code...)
+		if ct != cc {
+			t.Errorf("ColorText: Expected %#v (%#v), got %#v", cc, code, ct)
+		}
+	}
+
+}

+ 2 - 2
door/convert.go

@@ -9,8 +9,8 @@ package door
 func CP437_to_Unicode(cp437 string) string {
 	var result string
 
-	// for _, char := range []byte(cp437) {
-	for _, char := range []rune(cp437) {
+	for _, char := range []byte(cp437) {
+		// for _, char := range []rune(cp437) {
 		switch int(char) {
 		case 0x01:
 			result += "\u263A"

+ 62 - 0
door/convert_test.go

@@ -0,0 +1,62 @@
+package door
+
+import "testing"
+
+func TestConvert(t *testing.T) {
+	/*
+		BinaryMap := map[string][]byte{"\x00\u263A\u263B\u2665\u2666\u2663\u2660\x07\x08\u25CB\x0a\u2642\u2640\x0d\u266B\u263C": {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+			"\u25BA\u25C4\u2195\u203C\xc2\xb6\xc2\xa7\u25AC\u21A8\u2191\u2193\u2192\x1b\u221F\u2194\u25B2\u25BC": {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f},
+			" !\"#$%&'()*+,-./": {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
+			"0123456789:;<=>?":  {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f},
+			"\u2302":            {0x7f},
+			"\xc3\x87\xc3\xbc\xc3\xa9\xc3\xa2\xc3\xa4\xc3\xa0\xc3\xa5\xc3\xa7\xc3\xaa\xc3\xab\xc3\xa8\xc3\xaf\xc3\xae\xc3\xac\xc3\x84\xc3\x85": {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f},
+		}
+	*/
+
+	// From: https://en.wikipedia.org/wiki/Code_page_437
+
+	BinaryMap := map[string][]byte{
+		// "\u0000\u263A\u263B\u2665\u2666\u2663\u2660\u2022": {0, 1, 2, 3, 4, 5, 6, 7},
+		"\u0000\u263A\u263B\u2665\u2666\u2663\u2660\x07": {0, 1, 2, 3, 4, 5, 6, 7},
+		// "\u25D8\u25CB\u25D9\u2642\u2640\u266A\u266B\u263C": {8, 9, 10, 11, 12, 13, 14, 15},
+		"\x08\u25CB\x0a\u2642\u2640\x0d\u266B\u263C":       {8, 9, 10, 11, 12, 13, 14, 15},
+		"\u25BA\u25C4\u2195\u203C\u00B6\u00A7\u25AC\u21A8": {16, 17, 18, 19, 20, 21, 22, 23},
+		// "\u2191\u2193\u2192\u2190\u221F\u2194\u25B2\u25BC": {24, 25, 26, 27, 28, 29, 30, 31},
+		"\u2191\u2193\u2192\x1b\u221F\u2194\u25B2\u25BC":   {24, 25, 26, 27, 28, 29, 30, 31},
+		"\u0020\u0021\u0022\u0023\u0024\u0025\u0026\u0027": {32, 33, 34, 35, 36, 37, 38, 39},
+		"\u0028\u0029\u002A\u002B\u002C\u002D\u002E\u002F": {40, 41, 42, 43, 44, 45, 46, 47},
+		"\u0030\u0031\u0032\u0033\u0034\u0035\u0036\u0037": {48, 49, 50, 51, 52, 53, 54, 55},
+		"\u0038\u0039\u003A\u003B\u003C\u003D\u003E\u003F": {56, 57, 58, 59, 60, 61, 62, 63},
+		"\u0040\u0041\u0042\u0043\u0044\u0045\u0046\u0047": {64, 65, 66, 67, 68, 69, 70, 71},
+		"\u0048\u0049\u004A\u004B\u004C\u004D\u004E\u004F": {72, 73, 74, 75, 76, 77, 78, 79},
+		"\u0050\u0051\u0052\u0053\u0054\u0055\u0056\u0057": {80, 81, 82, 83, 84, 85, 86, 87},
+		"\u0058\u0059\u005A\u005B\u005C\u005D\u005E\u005F": {88, 89, 90, 91, 92, 93, 94, 95},
+		"\u0060\u0061\u0062\u0063\u0064\u0065\u0066\u0067": {96, 97, 98, 99, 100, 101, 102, 103},
+		"\u0068\u0069\u006A\u006B\u006C\u006D\u006E\u006F": {104, 105, 106, 107, 108, 109, 110, 111},
+		"\u0070\u0071\u0072\u0073\u0074\u0075\u0076\u0077": {112, 113, 114, 115, 116, 117, 118, 119},
+		"\u0078\u0079\u007A\u007B\u007C\u007D\u007E\u2302": {120, 121, 122, 123, 124, 125, 126, 127},
+		"\u00C7\u00FC\u00E9\u00E2\u00E4\u00E0\u00E5\u00E7": {128, 129, 130, 131, 132, 133, 134, 135},
+		"\u00EA\u00EB\u00E8\u00EF\u00EE\u00EC\u00C4\u00C5": {136, 137, 138, 139, 140, 141, 142, 143},
+		"\u00C9\u00E6\u00C6\u00F4\u00F6\u00F2\u00FB\u00F9": {144, 145, 146, 147, 148, 149, 150, 151},
+		"\u00FF\u00D6\u00DC\u00A2\u00A3\u00A5\u20A7\u0192": {152, 153, 154, 155, 156, 157, 158, 159},
+		"\u00E1\u00ED\u00F3\u00FA\u00F1\u00D1\u00AA\u00BA": {160, 161, 162, 163, 164, 165, 166, 167},
+		"\u00BF\u2310\u00AC\u00BD\u00BC\u00A1\u00AB\u00BB": {168, 169, 170, 171, 172, 173, 174, 175},
+		"\u2591\u2592\u2593\u2502\u2524\u2561\u2562\u2556": {176, 177, 178, 179, 180, 181, 182, 183},
+		"\u2555\u2563\u2551\u2557\u255D\u255C\u255B\u2510": {184, 185, 186, 187, 188, 189, 190, 191},
+		"\u2514\u2534\u252C\u251C\u2500\u253C\u255E\u255F": {192, 193, 194, 195, 196, 197, 198, 199},
+		"\u255A\u2554\u2569\u2566\u2560\u2550\u256C\u2567": {200, 201, 202, 203, 204, 205, 206, 207},
+		"\u2568\u2564\u2565\u2559\u2558\u2552\u2553\u256B": {208, 209, 210, 211, 212, 213, 214, 215},
+		"\u256A\u2518\u250C\u2588\u2584\u258C\u2590\u2580": {216, 217, 218, 219, 220, 221, 222, 223},
+		"\u03B1\u00DF\u0393\u03C0\u03A3\u03C3\u00B5\u03C4": {224, 225, 226, 227, 228, 229, 230, 231},
+		"\u03A6\u0398\u03A9\u03B4\u221E\u03C6\u03B5\u2229": {232, 233, 234, 235, 236, 237, 238, 239},
+		"\u2261\u00B1\u2265\u2264\u2320\u2321\u00F7\u2248": {240, 241, 242, 243, 244, 245, 246, 247},
+		"\u00B0\u2219\u00B7\u221A\u207F\u00B2\u25A0\u00A0": {248, 249, 250, 251, 252, 253, 254, 255},
+	}
+
+	for output, binary := range BinaryMap {
+		value := CP437_to_Unicode(string(binary))
+		if output != value {
+			t.Errorf("CP437_to_Unicode: Expected %#v (%#v), got %#v", output, binary, value)
+		}
+	}
+}

+ 4 - 4
door/input.go

@@ -230,7 +230,7 @@ func (d *Door) GetKey() int {
 			// Just escape key
 			return c
 		}
-		var extended string = string(c2)
+		var extended string = string(byte(c2))
 
 		c2 = d.getkey_or_pushback()
 		for c2 > 0 {
@@ -238,7 +238,7 @@ func (d *Door) GetKey() int {
 				d.Pushback.PushBack(c2)
 				break
 			}
-			extended += string(c2)
+			extended += string(byte(c2))
 			c2 = d.getkey_or_pushback()
 		}
 
@@ -399,8 +399,8 @@ func (d *Door) Input(max int) string {
 
 		if strconv.IsPrint(rune(c)) {
 			if len(line) < max {
-				d.Write(string(c))
-				line += string(c)
+				d.Write(string(byte(c)))
+				line += string(byte(c))
 			} else {
 				d.Write("\x07")
 			}

+ 73 - 0
door/line_test.go

@@ -0,0 +1,73 @@
+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)
+	}
+
+}