]> git.donarmstrong.com Git - deb_pkgs/libstatistics-r-perl.git/blob - t/03-run.t
New upstream version 0.34
[deb_pkgs/libstatistics-r-perl.git] / t / 03-run.t
1 #! perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use File::Copy;
7 use File::Temp;
8 use Statistics::R;
9 use File::Spec::Functions;
10
11 my ($R, $expected, $bin, $version);
12
13 my $file = 'file.ps';
14
15 ok $R = Statistics::R->new();
16
17 ok $bin = $R->bin();
18 ok $bin =~ /\S+/, 'Executable name';
19
20 $expected = '';
21 is $R->run( ), $expected;
22
23 ok $bin = $R->bin();
24 ok $bin =~ /\S+/, 'Executable path';
25
26 ok $version = $R->version();
27 ok $version =~ /^\d+\.\d+\.\d+$/, 'Version';
28
29 diag "R version $version found at $bin\n";
30
31
32 $expected = '';
33 is $R->run( qq`postscript("$file" , horizontal=FALSE , width=500 , height=500 , pointsize=1)`), $expected, 'Basic';
34
35 $expected = '';
36 is $R->run( q`plot(c(1, 5, 10), type = "l");` ), $expected;
37
38 $expected = 
39 'null device 
40           1 ';
41 is $R->run( q`dev.off()` ), $expected; # RT bug #66190
42
43 ok -e $file; # RT bug #70307
44 unlink $file;
45
46 $expected =
47 'loop iteration 1
48 loop iteration 2
49 loop iteration 3';
50 is $R->run( q`for (j in 1:3) { cat("loop iteration "); cat(j); cat("\n") }` ), $expected;
51
52 $expected = 'Some innocuous message on stderr';
53 is $R->run( q`write("Some innocuous message on stderr", stderr())` ), $expected, 'IO';
54
55 $expected = 'Some innocuous message on stdout';
56 is $R->run( q`write("Some innocuous message on stdout", stdout())` ), $expected;
57
58 $expected = '[1] 123';
59 is $R->run( qq`x <- 123 \n print(x)` ), $expected, 'Multi-line commands';
60
61 $expected = '456';
62 my $cmd1 = 'x <- 456 ; write.table(x, file="", row.names=FALSE, col.names=FALSE)';
63 is $R->run( $cmd1 ), $expected; # RT bug #70314
64
65 my $cmd2 = <<EOF;
66 a <- 2
67 b <- 5
68 c <- a * b
69 print('ok')
70 EOF
71 $expected = '[1] "ok"';
72 is $R->run( $cmd2 ), $expected, 'Heredoc commands';
73
74 $expected =
75 '456
76 [1] "ok"';
77 is $R->run( $cmd1, $cmd2 ), $expected, 'Multiple commands';
78
79 $expected =
80 'Some innocuous message on stderr
81 loop iteration: [1] 1
82 loop iteration: [1] 2
83 loop iteration: [1] 3
84 Some innocuous message on stdout
85
86 [1] 123
87 456
88 [1] "ok"';
89 $file = catfile('t', 'data', 'script.R');
90 is $R->run_from_file( $file ), $expected, 'Command from file (relative path)';
91
92 my $absfile = File::Temp->new( UNLINK => 1 )->filename;
93 copy($file, $absfile) or die "Error: Could not copy file $file to $absfile: $!\n";
94 is $R->run_from_file( $absfile ), $expected, 'Commands from file (absolute path)';
95
96 done_testing;