File: test.pl

package info (click to toggle)
libterm-twiddle-perl 2.73-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 80 kB
  • ctags: 25
  • sloc: perl: 308; makefile: 2
file content (164 lines) | stat: -rw-r--r-- 3,936 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use Test;
use vars qw( $tests );
BEGIN { $tests = 13; plan tests => $tests };
use Term::Twiddle;
ok(1);

my $sp;
$sp = new Term::Twiddle;

unless( _is_interactive() and
        get_ans("Do you want to run the (brief but interactive) tests?", 'y') ) {
    for (1..$tests-1) { ok(1) }
    exit;
}

## basic spin
printw("going to spin for 1 second or so ==> ");
$sp = new Term::Twiddle;
$sp->start();
system('sleep', '1');
$sp->stop();
ok( get_ans("Did it work?", "y") );

## random spin
printw("going to spin a varying speeds for a few seconds ==> ");
$sp->random;
$sp->start;
system('sleep', '3');
$sp->stop;
ok( get_ans("Did it work?", "y") );

## new thingy
printw("going to show a pair of eyes blinking ==> ");
$sp->random(0);
$sp->rate(0.175);
$sp->thingy( ["00", "--"] );
$sp->start;
system('sleep', '2');
$sp->stop;
ok( get_ans("Did it work?", "y") );

## new thingy
printw("going to show a rolling ball ==> ");
$sp->rate(0.075);
$sp->thingy( [
        '|o_____|', 
        '|_o____|', 
        '|__o___|', 
        '|___o__|', 
        '|____o_|', 
        '|_____o|', 
        '|____o_|',
        '|___o__|',
        '|__o___|',
        '|_o____|',
        '|o_____|',
        ] );
$sp->start;
system('sleep', '2');
$sp->stop;
ok( get_ans("Did it work?", "y") );

## slow constructor
printw("trying a new constructor (spinner should be slow) ==> ");
$sp = new Term::Twiddle({rate => 0.275});
$sp->start;
system('sleep', '3');
$sp->stop;
ok( get_ans("Did it work?", "y") );

## moderate constructor
printw("trying a new constructor (spinner should be moderate) ==> ");
$sp = new Term::Twiddle({rate => 0.075});
$sp->start;
system('sleep', '3');
$sp->stop;
ok( get_ans("Did it work?", "y") );

## fast constructor
printw("trying a new constructor (spinner should be fast) ==> ");
$sp = new Term::Twiddle({rate => 0.015});
$sp->start;
system('sleep', '3');
$sp->stop;
ok( get_ans("Did it work?", "y") );

## random constructor
printw("trying a new constructor (spinner should be pretty random) ==> ");
$sp = new Term::Twiddle({probability => 70, rate => 0.075});
$sp->start;
system('sleep', '3');
$sp->stop;
ok( get_ans("Did it work?", "y") );

## swishing object
printw("trying a new constructor (should be a swishing object) ==>\n");
$sp = new Term::Twiddle({type => 'swish'});
$sp->start;
system('sleep', '4');
$sp->stop;
ok( get_ans("\nDid it work?", "y") );

## bouncy ball
printw("trying a new constructor (should be a bouncing ball) ==>\n");
$sp = new Term::Twiddle({type => 'bounce'});
$sp->start;
system('sleep', '5');
$sp->stop;
ok( get_ans("\nDid it work?", "y") );

printw("trying a new constructor (should be another, shorter bouncing ball) ==>\n");
$sp = new Term::Twiddle();
$sp->type('bounce');
$sp->width($sp->width()/2);
$sp->start;
system('sleep', '5');
$sp->stop;
ok( get_ans("\nDid it work?", "y") );

## out of scope
{
    printw("testing destructor (spinner should stop after 2 sec) ==> ");
    my $tw = new Term::Twiddle({rate => 0.1});
    $tw->start;
    system('sleep', '2');
}
ok( get_ans("Did it work?", "y") );

exit;

## print and wait a sec
sub printw {
    my $msg = shift;
    print STDERR $msg;
    select(undef, undef, undef, 0.5);
}

sub get_ans {
    my $query   = shift;
    my $default = shift || 'y';
    my $ans     = shift || $default;

    print STDERR "$query [$ans]: ";
    chomp( $ans = <STDIN> );
    $ans = ( $ans ? $ans : $default );

    return $ans =~ /^$default/i;
}

# scottw: copied (also without comments) from IO::Prompt::Tiny
# Copied (without comments) from IO::Interactive::Tiny by Daniel Muey,
# based on IO::Interactive by Damian Conway and brian d foy
sub _is_interactive {
  my ($out_handle) = (@_, select);
  return 0 if not -t $out_handle;
  if ( tied(*ARGV) or defined(fileno(ARGV)) ) {
    return -t *STDIN if defined $ARGV && $ARGV eq '-';
    return @ARGV>0 && $ARGV[0] eq '-' && -t *STDIN if eof *ARGV;
    return -t *ARGV;
}
  else {
    return -t *STDIN;
}
}