Add Readline tests

Also note that I fixed some bugs and handled some other edge cases in the main code in the process.

@lmorduch: Please review.