Search code examples
testingpromptraku

How to test the prompt command?


my $x = prompt "Are you OK? ";
say $x ~~ 'y' ?? "Great!" !! "Bummer";

use Test;

What tests can a put in a t/ file? How can it respond to the prompt?


Solution

  • I don't know how to do it directly(probably by capturing handles), so I leave that to others, but you can put your code in a script file and test it with Test::Run module:

    # script.raku
    
    my $x = prompt "Are you OK? ";
    say $x ~~ 'y' ?? "Great!" !! "Bummer";
    
    # script.t
    
    use Test::Run:auth<zef:CIAvash>:ver<0.2.*> :runs_ok;
    
    runs_ok :args«$*EXECUTABLE script.raku», :in<y>, :out(*.contains: 'Are you OK? Great!');
    runs_ok :args«$*EXECUTABLE script.raku», :in<n>, :out(*.contains: 'Are you OK? Bummer');