| 1 |
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file. |
| 2 |
import sys |
| 3 |
from unittest import TestCase, main, skipUnless |
| 4 |
|
| 5 |
try: |
| 6 |
from unittest.mock import Mock, patch |
| 7 |
except ImportError: |
| 8 |
from mock import Mock, patch |
| 9 |
|
| 10 |
from ..winterm import WinColor, WinStyle, WinTerm |
| 11 |
|
| 12 |
|
| 13 |
class WinTermTest(TestCase): |
| 14 |
|
| 15 |
@patch('colorama.winterm.win32') |
| 16 |
def testInit(self, mockWin32): |
| 17 |
mockAttr = Mock() |
| 18 |
mockAttr.wAttributes = 7 + 6 * 16 + 8 |
| 19 |
mockWin32.GetConsoleScreenBufferInfo.return_value = mockAttr |
| 20 |
term = WinTerm() |
| 21 |
self.assertEqual(term._fore, 7) |
| 22 |
self.assertEqual(term._back, 6) |
| 23 |
self.assertEqual(term._style, 8) |
| 24 |
|
| 25 |
@skipUnless(sys.platform.startswith("win"), "requires Windows") |
| 26 |
def testGetAttrs(self): |
| 27 |
term = WinTerm() |
| 28 |
|
| 29 |
term._fore = 0 |
| 30 |
term._back = 0 |
| 31 |
term._style = 0 |
| 32 |
self.assertEqual(term.get_attrs(), 0) |
| 33 |
|
| 34 |
term._fore = WinColor.YELLOW |
| 35 |
self.assertEqual(term.get_attrs(), WinColor.YELLOW) |
| 36 |
|
| 37 |
term._back = WinColor.MAGENTA |
| 38 |
self.assertEqual( |
| 39 |
term.get_attrs(), |
| 40 |
WinColor.YELLOW + WinColor.MAGENTA * 16) |
| 41 |
|
| 42 |
term._style = WinStyle.BRIGHT |
| 43 |
self.assertEqual( |
| 44 |
term.get_attrs(), |
| 45 |
WinColor.YELLOW + WinColor.MAGENTA * 16 + WinStyle.BRIGHT) |
| 46 |
|
| 47 |
@patch('colorama.winterm.win32') |
| 48 |
def testResetAll(self, mockWin32): |
| 49 |
mockAttr = Mock() |
| 50 |
mockAttr.wAttributes = 1 + 2 * 16 + 8 |
| 51 |
mockWin32.GetConsoleScreenBufferInfo.return_value = mockAttr |
| 52 |
term = WinTerm() |
| 53 |
|
| 54 |
term.set_console = Mock() |
| 55 |
term._fore = -1 |
| 56 |
term._back = -1 |
| 57 |
term._style = -1 |
| 58 |
|
| 59 |
term.reset_all() |
| 60 |
|
| 61 |
self.assertEqual(term._fore, 1) |
| 62 |
self.assertEqual(term._back, 2) |
| 63 |
self.assertEqual(term._style, 8) |
| 64 |
self.assertEqual(term.set_console.called, True) |
| 65 |
|
| 66 |
@skipUnless(sys.platform.startswith("win"), "requires Windows") |
| 67 |
def testFore(self): |
| 68 |
term = WinTerm() |
| 69 |
term.set_console = Mock() |
| 70 |
term._fore = 0 |
| 71 |
|
| 72 |
term.fore(5) |
| 73 |
|
| 74 |
self.assertEqual(term._fore, 5) |
| 75 |
self.assertEqual(term.set_console.called, True) |
| 76 |
|
| 77 |
@skipUnless(sys.platform.startswith("win"), "requires Windows") |
| 78 |
def testBack(self): |
| 79 |
term = WinTerm() |
| 80 |
term.set_console = Mock() |
| 81 |
term._back = 0 |
| 82 |
|
| 83 |
term.back(5) |
| 84 |
|
| 85 |
self.assertEqual(term._back, 5) |
| 86 |
self.assertEqual(term.set_console.called, True) |
| 87 |
|
| 88 |
@skipUnless(sys.platform.startswith("win"), "requires Windows") |
| 89 |
def testStyle(self): |
| 90 |
term = WinTerm() |
| 91 |
term.set_console = Mock() |
| 92 |
term._style = 0 |
| 93 |
|
| 94 |
term.style(22) |
| 95 |
|
| 96 |
self.assertEqual(term._style, 22) |
| 97 |
self.assertEqual(term.set_console.called, True) |
| 98 |
|
| 99 |
@patch('colorama.winterm.win32') |
| 100 |
def testSetConsole(self, mockWin32): |
| 101 |
mockAttr = Mock() |
| 102 |
mockAttr.wAttributes = 0 |
| 103 |
mockWin32.GetConsoleScreenBufferInfo.return_value = mockAttr |
| 104 |
term = WinTerm() |
| 105 |
term.windll = Mock() |
| 106 |
|
| 107 |
term.set_console() |
| 108 |
|
| 109 |
self.assertEqual( |
| 110 |
mockWin32.SetConsoleTextAttribute.call_args, |
| 111 |
((mockWin32.STDOUT, term.get_attrs()), {}) |
| 112 |
) |
| 113 |
|
| 114 |
@patch('colorama.winterm.win32') |
| 115 |
def testSetConsoleOnStderr(self, mockWin32): |
| 116 |
mockAttr = Mock() |
| 117 |
mockAttr.wAttributes = 0 |
| 118 |
mockWin32.GetConsoleScreenBufferInfo.return_value = mockAttr |
| 119 |
term = WinTerm() |
| 120 |
term.windll = Mock() |
| 121 |
|
| 122 |
term.set_console(on_stderr=True) |
| 123 |
|
| 124 |
self.assertEqual( |
| 125 |
mockWin32.SetConsoleTextAttribute.call_args, |
| 126 |
((mockWin32.STDERR, term.get_attrs()), {}) |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
if __name__ == '__main__': |
| 131 |
main() |