Fixed allSelectorsSent because a simpler implementation was overriding the original.
[cslatevm.git] / tests / smartconsole.slate
blob337be7b532ee072c9bb46ca5e1739832e909a57d
3 Image saving does not work! If you want to save an image after using SmartConsole
4 you have to execute the line below, otherwise the reference to the dll is saved
5 which will cause a crash after loading the saved image and using SmartConsole.
7 CursesConsole traits lib: Nil.
8 Image save.
11 UnitTests addPrototype: #SmartConsole derivedFrom: {Cloneable}.
12 "Not a real unit test, it's an object to dispatch on for a few
13 methods that test SmartConsole/Terminal functionality with human
14 interaction."
16 _@(UnitTests SmartConsole traits) streamedCommandProcessor
18   SmartConsole new sessionDo: [| :console terminal editor reader |
19     terminal: (Terminal newBasedOn: console).
20     editor: terminal commandEditor.
21     editor prompt: 'Prompt> '.
22     reader: editor reader.
24     terminal ; 'This test should echo all lines committed by enters, and exit if a \'q\' character is encountered'.
26     [| char |
27       char: reader next.
28       terminal nextPut: char.
29       "char printOn: terminal &radix: 16."
30       terminal flush.
31       char = $q
32     ] whileFalse.
33   ].
36 _@(UnitTests SmartConsole traits) commandProcessor
38   SmartConsole new sessionDo: [| :console terminal editor |
39     terminal: (Terminal newBasedOn: console).
40     editor: terminal commandEditor.
41     editor prompt: 'Prompt> '.
42     
43     terminal ; 'This test should echo lines ending with a dot and committed with enters; quit with the \'quit.\' command'.
44     terminal newLine.
45     console writer ; 'Also demonstrates editing at any column: '.
47     [| line |
48       [ line: editor readLine.
49         ["console writeCursorPositionAt: (console width // 2) , (console height // 2)."
50          line size = 0 \/ [(line at: line indexLast) ~= $. ]]
51           whileTrue: [line: (editor continue)].
53         terminal newLine.
54         terminal ; '\'' ; line ; '\''.
55         terminal newLine.
56         terminal flush]
57       on: Stream Exhaustion do: [| :_ |
58         terminal ; '\nStream closed\n'].
60      line ~= Nil /\ [line = 'quit.']
61    ] whileFalse.
62   ].
65 _@(UnitTests SmartConsole traits) dimensionInfo
67   SmartConsole new sessionDo: [| :console |
68     
69     console writer ; 'This test should write the screen dimentions in the center; quit with the Escape key'.
70     [
71       console
72         writePosition: (console width , console height)
73         at: ((console width // 2) , (console height // 2)).
75       console events next keyName = #Escape
76     ] whileFalse.
77   ].
80 _@(UnitTests SmartConsole traits) draw
82   SmartConsole new sessionDo: [| :console |
83     console ; 'Move with arrows, scroll with space, change color with 1-8, type chars, Esc quits\n'.
85     console events do: [| :event |
86       event keyName caseOf: {
87         #Escape      -> [^ Nil].
88         #Enter       -> [console moveCursorToBONL].
89         #LeftArrow   -> [console moveCursorLeft].
90         #RightArrow  -> [console moveCursorRight].
91         #UpArrow     -> [console moveCursorUp].
92         #DownArrow   -> [console moveCursorDown].
93       } otherwise: [
94         event char ifNotNil: [
95           ((event char as: Integer) between: 16r31 and: 16r40)
96             ifTrue: [console foregroundColor: (event char as: Integer) - 16r31]
97             ifFalse: [
98               event char = $\s
99                 ifTrue: [console scroll]
100                 ifFalse: [console writer nextPut: event char]]]].
101       console writeCursorPositionAt: {console width // 2. console height // 2}.
102     ].
103   ].
106 _@(UnitTests SmartConsole traits) echoEvent: event@(SmartConsole ResizeEvent traits) on: terminal
107 [| console |
108   console: terminal console.
109   terminal ; 'Console resized to: ' ; (console width as: String) ; 'x' ; (console height as: String).
110   console clearToEOL.
111   terminal newLine.
112   False
115 _@(UnitTests SmartConsole traits) echoEvent: event@(SmartConsole KeyEvent traits) on: terminal
116 [| console |
117   console: terminal console.
118   terminal ; 'Key code: ' ; (event keyCode as: String) ; ' - 16r' ;
119     (event keyCode printString &radix: 16).
120   console clearToEOL.
121   terminal newLine.
123   terminal ; 'Key name: '.
124   event keyName
125     ifNil: [terminal ; 'Nil']
126     ifNotNil: [terminal ; (event keyName as: terminal collectionType)].
127   console clearToEOL.
128   terminal newLine.
130   terminal ; 'Character: '.
131   event char
132     ifNil: [terminal ; 'Nil']
133     ifNotNil: [terminal nextPut: event char].
134   console clearToEOL.
135   terminal newLine.
137   terminal ; 'Controls: ' ; event controls printString.
138   console clearToEOL.
139   terminal newLine.
141   console writeCursorPositionAt: {console width // 2. console height // 2}.
143   event keyName = #Escape
146 t@(UnitTests SmartConsole traits) echoEvents
148   SmartConsole new sessionDo: [| :console terminal |
149    terminal: (Terminal newBasedOn: console).
150    terminal ; 'Press keys and their event will be printed, quit with Esc\n'.
152     [
153 "      console moveCursorTo: (0 , 1)."
154       t echoEvent: console events next on: terminal
155     ] whileFalse
156   ]
159 _@(UnitTests SmartConsole traits) echoRawCursesEvents
161   CursesConsole new sessionDo: [| :console terminal |
162    terminal: (Terminal newBasedOn: console).
163    terminal ; 'Press keys and raw key codes will be printed, quit with Enter\n'.
165     [| keyCode |
166       keyCode: (console primitives nextEvent applyTo: {-1}).
167       terminal ; (keyCode as: String) ; ' - 16r' ;
168         (keyCode printString &radix: 16).
169       console clearToEOL.
170       terminal newLine.
171       keyCode = 10 \/ [keyCode = 13]
172     ] whileFalse
173   ]
176 _@(UnitTests SmartConsole traits) echoRawWindowsEvents &unicode: unicode
177 [| available |
178   available: False.
179   [WindowsConsole isAvailable ifTrue: [available: True]]
180     breakOn: Error.
181   available ifFalse: [error: 'Windows console plugin not loaded or otherwise not available'. ^ Nil].
182   unicode ifNil: [unicode: False].
184   WindowsConsole clone sessionDo: [| :c record dword |
185     c ; 'Printing out raw console events coming from Windows, quit with Esc\n'.
186     c ; 'Codepage returned by GetConsoleCP: ' ; Windows Kernel GetConsoleCP do printString ; '\n'.
188     record: Windows INPUT_RECORD clone.
189     dword: (ByteArray new &capacity: #[Windows DWORD byteSize]).
190     [
191       [| method |
192         unicode
193           ifTrue: [method: Windows Kernel ReadConsoleInputW]
194           ifFalse: [method: Windows Kernel ReadConsoleInputA].
195         method applyTo: {c consoleIn. record. 1. dword}.
196         record EventType = #[Windows KEY_EVENT]
197       ] whileFalse.
198       
199       "record printOn: c writer.
200       c newLine."
201       c ; 'Down ' ; record Event_KeyEvent_bKeyDown printString
202         ; ', Repeat ' ; record Event_KeyEvent_wRepeatCount printString
203         ; ', VKeyCode ' ; record Event_KeyEvent_wVirtualKeyCode printString
204         ; ', VScanCode ' ; record Event_KeyEvent_wVirtualScanCode printString.
205       unicode
206         ifTrue: [c ; ', Unicode ' ; (record Event_KeyEvent_uChar_UnicodeChar printString &radox: 16)]
207         ifFalse: [
208           c ; ', Ascii ' ; (record Event_KeyEvent_uChar_AsciiChar printString &radix: 16).
209           (record Event_KeyEvent_uChar_AsciiChar between: 0 and: 255)
210             ifTrue: [
211               c ; ' ('.
212               c writer nextPut: (record Event_KeyEvent_uChar_AsciiChar as: ASCIICharacter).
213               c ; ')']].
214       c  ; ', Controls ' ; (record Event_KeyEvent_dwControlKeyState printString &radix: 2) ; ' ('.
215       {
216         #RIGHT_ALT_PRESSED.
217         #LEFT_ALT_PRESSED.
218         #RIGHT_CTRL_PRESSED.
219         #LEFT_CTRL_PRESSED.
220         #SHIFT_PRESSED.
221         #NUMLOCK_ON.
222         #SCROLLLOCK_ON.
223         #CAPSLOCK_ON.
224         #ENHANCED_KEY.
225       } inject: 0 into: [| :bitCount :symbol |
226         (record Event_KeyEvent_dwControlKeyState bitAnd: (symbol sendTo: {Windows})) isZero
227           ifTrue: [bitCount]
228           ifFalse: [
229             bitCount isZero ifFalse: [c ; ','].
230             c ; symbol name.
231             bitCount + 1]].
232       c ; ')\n'.
233       record Event_KeyEvent_wVirtualKeyCode = 27
234     ] whileFalse.
235   ]