github.com/carlanton/docker@v1.8.0-rc1/pkg/term/winconsole/console_windows_test.go (about) 1 // +build windows 2 3 package winconsole 4 5 import ( 6 "fmt" 7 "testing" 8 ) 9 10 func helpsTestParseInt16OrDefault(t *testing.T, expectedValue int16, shouldFail bool, input string, defaultValue int16, format string, args ...string) { 11 value, err := parseInt16OrDefault(input, defaultValue) 12 if nil != err && !shouldFail { 13 t.Errorf("Unexpected error returned %v", err) 14 t.Errorf(format, args) 15 } 16 if nil == err && shouldFail { 17 t.Errorf("Should have failed as expected\n\tReturned value = %d", value) 18 t.Errorf(format, args) 19 } 20 if expectedValue != value { 21 t.Errorf("The value returned does not match expected\n\tExpected:%v\n\t:Actual%v", expectedValue, value) 22 t.Errorf(format, args) 23 } 24 } 25 26 func TestParseInt16OrDefault(t *testing.T) { 27 // empty string 28 helpsTestParseInt16OrDefault(t, 0, false, "", 0, "Empty string returns default") 29 helpsTestParseInt16OrDefault(t, 2, false, "", 2, "Empty string returns default") 30 31 // normal case 32 helpsTestParseInt16OrDefault(t, 0, false, "0", 0, "0 handled correctly") 33 helpsTestParseInt16OrDefault(t, 111, false, "111", 2, "Normal") 34 helpsTestParseInt16OrDefault(t, 111, false, "+111", 2, "+N") 35 helpsTestParseInt16OrDefault(t, -111, false, "-111", 2, "-N") 36 helpsTestParseInt16OrDefault(t, 0, false, "+0", 11, "+0") 37 helpsTestParseInt16OrDefault(t, 0, false, "-0", 12, "-0") 38 39 // ill formed strings 40 helpsTestParseInt16OrDefault(t, 0, true, "abc", 0, "Invalid string") 41 helpsTestParseInt16OrDefault(t, 42, true, "+= 23", 42, "Invalid string") 42 helpsTestParseInt16OrDefault(t, 42, true, "123.45", 42, "float like") 43 44 } 45 46 func helpsTestGetNumberOfChars(t *testing.T, expected uint32, fromCoord COORD, toCoord COORD, screenSize COORD, format string, args ...interface{}) { 47 actual := getNumberOfChars(fromCoord, toCoord, screenSize) 48 mesg := fmt.Sprintf(format, args) 49 assertTrue(t, expected == actual, fmt.Sprintf("%s Expected=%d, Actual=%d, Parameters = { fromCoord=%+v, toCoord=%+v, screenSize=%+v", mesg, expected, actual, fromCoord, toCoord, screenSize)) 50 } 51 52 func TestGetNumberOfChars(t *testing.T) { 53 // Note: The columns and lines are 0 based 54 // Also that interval is "inclusive" means will have both start and end chars 55 // This test only tests the number opf characters being written 56 57 // all four corners 58 maxWindow := COORD{X: 80, Y: 50} 59 leftTop := COORD{X: 0, Y: 0} 60 rightTop := COORD{X: 79, Y: 0} 61 leftBottom := COORD{X: 0, Y: 49} 62 rightBottom := COORD{X: 79, Y: 49} 63 64 // same position 65 helpsTestGetNumberOfChars(t, 1, COORD{X: 1, Y: 14}, COORD{X: 1, Y: 14}, COORD{X: 80, Y: 50}, "Same position random line") 66 67 // four corners 68 helpsTestGetNumberOfChars(t, 1, leftTop, leftTop, maxWindow, "Same position- leftTop") 69 helpsTestGetNumberOfChars(t, 1, rightTop, rightTop, maxWindow, "Same position- rightTop") 70 helpsTestGetNumberOfChars(t, 1, leftBottom, leftBottom, maxWindow, "Same position- leftBottom") 71 helpsTestGetNumberOfChars(t, 1, rightBottom, rightBottom, maxWindow, "Same position- rightBottom") 72 73 // from this char to next char on same line 74 helpsTestGetNumberOfChars(t, 2, COORD{X: 0, Y: 0}, COORD{X: 1, Y: 0}, maxWindow, "Next position on same line") 75 helpsTestGetNumberOfChars(t, 2, COORD{X: 1, Y: 14}, COORD{X: 2, Y: 14}, maxWindow, "Next position on same line") 76 77 // from this char to next 10 chars on same line 78 helpsTestGetNumberOfChars(t, 11, COORD{X: 0, Y: 0}, COORD{X: 10, Y: 0}, maxWindow, "Next position on same line") 79 helpsTestGetNumberOfChars(t, 11, COORD{X: 1, Y: 14}, COORD{X: 11, Y: 14}, maxWindow, "Next position on same line") 80 81 helpsTestGetNumberOfChars(t, 5, COORD{X: 3, Y: 11}, COORD{X: 7, Y: 11}, maxWindow, "To and from on same line") 82 83 helpsTestGetNumberOfChars(t, 8, COORD{X: 0, Y: 34}, COORD{X: 7, Y: 34}, maxWindow, "Start of line to middle") 84 helpsTestGetNumberOfChars(t, 4, COORD{X: 76, Y: 34}, COORD{X: 79, Y: 34}, maxWindow, "Middle to end of line") 85 86 // multiple lines - 1 87 helpsTestGetNumberOfChars(t, 81, COORD{X: 0, Y: 0}, COORD{X: 0, Y: 1}, maxWindow, "one line below same X") 88 helpsTestGetNumberOfChars(t, 81, COORD{X: 10, Y: 10}, COORD{X: 10, Y: 11}, maxWindow, "one line below same X") 89 90 // multiple lines - 2 91 helpsTestGetNumberOfChars(t, 161, COORD{X: 0, Y: 0}, COORD{X: 0, Y: 2}, maxWindow, "one line below same X") 92 helpsTestGetNumberOfChars(t, 161, COORD{X: 10, Y: 10}, COORD{X: 10, Y: 12}, maxWindow, "one line below same X") 93 94 // multiple lines - 3 95 helpsTestGetNumberOfChars(t, 241, COORD{X: 0, Y: 0}, COORD{X: 0, Y: 3}, maxWindow, "one line below same X") 96 helpsTestGetNumberOfChars(t, 241, COORD{X: 10, Y: 10}, COORD{X: 10, Y: 13}, maxWindow, "one line below same X") 97 98 // full line 99 helpsTestGetNumberOfChars(t, 80, COORD{X: 0, Y: 0}, COORD{X: 79, Y: 0}, maxWindow, "Full line - first") 100 helpsTestGetNumberOfChars(t, 80, COORD{X: 0, Y: 23}, COORD{X: 79, Y: 23}, maxWindow, "Full line - random") 101 helpsTestGetNumberOfChars(t, 80, COORD{X: 0, Y: 49}, COORD{X: 79, Y: 49}, maxWindow, "Full line - last") 102 103 // full screen 104 helpsTestGetNumberOfChars(t, 80*50, leftTop, rightBottom, maxWindow, "full screen") 105 106 helpsTestGetNumberOfChars(t, 80*50-1, COORD{X: 1, Y: 0}, rightBottom, maxWindow, "dropping first char to, end of screen") 107 helpsTestGetNumberOfChars(t, 80*50-2, COORD{X: 2, Y: 0}, rightBottom, maxWindow, "dropping first two char to, end of screen") 108 109 helpsTestGetNumberOfChars(t, 80*50-1, leftTop, COORD{X: 78, Y: 49}, maxWindow, "from start of screen, till last char-1") 110 helpsTestGetNumberOfChars(t, 80*50-2, leftTop, COORD{X: 77, Y: 49}, maxWindow, "from start of screen, till last char-2") 111 112 helpsTestGetNumberOfChars(t, 80*50-5, COORD{X: 4, Y: 0}, COORD{X: 78, Y: 49}, COORD{X: 80, Y: 50}, "from start of screen+4, till last char-1") 113 helpsTestGetNumberOfChars(t, 80*50-6, COORD{X: 4, Y: 0}, COORD{X: 77, Y: 49}, COORD{X: 80, Y: 50}, "from start of screen+4, till last char-2") 114 } 115 116 var allForeground = []int16{ 117 ANSI_FOREGROUND_BLACK, 118 ANSI_FOREGROUND_RED, 119 ANSI_FOREGROUND_GREEN, 120 ANSI_FOREGROUND_YELLOW, 121 ANSI_FOREGROUND_BLUE, 122 ANSI_FOREGROUND_MAGENTA, 123 ANSI_FOREGROUND_CYAN, 124 ANSI_FOREGROUND_WHITE, 125 ANSI_FOREGROUND_DEFAULT, 126 } 127 var allBackground = []int16{ 128 ANSI_BACKGROUND_BLACK, 129 ANSI_BACKGROUND_RED, 130 ANSI_BACKGROUND_GREEN, 131 ANSI_BACKGROUND_YELLOW, 132 ANSI_BACKGROUND_BLUE, 133 ANSI_BACKGROUND_MAGENTA, 134 ANSI_BACKGROUND_CYAN, 135 ANSI_BACKGROUND_WHITE, 136 ANSI_BACKGROUND_DEFAULT, 137 } 138 139 func maskForeground(flag WORD) WORD { 140 return flag & FOREGROUND_MASK_UNSET 141 } 142 143 func onlyForeground(flag WORD) WORD { 144 return flag & FOREGROUND_MASK_SET 145 } 146 147 func maskBackground(flag WORD) WORD { 148 return flag & BACKGROUND_MASK_UNSET 149 } 150 151 func onlyBackground(flag WORD) WORD { 152 return flag & BACKGROUND_MASK_SET 153 } 154 155 func helpsTestGetWindowsTextAttributeForAnsiValue(t *testing.T, oldValue WORD /*, expected WORD*/, ansi int16, onlyMask WORD, restMask WORD) WORD { 156 actual, err := getWindowsTextAttributeForAnsiValue(oldValue, FOREGROUND_MASK_SET, ansi) 157 assertTrue(t, nil == err, "Should be no error") 158 // assert that other bits are not affected 159 if 0 != oldValue { 160 assertTrue(t, (actual&restMask) == (oldValue&restMask), "The operation should not have affected other bits actual=%X oldValue=%X ansi=%d", actual, oldValue, ansi) 161 } 162 return actual 163 } 164 165 func TestBackgroundForAnsiValue(t *testing.T) { 166 // Check that nothing else changes 167 // background changes 168 for _, state1 := range allBackground { 169 for _, state2 := range allBackground { 170 flag := WORD(0) 171 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state1, BACKGROUND_MASK_SET, BACKGROUND_MASK_UNSET) 172 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state2, BACKGROUND_MASK_SET, BACKGROUND_MASK_UNSET) 173 } 174 } 175 // cummulative bcakground changes 176 for _, state1 := range allBackground { 177 flag := WORD(0) 178 for _, state2 := range allBackground { 179 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state1, BACKGROUND_MASK_SET, BACKGROUND_MASK_UNSET) 180 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state2, BACKGROUND_MASK_SET, BACKGROUND_MASK_UNSET) 181 } 182 } 183 // change background after foreground 184 for _, state1 := range allForeground { 185 for _, state2 := range allBackground { 186 flag := WORD(0) 187 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state1, FOREGROUND_MASK_SET, FOREGROUND_MASK_UNSET) 188 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state2, BACKGROUND_MASK_SET, BACKGROUND_MASK_UNSET) 189 } 190 } 191 // change background after change cumulative 192 for _, state1 := range allForeground { 193 flag := WORD(0) 194 for _, state2 := range allBackground { 195 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state1, FOREGROUND_MASK_SET, FOREGROUND_MASK_UNSET) 196 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state2, BACKGROUND_MASK_SET, BACKGROUND_MASK_UNSET) 197 } 198 } 199 } 200 201 func TestForegroundForAnsiValue(t *testing.T) { 202 // Check that nothing else changes 203 for _, state1 := range allForeground { 204 for _, state2 := range allForeground { 205 flag := WORD(0) 206 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state1, FOREGROUND_MASK_SET, FOREGROUND_MASK_UNSET) 207 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state2, FOREGROUND_MASK_SET, FOREGROUND_MASK_UNSET) 208 } 209 } 210 211 for _, state1 := range allForeground { 212 flag := WORD(0) 213 for _, state2 := range allForeground { 214 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state1, FOREGROUND_MASK_SET, FOREGROUND_MASK_UNSET) 215 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state2, FOREGROUND_MASK_SET, FOREGROUND_MASK_UNSET) 216 } 217 } 218 for _, state1 := range allBackground { 219 for _, state2 := range allForeground { 220 flag := WORD(0) 221 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state1, BACKGROUND_MASK_SET, BACKGROUND_MASK_UNSET) 222 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state2, FOREGROUND_MASK_SET, FOREGROUND_MASK_UNSET) 223 } 224 } 225 for _, state1 := range allBackground { 226 flag := WORD(0) 227 for _, state2 := range allForeground { 228 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state1, BACKGROUND_MASK_SET, BACKGROUND_MASK_UNSET) 229 flag = helpsTestGetWindowsTextAttributeForAnsiValue(t, flag, state2, FOREGROUND_MASK_SET, FOREGROUND_MASK_UNSET) 230 } 231 } 232 }