@@ -44,6 +44,49 @@ function addTest(sequences, expectedKeys) {
44
44
assert . deepStrictEqual ( keys , expectedKeys ) ;
45
45
}
46
46
47
+ // Simulate key interval test cases
48
+ // Returns a function that takes `next` test case and returns a thunk
49
+ // that can be called to run tests in sequence
50
+ // e.g.
51
+ // addKeyIntervalTest(..)
52
+ // (addKeyIntervalTest(..)
53
+ // (addKeyIntervalTest(..)(noop)))()
54
+ // where noop is a terminal function(() => {}).
55
+
56
+ const addKeyIntervalTest = ( sequences , expectedKeys , interval = 550 ,
57
+ assertDelay = 550 ) => {
58
+ return ( next ) => ( ) => {
59
+
60
+ if ( ! Array . isArray ( sequences ) ) {
61
+ sequences = [ sequences ] ;
62
+ }
63
+
64
+ if ( ! Array . isArray ( expectedKeys ) ) {
65
+ expectedKeys = [ expectedKeys ] ;
66
+ }
67
+
68
+ expectedKeys = expectedKeys . map ( function ( k ) {
69
+ return k ? extend ( { ctrl : false , meta : false , shift : false } , k ) : k ;
70
+ } ) ;
71
+
72
+ const keys = [ ] ;
73
+ fi . on ( 'keypress' , ( s , k ) => keys . push ( k ) ) ;
74
+
75
+ const emitKeys = ( [ head , ...tail ] ) => {
76
+ if ( head ) {
77
+ fi . write ( head ) ;
78
+ setTimeout ( ( ) => emitKeys ( tail ) , interval ) ;
79
+ } else {
80
+ setTimeout ( ( ) => {
81
+ next ( ) ;
82
+ assert . deepStrictEqual ( keys , expectedKeys ) ;
83
+ } , assertDelay ) ;
84
+ }
85
+ } ;
86
+ emitKeys ( sequences ) ;
87
+ } ;
88
+ } ;
89
+
47
90
// regular alphanumerics
48
91
addTest ( 'io.JS' , [
49
92
{ name : 'i' , sequence : 'i' } ,
@@ -149,3 +192,22 @@ addTest('\x1b[31ma\x1b[39ma', [
149
192
{ name : 'undefined' , sequence : '\x1b[39m' , code : '[39m' } ,
150
193
{ name : 'a' , sequence : 'a' } ,
151
194
] ) ;
195
+
196
+ // Reduce array of addKeyIntervalTest(..) right to left
197
+ // with () => {} as initial function
198
+ const runKeyIntervalTests = [
199
+ // escape character
200
+ addKeyIntervalTest ( '\x1b' , [
201
+ { name : 'escape' , sequence : '\x1b' , meta : true }
202
+ ] ) ,
203
+ // chain of escape characters
204
+ addKeyIntervalTest ( '\x1b\x1b\x1b\x1b' . split ( '' ) , [
205
+ { name : 'escape' , sequence : '\x1b' , meta : true } ,
206
+ { name : 'escape' , sequence : '\x1b' , meta : true } ,
207
+ { name : 'escape' , sequence : '\x1b' , meta : true } ,
208
+ { name : 'escape' , sequence : '\x1b' , meta : true }
209
+ ] )
210
+ ] . reverse ( ) . reduce ( ( acc , fn ) => fn ( acc ) , ( ) => { } ) ;
211
+
212
+ // run key interval tests one after another
213
+ runKeyIntervalTests ( ) ;
0 commit comments