Debian Codegraph @

examples/test_script

raw
1 #! /usr/bin/perl
2 #########################################################################
3 # This Perl script is Copyright (c) 2002, Peter J Billam #
4 # c/o P J B Computing, www.pjb.com.au #
5 # #
6 # This script is free software; you can redistribute it and/or #
7 # modify it under the same terms as Perl itself. #
8 #########################################################################
9 use utf8;
10 use open ':locale'; # when was the open pragma introduced ?
11 #my $EncodingString = q{};
12 #if (($ENV{LANG} =~ /utf-?8/i) || ($ENV{LC_TYPE} =~ /utf-?8/i)) {
13 # $EncodingString = ':encoding(utf8)';
14 # binmode STDERR, $EncodingString;
15 # binmode STDOUT, $EncodingString;
16 #}
17 if (($ENV{LANG} =~ /utf-?8/i) || ($ENV{LC_TYPE} =~ /utf-?8/i)) {
18 $Brillouin = "Léon Brillouin";
19 $Delbrueck = "Max Delbrück";
20 $Descartes = "René Descartes";
21 $Goedel = "Kurt Gödel";
22 $Levy = "Paul Lévy";
23 $Poincare = "Henri Poincaré";
24 $Schroedinger = "Erwin Schrödinger";
25 } else {
26 $Brillouin = "L\x{e9}on Brillouin";
27 $Delbrueck = "Max Delbr\x{fc}ck";
28 $Descartes = "Ren\x{e9} Descartes";
29 $Goedel = "Kurt G\x{f6}del";
30 $Levy = "Paul L\x{e9}vy";
31 $Poincare = "Henri Poincar\x{e9}";
32 $Schroedinger = "Erwin Schr\x{f6}dinger";
33 }
34 eval "require './lib/Term/Clui.pm'";
35 if (!$@) {
36 warn "using Term::Clui\n";
37 } else {
38 eval "require '../Clui.pm'";
39 if (!$@) {
40 warn "using ../Clui.pm\n";
41 } else {
42 eval "require 'Clui.pm'";
43 if (!$@) {
44 warn "using ./Clui.pm\n";
45 } else {
46 die "can't find Clui.pm in this dir, parent dir, or INC path\n";
47 }
48 }
49 }
50 import Term::Clui;
51
52 eval "require './lib/Term/Clui/FileSelect.pm'";
53 if ($@) { eval "require '../Clui/FileSelect.pm'"; }
54 if ($@) { eval "require 'Clui/FileSelect.pm'"; }
55 if ($@) {
56 die "can't find FileSelect.pm in this dir, parent dir, or INC path\n";
57 }
58 import Term::Clui::FileSelect;
59 my $colour = "";
60 my $paint = "";
61 my $name = "";
62
63 while (1) {
64 my $task = &choose('Test which Clui.pm subroutine ?',
65 'ask','choose','confirm','edit','view','select_file',
66 );
67 exit unless $task;
68 eval "&test_$task()"; if ($@) { print STDERR "$@\n"; }
69 }
70
71 sub test_choose {
72 my @colours = ('Red','Orange','Black','Grey','Blue');
73 my @paints = (
74 'Bizzare extremely long name that certainly will never occur on any real artist pallette',
75 'Alizarin Crimson', 'Burnt Sienna', 'Cadmium Yellow', 'Cobalt Blue',
76 'Flake White', 'Indian Red', 'Indian Yellow', 'Ivory Black', 'Lemon Yellow',
77 'Naples Yellow', 'Prussian Blue', 'Raw Sienna', 'Raw Umber', 'Red Ochre',
78 'Rose Madder', 'Ultramarine Blue', 'Vandyke Brown', 'Viridian Green',
79 'Yellow Ochre',
80 );
81 my @scientists = (
82 'Luis Alvarez', 'Alain Aspect', 'Michael Barnsley', 'Johann Bernouilli',
83 'Nicolas Bernouilli', 'Friedrich Wilhelm Bessel', 'John Bell',
84 'Antoine Becquerel', 'Hans Bethe', 'David Bohm', 'Niels Bohr',
85 'Ludwig Boltzmann', 'Hermann Bondi', 'George Boole', 'Max Born',
86 'Satyendra Bose', 'Robert Boyle', $Brillouin, 'Eugenio Calabi',
87 'Georg Cantor', 'James Chadwick', 'Gregory Chaitin',
88 'Subrahmanyan Chandrasekar', 'Geoffrey Chew', 'Alonzo Church',
89 'John Horton Conway', 'Francis Crick', 'Marie Curie', 'Charles Darwin',
90 'Humphrey Davy', 'Richard Dawkins', 'Louis de Broglie', $Delbrueck,
91 $Descartes, 'Willem de Sitter', 'Bruce DeWitt', 'Paul Dirac',
92 'Freeman Dyson', 'Arthur Stanley Eddington', 'Albert Einstein',
93 'Leonhard Euler', 'Hugh Everett', 'Michael Faraday', 'Pierre Fatou',
94 'Mitchell Feigenbaum', 'Pierre de Fermat', 'Enrico Fermi',
95 'Richard Feynman', 'Joseph Fraunhofer', 'Galileo Galilei',
96 'Evariste Galois', 'George Gamov', 'Carl Friedrich Gauss',
97 'Murray Gell-Mann', $Goedel, 'Alan Guth', 'Stephen Hawking',
98 'Felix Hausdorff', 'Werner Heisenberg', 'Charles Hermite',
99 'Peter Higgs', 'David Hilbert', 'Fred Hoyle', 'Edwin Hubble',
100 'Christian Huygens', 'David Hilbert', 'Edwin Hubble', 'Pascual Jordan',
101 'Gaston Julia', 'Marc Kac', 'Theodor Kaluza', 'Stuart Kauffman',
102 'William Lord Kelvin', 'Gustav Robert Kirchhoff', 'Oskar Klein',
103 'Helge von Kock', 'Willis Lamb', 'Lev Davidovich Landau', 'Paul Langevin',
104 'Pierre Simon de Laplace', 'Gottfried Wilhelm Leibnitz', $Levy,
105 'Hendrik Lorentz', 'James Clark Maxwell', 'Marston Morse',
106 'Benoit Mandelbrot', 'Gregor Mendel', 'Dmitri Mendeleev', 'Robert Millikan',
107 'Hermann Minkowski', 'John von Neumann', 'Isaac Newton', 'Emmy Noether',
108 'Hans Christian Oersted', 'Lars Onsager', 'Robert Oppenheimer',
109 'Abraham Pais', 'Heinz Pagels', 'Vilfredo Pareto', 'Louis Pasteur',
110 'Wolfgang Pauli', 'Linus Pauling', 'Guiseppe Peano', 'Rudolf Peierls',
111 'Roger Penrose', 'Arno Penzias', 'Jean Perrin', 'Max Planck',
112 'Boris Podolsky', $Poincare, 'Isidor Rabi', 'Srinivasa Ramanujan',
113 'Lord Rayleigh', 'Lewis Fry Richardson', 'B. Riemann', 'Nathan Rosen',
114 'Ernest Rutherford', 'Abdus Salam', $Schroedinger,
115 'Karl Schwarzschild', 'Julian Schwinger', 'Claude Shannon',
116 'Waclaw Sierpinski', 'Leo Szilard', 'Kip Thorne', 'Alan Turning',
117 'Sin-itro Tomonaga', 'Stanislaw Ulam', 'James Watson', 'Karl Weierstrauss',
118 'Hermann Weyl', 'Steven Weinberg', 'John Wheeler', 'Charles Weiner',
119 'Norbert Wiener', 'Eugene Wigner', 'Robert Wilson', 'Edward Witten',
120 'Shing-Tung Yau', 'Chen-Ning Yang', 'Hideki Yukawa', 'George Kingsley Zipf',
121 );
122
123 my $multi = &choose('Mode ?', 'Single-choice', 'Multi-choice');
124 return unless $multi;
125 if ($multi eq 'Single-choice') {
126 $paint = &choose("Your favourite paint ?\n".help_text(), @paints);
127 my $scientist = &choose("Your favourite scientist ?", @scientists);
128 $colour = &choose(<<'EOT', @colours);
129 Your favourite colour ?
130
131 This tests how the 'choose' subroutine handles multi-line
132 questions. After you choose, all but the first line should disappear,
133 leaving the question and answer on the screen as a record of the dialogue.
134 The other lines should only get displayed if there is room.
135 EOT
136 &inform("paint=$paint, scientist=$scientist, colour=$colour\n");
137 } else {
138 my @fav_paints
139 = &choose("Your favourite paints ?\n".help_text('multi'),@paints);
140 my @fav_scientists = &choose("Your favourite scientists ?",@scientists);
141 warn "paints = ".join(', ',@fav_paints) .
142 "\nscientists = ".join(', ',@fav_scientists)."\n";
143 }
144 return;
145 }
146
147 sub test_confirm {
148 &confirm(<<EOT) || return;
149 OK to proceed with the test ?
150
151 This step checks the 'confirm' subroutine and whether it handles
152 a multiline question OK. After you choose Yes or No all but the
153 first line should disappear, leaving the question and answer on
154 the screen as a record of the dialogue.
155 EOT
156
157 &confirm('Did the text vanish except for the 1st line ?');
158 }
159
160 sub test_ask {
161 my $question = <<EOT;
162 Enter a string :
163
164 The point of this test is to check out the behaviour of &ask
165 with multi-line questions; subsequent lines after the initial
166 question should be formatted within the window width ...
167 EOT
168 my $string = &ask($question.help_text('ask'));
169
170 my @colours = ('Red','Orange','Black','Grey','Blue');
171 $colour = &choose('Your favourite colour ?', @colours);
172 return unless $colour;
173 %names = (
174 Red=>'Fred', Orange=>'Solange', Black=>'Jack', Grey=>'May', Blue=>'Sue',
175 );
176 $name = &ask("Choose a name which rhymes with $colour :", $names{$colour});
177 warn "string=$string, name=$name\n";
178 my $passwd = ask_password("Enter some password:");
179 warn "that password was ".length($passwd)." chars long\n";
180 my $f = ask_filename("filename ?\n\ntry out the tab-filename-completion");
181 warn "that filename was $f\n";
182 }
183
184 sub test_edit {
185 $text = &edit('Your limerick', <<EOT);
186 There was a brave soul called $name,
187 Whose favourite colour was $colour;
188 But some $paint
189 ...
190 And that was the end of $name.
191 EOT
192 }
193
194 sub test_view {
195 &view('Your limerick:', $text||'try testing "ask" and "edit" first :-)');
196 }
197
198 sub test_select_file {
199 my @bool_opts = ('-Chdir','-Create','-ShowAll','-DisableShowAll',
200 '-SelDir','-TextFile','-Readable','-Writeable','-Executable','-Owned',
201 '-Directory');
202 my @text_opts = ('-FPat','-File','-Path','-Title','-TopDir');
203 my $multiple = &choose('Select','Single file','Multiple files');
204 if ($multiple eq 'Multiple files') { shift @bool_opts; shift @bool_opts; }
205 my %opts;
206 foreach (@bool_opts) {
207 $opts{$_} = &choose("option $_ ?",'default','0','1');
208 return unless defined $opts{$_};
209 if ($opts{$_} eq 'default') { delete $opts{$_}; }
210 }
211 foreach (@text_opts) {
212 $opts{$_} = &ask("option $_ ?", $opts{$_});
213 if (! $opts{$_}) { delete $opts{$_}; }
214 }
215 if ($multiple eq 'Multiple files') {
216 my @files = &select_file(-Chdir=>0, %opts);
217 print STDERR "You selected @files\n";
218 } else {
219 print STDERR "You selected " .&select_file(%opts), "\n";
220 }
221 }