Integrated the perl module into the pspp build system.
[pspp-builds.git] / perl-module / t / Pspp.t
1 # -*-perl-*-
2 # Before `make install' is performed this script should be runnable with
3 # `make test'. After `make install' it should work as `perl PSPP.t'
4
5 #########################
6
7 # change 'tests => 1' to 'tests => last_test_to_print';
8
9 use Test::More tests => 19;
10 use Text::Diff;
11 use File::Temp qw/ tempfile tempdir /;
12 BEGIN { use_ok('PSPP') };
13
14 #########################
15
16 sub run_pspp_syntax
17 {
18     my $tempdir = shift;
19     my $syntax = shift;
20     my $result = shift;
21     my $syntaxfile = "$tempdir/foo.sps";
22
23     open (FH, ">$syntaxfile");
24     print FH "$syntax";
25     close (FH);
26
27     system ("cd $tempdir; pspp -o raw-ascii $syntaxfile");
28
29     my $diff =  diff ("$tempdir/pspp.list", \$result);
30
31     if ( ! ($diff eq ""))
32     {
33         diag ("$diff");
34     }
35
36     return ($diff eq "");
37 }
38
39
40 # Insert your test code below, the Test::More module is use()ed here so read
41 # its man page ( perldoc Test::More ) for help writing this test script.
42
43 {
44   my $d = PSPP::Dict->new();
45   ok (ref $d, "Dictionary Creation");
46
47   $d->set_label ("My Dictionary");
48   $d->set_documents ("These Documents");
49
50   # Tests for variable creation
51
52   my $var0 = PSPP::Var->new ($d, "le");
53   ok (!ref $var0, "Trap illegal variable name");
54
55   $var0 = PSPP::Var->new ($d, "legal");
56   ok (ref $var0, "Accept legal variable name");
57
58   my $var1 = PSPP::Var->new ($d, "legal");
59   ok (!ref $var1, "Trap duplicate variable name");
60
61   $var1 = PSPP::Var->new ($d, "money", 
62                           (fmt=>PSPP::Fmt::DOLLAR, 
63                            width=>4, decimals=>2) );
64   ok (ref $var1, "Accept valid format");
65
66   $d->set_weight ($var1);
67
68
69   # Tests for system file creation
70   # Make sure a system file can be created
71   {
72       my $tempdir = tempdir( CLEANUP => 1 );
73       my $tempfile = "$tempdir/testfile.sav";
74       my $syntaxfile = "$tempdir/syntax.sps";
75       my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
76       ok (ref $sysfile, "Create sysfile object");
77
78       $sysfile->close ();
79       ok (-s "$tempfile", "Write system file");
80   }
81 }
82
83
84 # Make sure we can write cases to a file
85 {
86   my $d = PSPP::Dict->new();
87   PSPP::Var->new ($d, "id",
88                          (
89                           fmt=>PSPP::Fmt::F, 
90                           width=>2, 
91                           decimals=>0
92                           )
93                          );
94
95   PSPP::Var->new ($d, "name",
96                          (
97                           fmt=>PSPP::Fmt::A, 
98                           width=>20, 
99                           )
100                          );
101
102   $d->set_documents ("This should not appear");
103   $d->clear_documents ();
104   $d->add_document ("This is a document line");
105
106   $d->set_label ("This is the file label");
107
108   # Check that we can write system files
109   {
110       my $tempdir = tempdir( CLEANUP => 1 );
111       my $tempfile = "$tempdir/testfile.sav";
112       my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
113
114       my $res = $sysfile->append_case ( [34, "frederick"]);
115       ok ($res, "Append Case");
116
117       $res = $sysfile->append_case ( [34, "frederick", "extra"]);
118       ok (!$res, "Appending Case with too many variables");
119
120       $sysfile->close ();
121       ok (-s  "$tempfile", "existance");
122   }
123
124   # Check that sysfiles are closed properly
125   {
126       my $tempdir = tempdir( CLEANUP => 1 );
127       my $tempfile = "$tempdir/testfile.sav";
128       {
129           my $sysfile = PSPP::Sysfile->new ("$tempfile", $d);
130
131           my $res = $sysfile->append_case ( [21, "wheelbarrow"]);
132           ok ($res, "Append Case 2");
133
134           # Don't close.  We want to test that the destructor  does that 
135           # automatically 
136       }
137       ok (-s "$tempfile", "existance2");
138
139     ok (run_pspp_syntax ($tempdir, <<SYNTAX, <<RESULT), "Check output");
140
141         GET FILE='$tempfile'.
142         DISPLAY DICTIONARY.
143         DISPLAY FILE LABEL.
144         DISPLAY DOCUMENTS.
145         LIST.
146 SYNTAX
147 1.1 DISPLAY.  
148 +--------+-------------------------------------------+--------+
149 |Variable|Description                                |Position|
150 #========#===========================================#========#
151 |id      |Format: F2.0                               |       1|
152 |        |Measure: Scale                             |        |
153 |        |Display Alignment: Right                   |        |
154 |        |Display Width: 8                           |        |
155 +--------+-------------------------------------------+--------+
156 |name    |Format: A20                                |       2|
157 |        |Measure: Nominal                           |        |
158 |        |Display Alignment: Left                    |        |
159 |        |Display Width: 20                          |        |
160 +--------+-------------------------------------------+--------+
161
162 File label:
163 This is the file label
164
165 Documents in the active file:
166
167 This is a document line
168
169 id                 name
170 -- --------------------
171 21 wheelbarrow          
172
173 RESULT
174
175
176   }
177
178   # Now do some tests to make sure all the variable parameters 
179   # can be written properly.
180
181   {
182       my $tempdir = tempdir( CLEANUP => 1 );
183       my $tempfile = "$tempdir/testfile.sav";      
184       my $dict = PSPP::Dict->new();
185       ok (ref $dict, "Dictionary Creation 2");
186
187       my $int = PSPP::Var->new ($dict, "integer", 
188                                 (width=>8, decimals=>0) );
189
190       $int->set_label ("My Integer");
191       
192       $int->add_value_label (99, "Silly");
193       $int->clear_value_labels ();
194       $int->add_value_label (0, "Zero");
195       $int->add_value_label (1, "Unity");
196       $int->add_value_label (2, "Duality");
197
198       my $str = PSPP::Var->new ($dict, "string", 
199                                 (fmt=>PSPP::Fmt::A, width=>8) );
200
201
202       $str->set_label ("My String");
203       ok ($str->add_value_label ("xx", "foo"), "Value label for short string");
204       diag ($PSPP::errstr);
205       $str->add_value_label ("yy", "bar");
206
207       $str->set_missing_values ("this", "that");
208
209       my $longstr = PSPP::Var->new ($dict, "longstring", 
210                                 (fmt=>PSPP::Fmt::A, width=>9) );
211
212
213       $longstr->set_label ("My Long String");
214       my $re = $longstr->add_value_label ("xxx", "xfoo");
215       ok (($re == 0), "Long strings cant have labels");
216
217       ok ($PSPP::errstr eq "Cannot add label to a long string variable", "Error msg");
218
219       $int->set_missing_values (9, 99);
220
221       my $sysfile = PSPP::Sysfile->new ("$tempfile", $dict);
222
223
224       $sysfile->close ();
225
226       ok (run_pspp_syntax ($tempdir, <<SYNTAX, <<RESULT), "Check output 2");
227 GET FILE='$tempfile'.
228 DISPLAY DICTIONARY.
229 SYNTAX
230 1.1 DISPLAY.  
231 +----------+-----------------------------------------+--------+
232 |Variable  |Description                              |Position|
233 #==========#=========================================#========#
234 |integer   |My Integer                               |       1|
235 |          |Format: F8.0                             |        |
236 |          |Measure: Scale                           |        |
237 |          |Display Alignment: Right                 |        |
238 |          |Display Width: 8                         |        |
239 |          |Missing Values: 9; 99                    |        |
240 |          +-----+-----------------------------------+        |
241 |          |    0|Zero                               |        |
242 |          |    1|Unity                              |        |
243 |          |    2|Duality                            |        |
244 +----------+-----+-----------------------------------+--------+
245 |string    |My String                                |       2|
246 |          |Format: A8                               |        |
247 |          |Measure: Nominal                         |        |
248 |          |Display Alignment: Left                  |        |
249 |          |Display Width: 8                         |        |
250 |          |Missing Values: "this    "; "that    "   |        |
251 |          +-----+-----------------------------------+        |
252 |          |   xx|foo                                |        |
253 |          |   yy|bar                                |        |
254 +----------+-----+-----------------------------------+--------+
255 |longstring|My Long String                           |       3|
256 |          |Format: A9                               |        |
257 |          |Measure: Nominal                         |        |
258 |          |Display Alignment: Left                  |        |
259 |          |Display Width: 9                         |        |
260 +----------+-----------------------------------------+--------+
261
262 RESULT
263
264   }
265
266 }