# Readline and StringIO

**URL:** <https://www.ruby-forum.com/t/readline-and-stringio/66619>\
**Category:** Ruby\
**Created:** [August 27, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/readline-and-stringio/66619 "2006-08-27T23:50:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [August 27, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/readline-and-stringio/66619/1 "2006-08-27T23:50:45Z")

</div>

It is currently not possible to substitute $stdout or  
$stdin with a StringIO when using the Readline library  
($stderr seems to be fine) due to a T\_FILE check in  
readline.c.

Can someone come up with a reason why this might be  
required before I change it and submit a patch?

While I am at it, I suppose I will try to provide an  
additional method for getting the full line of input.

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [August 28, 2006, 12:07am UTC](https://www.ruby-forum.com/t/readline-and-stringio/66619/2 "2006-08-28T00:07:21Z")

</div>

Eero S. wrote:

> It is currently not possible to substitute $stdout or  
> $stdin with a StringIO when using the Readline library  
> ($stderr seems to be fine) due to a T\_FILE check in  
> readline.c.
> 
> Can someone come up with a reason why this might be  
> required before I change it and submit a patch?
> 
> While I am at it, I suppose I will try to provide an  
> additional method for getting the full line of input.

Um, I don’t understand why you would want to do that. You are talking  
about GNU readline, aren’t you? AFAIK this lib is for interactive  
editing input - something you cannot do with a StringIO anyway. Did I  
miss something?

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [August 28, 2006, 12:14am UTC](https://www.ruby-forum.com/t/readline-and-stringio/66619/3 "2006-08-28T00:14:48Z")

</div>

Robert K. wrote:

> Eero S. wrote:
> 
> > It is currently not possible to substitute $stdout or  
> > $stdin with a StringIO when using the Readline library  
> > ($stderr seems to be fine) due to a T\_FILE check in  
> > readline.c.
> > 
> > Can someone come up with a reason why this might be  
> > required before I change it and submit a patch?
> > 
> > While I am at it, I suppose I will try to provide an  
> > additional method for getting the full line of input.
> 
> Um, I don’t understand why you would want to do that. You are talking  
> about GNU readline, aren’t you? AFAIK this lib is for interactive  
> editing input - something you cannot do with a StringIO anyway. Did I  
> miss something?

Unit-testing a program that is supposed to be  
interactive. StringIO can would be a fairly simple  
way of providing specific input and being able to  
predict the output. This is possible with files as  
well, if a bit less wieldy 🙂

> Kind regards
> 
> robert

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [August 28, 2006, 12:20am UTC](https://www.ruby-forum.com/t/readline-and-stringio/66619/4 "2006-08-28T00:20:33Z")

</div>

Eero S. wrote:

> Robert K. wrote:
> 
> > Eero S. wrote:
> > 
> > > It is currently not possible to substitute $stdout or  
> > > $stdin with a StringIO when using the Readline library  
> > > ($stderr seems to be fine) due to a T\_FILE check in  
> > > readline.c.
> > > 
> > > Can someone come up with a reason why this might be  
> > > required before I change it and submit a patch?
> > > 
> > > While I am at it, I suppose I will try to provide an  
> > > additional method for getting the full line of input.
> > 
> > Um, I don’t understand why you would want to do that. You are talking  
> > about GNU readline, aren’t you? AFAIK this lib is for interactive  
> > editing input - something you cannot do with a StringIO anyway. Did I  
> > miss something?
> 
> Unit-testing a program that is supposed to be  
> interactive. StringIO can would be a fairly simple  
> way of providing specific input and being able to  
> predict the output. This is possible with files as  
> well, if a bit less wieldy 🙂

Come to think of it, might make such input redirection  
possible in irb also (if using --readline).

> > Kind regards
> > 
> > robert

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [August 28, 2006, 11:16am UTC](https://www.ruby-forum.com/t/readline-and-stringio/66619/5 "2006-08-28T11:16:19Z")

</div>

On 28.08.2006 00:20, Eero S. wrote:

> > > >
> > 
> > well, if a bit less wieldy 🙂
> 
> Come to think of it, might make such input redirection  
> possible in irb also (if using --readline).

You can use fork with a temporary file that is used as input. Even more  
appropriate for testing interactive applications might be something like  
expect or the Ruby version of it.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [August 28, 2006, 4:25pm UTC](https://www.ruby-forum.com/t/readline-and-stringio/66619/6 "2006-08-28T16:25:47Z")

</div>

Robert K. wrote:

> On 28.08.2006 00:20, Eero S. wrote:
> 
> > > > >
> > > 
> > > well, if a bit less wieldy 🙂
> > 
> > Come to think of it, might make such input redirection  
> > possible in irb also (if using --readline).
> 
> You can use fork with a temporary file that is used as input. Even more  
> appropriate for testing interactive applications might be something like  
> expect or the Ruby version of it.

Not sure if expect will work on output streams (though  
I will take a look) and yes, I am aware that this can  
be done with files (which is what I am doing currently).

I was merely wondering if there is any reason for the  
T\_FILE check in Readline (as opposed to a more permissive  
one) since that would be the easier solution.

> Kind regards
> 
> robert

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [August 28, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/readline-and-stringio/66619/7 "2006-08-28T16:57:09Z")

</div>

Eero S. wrote:

> I was merely wondering if there is any reason for the  
> T\_FILE check in Readline (as opposed to a more permissive  
> one) since that would be the easier solution.

Change the check and see if there is a reason?

Personally, I’d avoid unit-testing with simulating interactive input.  
Unless you’re unit-testing the readline library / binding. Maybe you’re  
missing an abstraction layer between user input and your program logic?

At least I thought unit testing was supposed to test the core logic  
modules, where you can just feed them with your specific data or  
environment. Testing at the interface level doesn’t seem like  
unit-testing to me, and you probably want tools like expect to get  
decent coverage from that approach.

David V.

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [August 28, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/readline-and-stringio/66619/8 "2006-08-28T17:17:33Z")

</div>

David V. wrote:

> Eero S. wrote:
> 
> > I was merely wondering if there is any reason for the  
> > T\_FILE check in Readline (as opposed to a more permissive  
> > one) since that would be the easier solution.
> 
> Change the check and see if there is a reason?
> 
> Personally, I’d avoid unit-testing with simulating interactive input.  
> Unless you’re unit-testing the readline library / binding. Maybe you’re  
> missing an abstraction layer between user input and your program logic?

I am actually trying to test output which is also not possible.

> At least I thought unit testing was supposed to test the core logic  
> modules, where you can just feed them with your specific data or  
> environment. Testing at the interface level doesn’t seem like  
> unit-testing to me, and you probably want tools like expect to get  
> decent coverage from that approach.

I dunno, I see nothing wrong with testing with correct/incorrect  
IO for those parts which access the streams directly. The alternative  
would be using some type of mocking but, well, that would not work  
either.

> David V.

---

<div class="post-metadata">

**Author:** ![rue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b77776/32.png) [@rue](https://www.ruby-forum.com/u/rue)\
**Post date:** [August 28, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/readline-and-stringio/66619/9 "2006-08-28T17:23:12Z")

</div>

On 8/28/06, David V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> missing an abstraction layer between user input and your program logic?
> 
> At least I thought unit testing was supposed to test the core logic  
> modules, where you can just feed them with your specific data or  
> environment. Testing at the interface level doesn’t seem like  
> unit-testing to me, and you probably want tools like expect to get  
> decent coverage from that approach.
> 
> David V.

I am afraid that this comes from the difficulties to automate testing at  
that level ( and OP is just fighting with one of the many problems to  
solve),  
but certainly interactive input treatement is a very worthy unit to  
test.

Cheers  
Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein
