@ -32,100 +32,104 @@ function FakeInput() {
inherits ( FakeInput , EventEmitter ) ;
inherits ( FakeInput , EventEmitter ) ;
FakeInput . prototype . resume = function ( ) { } ;
FakeInput . prototype . resume = function ( ) { } ;
FakeInput . prototype . pause = function ( ) { } ;
FakeInput . prototype . pause = function ( ) { } ;
FakeInput . prototype . write = function ( ) { } ;
FakeInput . prototype . end = function ( ) { } ;
var fi ;
[ true , false ] . forEach ( function ( terminal ) {
var rli ;
var fi ;
var called ;
var rli ;
var called ;
// sending a full line
// sending a full line
fi = new FakeInput ( ) ;
fi = new FakeInput ( ) ;
rli = new readline . Interface ( fi , { } ) ;
rli = new readline . Interface ( { input : fi , output : fi , terminal : terminal } ) ;
called = false ;
called = false ;
rli . on ( 'line' , function ( line ) {
rli . on ( 'line' , function ( line ) {
called = true ;
called = true ;
assert . equal ( line , 'asdf' ) ;
assert . equal ( line , 'asdf' ) ;
} ) ;
} ) ;
fi . emit ( 'data' , 'asdf\n' ) ;
fi . emit ( 'data' , 'asdf\n' ) ;
assert . ok ( called ) ;
assert . ok ( called ) ;
// sending a blank line
// sending a blank line
fi = new FakeInput ( ) ;
fi = new FakeInput ( ) ;
rli = new readline . Interface ( fi , { } ) ;
rli = new readline . Interface ( { input : fi , output : fi , terminal : terminal } ) ;
called = false ;
called = false ;
rli . on ( 'line' , function ( line ) {
rli . on ( 'line' , function ( line ) {
called = true ;
called = true ;
assert . equal ( line , '' ) ;
assert . equal ( line , '' ) ;
} ) ;
} ) ;
fi . emit ( 'data' , '\n' ) ;
fi . emit ( 'data' , '\n' ) ;
assert . ok ( called ) ;
assert . ok ( called ) ;
// sending a single character with no newline
// sending a single character with no newline
fi = new FakeInput ( ) ;
fi = new FakeInput ( ) ;
rli = new readline . Interface ( fi , { } ) ;
rli = new readline . Interface ( fi , { } ) ;
called = false ;
called = false ;
rli . on ( 'line' , function ( line ) {
rli . on ( 'line' , function ( line ) {
called = true ;
called = true ;
} ) ;
} ) ;
fi . emit ( 'data' , 'a' ) ;
fi . emit ( 'data' , 'a' ) ;
assert . ok ( ! called ) ;
assert . ok ( ! called ) ;
rli . close ( ) ;
rli . close ( ) ;
// sending a single character with no newline and then a newline
// sending a single character with no newline and then a newline
fi = new FakeInput ( ) ;
fi = new FakeInput ( ) ;
rli = new readline . Interface ( fi , { } ) ;
rli = new readline . Interface ( { input : fi , output : fi , terminal : terminal } ) ;
called = false ;
called = false ;
rli . on ( 'line' , function ( line ) {
rli . on ( 'line' , function ( line ) {
called = true ;
called = true ;
assert . equal ( line , 'a' ) ;
assert . equal ( line , 'a' ) ;
} ) ;
} ) ;
fi . emit ( 'data' , 'a' ) ;
fi . emit ( 'data' , 'a' ) ;
assert . ok ( ! called ) ;
assert . ok ( ! called ) ;
fi . emit ( 'data' , '\n' ) ;
fi . emit ( 'data' , '\n' ) ;
assert . ok ( called ) ;
assert . ok ( called ) ;
rli . close ( ) ;
rli . close ( ) ;
// sending multiple newlines at once
// sending multiple newlines at once
fi = new FakeInput ( ) ;
fi = new FakeInput ( ) ;
rli = new readline . Interface ( fi , { } ) ;
rli = new readline . Interface ( { input : fi , output : fi , terminal : terminal } ) ;
var expectedLines = [ 'foo\n ' , 'bar\n ' , 'baz\n ' ] ;
var expectedLines = [ 'foo' , 'bar' , 'baz' ] ;
var callCount = 0 ;
var callCount = 0 ;
rli . on ( 'line' , function ( line ) {
rli . on ( 'line' , function ( line ) {
assert . equal ( line , expectedLines [ callCount ] ) ;
assert . equal ( line , expectedLines [ callCount ] ) ;
callCount ++ ;
callCount ++ ;
} ) ;
} ) ;
fi . emit ( 'data' , expectedLines . join ( '\n' ) + '\n' ) ;
fi . emit ( 'data' , expectedLines . join ( '\n' ) + '\n' ) ;
assert . equal ( callCount , expectedLines . length ) ;
assert . equal ( callCount , expectedLines . length ) ;
rli . close ( ) ;
rli . close ( ) ;
// sending multiple newlines at once that does not end with a new line
// sending multiple newlines at once that does not end with a new line
fi = new FakeInput ( ) ;
fi = new FakeInput ( ) ;
rli = new readline . Interface ( fi , { } ) ;
rli = new readline . Interface ( { input : fi , output : fi , terminal : terminal } ) ;
var expectedLines = [ 'foo\n ' , 'bar\n ' , 'baz\n ' , 'bat' ] ;
expectedLines = [ 'foo' , 'bar' , 'baz' , 'bat' ] ;
var callCount = 0 ;
callCount = 0 ;
rli . on ( 'line' , function ( line ) {
rli . on ( 'line' , function ( line ) {
assert . equal ( line , expectedLines [ callCount ] ) ;
assert . equal ( line , expectedLines [ callCount ] ) ;
callCount ++ ;
callCount ++ ;
} ) ;
} ) ;
fi . emit ( 'data' , expectedLines . join ( '\n' ) ) ;
fi . emit ( 'data' , expectedLines . join ( '\n' ) ) ;
assert . equal ( callCount , expectedLines . length - 1 ) ;
assert . equal ( callCount , expectedLines . length - 1 ) ;
rli . close ( ) ;
rli . close ( ) ;
// sending a multi-byte utf8 char over multiple writes
// sending a multi-byte utf8 char over multiple writes
var buf = Buffer ( '☮' , 'utf8' ) ;
var buf = Buffer ( '☮' , 'utf8' ) ;
fi = new FakeInput ( ) ;
fi = new FakeInput ( ) ;
rli = new readline . Interface ( fi , { } ) ;
rli = new readline . Interface ( { input : fi , output : fi , terminal : terminal } ) ;
callCount = 0 ;
callCount = 0 ;
rli . on ( 'line' , function ( line ) {
rli . on ( 'line' , function ( line ) {
callCount ++ ;
callCount ++ ;
assert . equal ( line , buf . toString ( 'utf8' ) ) ;
assert . equal ( line , buf . toString ( 'utf8' ) ) ;
} ) ;
} ) ;
[ ] . forEach . call ( buf , function ( i ) {
[ ] . forEach . call ( buf , function ( i ) {
fi . emit ( 'data' , Buffer ( [ i ] ) ) ;
fi . emit ( 'data' , Buffer ( [ i ] ) ) ;
} ) ;
} ) ;
assert . equal ( callCount , 0 ) ;
assert . equal ( callCount , 0 ) ;
fi . emit ( 'data' , '\n' ) ;
fi . emit ( 'data' , '\n' ) ;
assert . equal ( callCount , 1 ) ;
assert . equal ( callCount , 1 ) ;
rli . close ( ) ;
rli . close ( ) ;
assert . deepEqual ( fi . listeners ( 'end' ) , [ ] ) ;
assert . deepEqual ( fi . listeners ( 'end' ) , [ ] ) ;
assert . deepEqual ( fi . listeners ( 'data' ) , [ ] ) ;
assert . deepEqual ( fi . listeners ( terminal ? 'keypress' : 'data' ) , [ ] ) ;
} ) ;