perl-module: Make PSPP::Reader::get_next_case() return a list.
[pspp-builds.git] / perl-module / t / Pspp.t
index ff0e4d1e0dac22d277a28f95d606398bbb637485..030a342aecb38048922a815b10e97e8997777c86 100644 (file)
@@ -6,7 +6,7 @@
 
 # change 'tests => 1' to 'tests => last_test_to_print';
 
-use Test::More tests => 31;
+use Test::More tests => 32;
 use Text::Diff;
 use File::Temp qw/ tempfile tempdir /;
 BEGIN { use_ok('PSPP') };
@@ -20,6 +20,13 @@ sub compare
     return ! diff ("$file", \$pattern);
 }
 
+my $pspp_cmd = $ENV{PSPP_TEST_CMD};
+
+if ( ! $pspp_cmd)
+{
+    $pspp_cmd="pspp";
+}
+
 sub run_pspp_syntax
 {
     my $tempdir = shift;
@@ -31,7 +38,7 @@ sub run_pspp_syntax
     print FH "$syntax";
     close (FH);
 
-    system ("cd $tempdir; pspp -o raw-ascii $syntaxfile");
+    system ("cd $tempdir; $pspp_cmd -o raw-ascii $syntaxfile");
 }
 
 sub run_pspp_syntax_cmp
@@ -314,6 +321,12 @@ add value labels
   /string '1111' 'ones' '2222' 'twos' '3333' 'threes'
   /numeric 1 'Unity' 2 'Duality' 3 'Thripality'.
 
+variable attribute
+    variables = numeric
+    attribute=colour[1]('blue') colour[2]('pink') colour[3]('violet')
+    attribute=size('large') nationality('foreign').
+
+
 save outfile='$filename'.
 SYNTAX
 
@@ -374,11 +387,11 @@ RESULT
     print MYFILE "$_ => $vl->{$_}\n" for keys %$vl;
  }
 
- while (my $c = $sf->get_next_case () )
+ while (my @c = $sf->get_next_case () )
  {
     for ($v = 0; $v < $dict->get_var_cnt(); $v++)
     {
-       print MYFILE "val$v: \"@$c[$v]\"\n";
+       print MYFILE "val$v: \"$c[$v]\"\n";
     }
     print MYFILE "\n";
  }
@@ -456,9 +469,9 @@ EOF
 
  my $output = PSPP::Sysfile->new ("$tempdir/out.sav", $dict);
 
- while (my $c = $input->get_next_case () )
+ while (my (@c) = $input->get_next_case () )
  {
-   $output->append_case ($c);
+   $output->append_case (\@c);
  }
 
  $output->close ();
@@ -506,10 +519,10 @@ SYNTAX
 
  my $dict = $sf->get_dict ();
 
- my $c = $sf->get_next_case ();
+ my (@c) = $sf->get_next_case ();
 
  my $var = $dict->get_var (0);
- my $val = @$c[0];
+ my $val = $c[0];
  my $formatted = PSPP::format_value ($val, $var);
  my $str = gmtime ($val - PSPP::PERL_EPOCH);
  print "Formatted string is \"$formatted\"\n";
@@ -544,29 +557,63 @@ SYNTAX
  my $dict = $sf->get_dict ();
 
 
- my $c = $sf->get_next_case ();
+ my (@c) = $sf->get_next_case ();
 
  my $stringvar = $dict->get_var (0);
  my $numericvar = $dict->get_var (2);
- my $val = @$c[0];
+ my $val = $c[0];
 
  ok ( !PSPP::value_is_missing ($val, $stringvar), "Missing Value Negative String");
 
- $val = @$c[2];
+ $val = $c[2];
 
  ok ( !PSPP::value_is_missing ($val, $numericvar), "Missing Value Negative Num");
 
$c = $sf->get_next_case (); 
$c = $sf->get_next_case (); 
@c = $sf->get_next_case (); 
@c = $sf->get_next_case (); 
 
- $val = @$c[0];
+ $val = $c[0];
  ok ( PSPP::value_is_missing ($val, $stringvar), "Missing Value Positive");
 
$c = $sf->get_next_case (); 
- $val = @$c[2];
@c = $sf->get_next_case (); 
+ $val = $c[2];
  ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive SYS");
 
$c = $sf->get_next_case (); 
- $val = @$c[2];
@c = $sf->get_next_case (); 
+ $val = $c[2];
  ok ( PSPP::value_is_missing ($val, $numericvar), "Missing Value Positive Num");
 }
+
+
+#Test reading of custom attributes
+{
+    my $tempdir = tempdir( CLEANUP => 1 );
+
+    generate_sav_file ("$tempdir/in.sav", "$tempdir");
+
+    my $sf = PSPP::Reader->open ("$tempdir/in.sav");
+
+    my $dict = $sf->get_dict ();
+
+    my $var = $dict->get_var_by_name ("numeric");
+
+    my $attr = $var->get_attributes ();
+
+    open (MYFILE, ">$tempdir/out.txt");
+
+    foreach $k (keys %$attr)
+    {
+       my $ll = $attr->{$k};
+       print MYFILE "$k =>";
+       print MYFILE map "$_\n", join ', ', @$ll;
+    }
+
+    close (MYFILE);
+
+    ok (compare ("$tempdir/out.txt", <<EOF), "Custom Attributes");
+colour =>blue, pink, violet
+nationality =>foreign
+size =>large
+EOF
+
+}