alexgutteridge-rsruby 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/test/tc_init.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class TestInit < Test::Unit::TestCase
5
+
6
+ def test_init
7
+ assert_nothing_raised(){RSRuby.instance}
8
+ assert_instance_of(RSRuby, RSRuby.instance)
9
+ assert_equal(RSRuby.instance, RSRuby.instance)
10
+ end
11
+ end
data/test/tc_io.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class DummyIO
5
+ def write
6
+ end
7
+ end
8
+
9
+ class TestIO < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @r = RSRuby.instance
13
+ $stdout = $stderr = DummyIO.new
14
+ end
15
+
16
+ def teardown
17
+ $stdout = STDOUT
18
+ $stderr = STDERR
19
+ end
20
+
21
+ def test_io_stdin
22
+ dummy = lambda{|prompt,n| prompt+'\n'}
23
+ @r.set_rsruby_input(dummy)
24
+ assert @r.readline('foo') == 'foo'
25
+ end
26
+
27
+ def test_io_stdout
28
+ out = []
29
+ dummy = lambda{|string| out.push(string)}
30
+ @r.set_rsruby_output(dummy)
31
+ @r.print(5)
32
+ assert out == ['[1]','5','\n']
33
+ end
34
+
35
+ def test_io_showfiles
36
+ out = []
37
+ dummy = lambda{|files,headers,title,delete|
38
+ out.push('foo')
39
+ }
40
+ @r.set_rsruby_showfiles(dummy)
41
+ @r.help()
42
+ assert out == ['foo']
43
+ end
44
+
45
+ def test_io_stdout_exception
46
+ #TODO - I can't understand this test in Rpy
47
+ end
48
+
49
+ def test_io_stdin_exception
50
+ #TODO - I can't understand this test in Rpy
51
+ end
52
+
53
+ def test_io_stderr_exception
54
+ #TODO - I can't understand this test in Rpy
55
+ end
56
+
57
+ end
@@ -0,0 +1,20 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class TestLibrary < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @r = RSRuby.instance
8
+ end
9
+
10
+ def test_library
11
+ #Test success
12
+ assert_nothing_raised(){@r.library("boot")}
13
+ end
14
+
15
+ def test_library_fail
16
+ #Test failure
17
+ assert_raises(RException){@r.library("Missing")}
18
+ end
19
+
20
+ end
data/test/tc_matrix.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class Matrix
5
+ def as_r
6
+ "wibble"
7
+ end
8
+ end
9
+
10
+ class TestMatrix < Test::Unit::TestCase
11
+
12
+ def setup
13
+ @r = RSRuby.instance
14
+ end
15
+
16
+ def test_matrix_no_convert
17
+ r = RSRuby.instance
18
+ r.matrix.autoconvert(RSRuby::NO_CONVERSION)
19
+ m = r.matrix([1,2,3,4], :ncol => 2, :nrow => 2)
20
+ assert r.is_matrix(m)
21
+
22
+ end
23
+ end
data/test/tc_modes.rb ADDED
@@ -0,0 +1,264 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class TestModes < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @r = RSRuby.instance
8
+ RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
9
+ @r.class_table.clear
10
+ @r.proc_table.clear
11
+ end
12
+
13
+ def test_to_ruby_args
14
+ assert_raises(ArgumentError){@r.seq.to_ruby(RSRuby::TOP_CONVERSION+1)}
15
+ assert_raises(ArgumentError){@r.seq.to_ruby(-2)}
16
+ assert_raises(TypeError){@r.seq.to_ruby('foo')}
17
+ end
18
+
19
+ def test_to_ruby
20
+ @r.c.autoconvert(RSRuby::NO_CONVERSION)
21
+ four = @r.c(4)
22
+ assert_equal(four.to_ruby, 4)
23
+ assert_equal(four.to_ruby(RSRuby::PROC_CONVERSION), 4)
24
+ assert_equal(four.to_ruby(RSRuby::BASIC_CONVERSION), 4)
25
+ assert_equal(four.to_ruby(RSRuby::VECTOR_CONVERSION), [4])
26
+ assert(@r["=="].call(four.to_ruby(RSRuby::NO_CONVERSION),four))
27
+ end
28
+
29
+ def test_to_ruby_default_arg
30
+ RSRuby.set_default_mode(RSRuby::NO_CONVERSION)
31
+ sequence = @r.seq(1,3)
32
+ t_test = @r.t_test([1,2,3])
33
+
34
+ assert_equal(sequence.to_ruby.class , RObj)
35
+ assert_equal(sequence.to_ruby.class, sequence.class)
36
+
37
+ RSRuby.set_default_mode(RSRuby::BASIC_CONVERSION)
38
+ assert_equal(sequence.to_ruby, [1,2,3])
39
+
40
+ RSRuby.set_default_mode(RSRuby::VECTOR_CONVERSION)
41
+ assert_equal(sequence.to_ruby, [1,2,3])
42
+
43
+ @r.class_table['htest'] = lambda{5}
44
+ RSRuby.set_default_mode(RSRuby::CLASS_CONVERSION)
45
+ assert_equal(t_test.to_ruby, 5)
46
+
47
+ @r.proc_table[lambda{true}] = lambda{return 6}
48
+ RSRuby.set_default_mode(RSRuby::PROC_CONVERSION)
49
+ assert_equal(t_test.to_ruby, 6)
50
+ end
51
+
52
+ def test_default_modes
53
+ RSRuby.set_default_mode(RSRuby::PROC_CONVERSION)
54
+ assert_equal(RSRuby.get_default_mode, RSRuby::PROC_CONVERSION)
55
+ RSRuby.set_default_mode(RSRuby::CLASS_CONVERSION)
56
+ assert_equal(RSRuby.get_default_mode, RSRuby::CLASS_CONVERSION)
57
+ RSRuby.set_default_mode(RSRuby::BASIC_CONVERSION)
58
+ assert_equal(RSRuby.get_default_mode, RSRuby::BASIC_CONVERSION)
59
+ RSRuby.set_default_mode(RSRuby::VECTOR_CONVERSION)
60
+ assert_equal(RSRuby.get_default_mode, RSRuby::VECTOR_CONVERSION)
61
+ RSRuby.set_default_mode(RSRuby::NO_CONVERSION)
62
+ assert_equal(RSRuby.get_default_mode, RSRuby::NO_CONVERSION)
63
+ RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
64
+ assert_equal(RSRuby.get_default_mode, RSRuby::NO_DEFAULT)
65
+ end
66
+
67
+ def test_bad_modes
68
+ assert_raises(ArgumentError){RSRuby.set_default_mode(-2)}
69
+ assert_raises(ArgumentError){RSRuby.set_default_mode(RSRuby::TOP_CONVERSION+1)}
70
+ end
71
+
72
+ def test_no_default_mode
73
+ @r.t_test.autoconvert(RSRuby::CLASS_CONVERSION)
74
+ @r.array.autoconvert(RSRuby::NO_CONVERSION)
75
+ @r.seq.autoconvert(RSRuby::BASIC_CONVERSION)
76
+
77
+ assert_equal(@r.array(1,3).class, @r.array.class)
78
+
79
+ assert_equal(@r.seq(1,3), [1,2,3])
80
+ @r.class_table['htest'] = lambda{5}
81
+ assert_equal(@r.t_test([1,2,3]), 5)
82
+ end
83
+
84
+ def test_individual_conversions
85
+ @r.c.autoconvert(RSRuby::BASIC_CONVERSION)
86
+ @r.seq.autoconvert(RSRuby::PROC_CONVERSION)
87
+ @r.min.autoconvert(RSRuby::VECTOR_CONVERSION)
88
+
89
+ RSRuby.set_default_mode(RSRuby::NO_CONVERSION)
90
+ assert_equal(@r.c(4).class, RObj)
91
+ assert_equal(@r.seq(1,3).class, RObj)
92
+ assert_equal(@r.min(1,3).class, RObj)
93
+
94
+ RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
95
+ assert_equal(@r.c.autoconvert, RSRuby::BASIC_CONVERSION)
96
+ assert_equal(@r.seq.autoconvert, RSRuby::PROC_CONVERSION)
97
+ assert_equal(@r.min.autoconvert, RSRuby::VECTOR_CONVERSION)
98
+ assert_equal(@r.c(4), 4)
99
+ assert_equal(@r.seq(1,3), [1,2,3])
100
+ assert_equal(@r.min(1,3), [1])
101
+
102
+ RSRuby.set_default_mode(RSRuby::BASIC_CONVERSION)
103
+ assert_equal(@r.c.autoconvert, RSRuby::BASIC_CONVERSION)
104
+ assert_equal(@r.seq.autoconvert, RSRuby::PROC_CONVERSION)
105
+ assert_equal(@r.min.autoconvert, RSRuby::VECTOR_CONVERSION)
106
+ assert_equal(@r.c(4), 4)
107
+ assert_equal(@r.seq(1,3), [1,2,3])
108
+ assert_equal(@r.min(1,3), 1)
109
+
110
+ RSRuby.set_default_mode(RSRuby::VECTOR_CONVERSION)
111
+ assert_equal(@r.c.autoconvert, RSRuby::BASIC_CONVERSION)
112
+ assert_equal(@r.seq.autoconvert, RSRuby::PROC_CONVERSION)
113
+ assert_equal(@r.min.autoconvert, RSRuby::VECTOR_CONVERSION)
114
+ assert_equal(@r.c(4), [4])
115
+ assert_equal(@r.seq(1,3), [1,2,3])
116
+ assert_equal(@r.min(1,3), [1])
117
+
118
+ end
119
+
120
+ def test_vector_conversion
121
+
122
+ RSRuby.set_default_mode(RSRuby::VECTOR_CONVERSION)
123
+
124
+ assert_equal(@r.c(true), [true])
125
+ assert_equal(@r.c(4) , [4])
126
+ assert_equal(@r.c('A') , ['A'])
127
+
128
+ assert_equal(@r.c(1,'A',2), ['1','A','2'])
129
+ assert_equal(@r.c(:a => 1, :b => 'A', :c => 2), {'a' => '1', 'b' => 'A', 'c' => '2'})
130
+ assert_equal(@r.list(:a => 1, :b => 'A', :c => 2),
131
+ {'a' => [1], 'b' => ['A'], 'c' => [2]})
132
+ assert_equal(@r.eval_R("x~y").class, RObj)
133
+ end
134
+
135
+ def test_basic_conversion
136
+
137
+ RSRuby.set_default_mode(RSRuby::BASIC_CONVERSION)
138
+ assert_equal(@r.c(true), true)
139
+ assert_equal(@r.c(4), 4)
140
+ assert_equal(@r.c('A') , 'A')
141
+
142
+ assert_equal(@r.c(1,'A',2), ['1','A','2'])
143
+ assert_equal(@r.c(:a => 1, :b => 'A', :c => 2), {'a' => '1', 'b' => 'A', 'c' => '2'})
144
+ assert_equal(@r.list(:a => 1, :b => 'A', :c => 2),
145
+ {'a' => 1, 'b' => 'A', 'c' => 2})
146
+ assert_equal(@r.eval_R("x~y").class, RObj)
147
+
148
+ end
149
+
150
+ def test_class_table
151
+
152
+ @r.class_table['htest'] = lambda{'htest'}
153
+ @r.class_table['data.frame'] = lambda{|x|
154
+ if @r['[['].call(x,1).length > 2
155
+ return 5
156
+ else
157
+ return 'bar'
158
+ end
159
+ }
160
+ RSRuby.set_default_mode(RSRuby::CLASS_CONVERSION)
161
+ assert_equal(@r.t_test([1,2,3]), 'htest')
162
+ assert_equal(@r.as_data_frame([1,2,3]), 5)
163
+ assert_equal(@r.as_data_frame([1,2]), 'bar')
164
+
165
+ end
166
+
167
+ def test_multiple_class_table
168
+
169
+ RSRuby.set_default_mode(RSRuby::NO_CONVERSION)
170
+ f = @r.class__(@r.c(4),'foo')
171
+ g = @r.class__(@r.c(4), ['bar','foo'])
172
+
173
+ @r.class_table['foo'] = lambda{'foo'}
174
+ @r.class_table['bar'] = lambda{'bar'}
175
+ @r.class_table[['bar','foo']] = lambda{5}
176
+
177
+ RSRuby.set_default_mode(RSRuby::CLASS_CONVERSION)
178
+ assert_equal(f.to_ruby, 'foo')
179
+ assert_equal(g.to_ruby, 5)
180
+
181
+ @r.class_table.delete(['bar','foo'])
182
+ assert_equal(g.to_ruby, 'bar')
183
+
184
+ @r.class_table.delete('bar')
185
+ assert_equal(g.to_ruby, 'foo')
186
+
187
+ end
188
+
189
+ def test_proc_table
190
+
191
+ t = lambda{|x|
192
+ e = @r.attr(x,'names')
193
+ return false if e.nil?
194
+ if e == 'alternative' or e.include?('alternative')
195
+ return true
196
+ else
197
+ return false
198
+ end
199
+ }
200
+ f = lambda{|x| @r['$'].call(x,'alternative')}
201
+
202
+ @r.proc_table[t] = f
203
+ RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
204
+ @r.t_test.autoconvert(RSRuby::PROC_CONVERSION)
205
+ assert_equal(@r.t_test([1,2,3]), 'two.sided')
206
+
207
+ end
208
+
209
+ def test_proc_convert
210
+
211
+ r = RSRuby.instance
212
+
213
+ check_str = lambda{|x| RSRuby.instance.is_character(x)}
214
+ f = lambda{|x|
215
+ x = x.to_ruby(RSRuby::BASIC_CONVERSION)
216
+ return "Cannot return 'foo'" if x == 'foo'
217
+ return x
218
+ }
219
+
220
+ r.proc_table[check_str] = f
221
+
222
+ RSRuby.set_default_mode(RSRuby::PROC_CONVERSION)
223
+
224
+ assert_equal('bar',r.c('bar'))
225
+ assert_equal("Cannot return 'foo'",r.c('foo'))
226
+ assert_equal(['bar','foo'],r.c('bar','foo'))
227
+
228
+ end
229
+
230
+ def test_restore_mode_after_exception_in_proc
231
+
232
+ r = RSRuby.instance
233
+
234
+ check_str = lambda{|x| RSRuby.instance.is_character(x)}
235
+ f = lambda{|x|
236
+ x.reverse
237
+ }
238
+
239
+ r.proc_table[check_str] = f
240
+
241
+ RSRuby.set_default_mode(RSRuby::PROC_CONVERSION)
242
+
243
+ assert_equal(6,r.sum(1,2,3))
244
+ assert_equal(RSRuby::PROC_CONVERSION,RSRuby.get_default_mode)
245
+ assert_raise(NoMethodError){r.paste("foo","bar")}
246
+ assert_raise(NoMethodError){r.paste("foo","bar")}
247
+ assert_equal(RSRuby::PROC_CONVERSION,RSRuby.get_default_mode)
248
+
249
+ end
250
+
251
+ def test_restore_mode_after_exception_in_class
252
+
253
+ r = RSRuby.instance
254
+ r.class_table['htest'] = lambda{|x| x.foo}
255
+
256
+ RSRuby.set_default_mode(RSRuby::CLASS_CONVERSION)
257
+ assert_equal(RSRuby::CLASS_CONVERSION,RSRuby.get_default_mode)
258
+ assert_raise(NoMethodError){r.t_test([1,2,3])}
259
+ assert_raise(NoMethodError){r.t_test([1,2,3])}
260
+ assert_equal(RSRuby::CLASS_CONVERSION,RSRuby.get_default_mode)
261
+
262
+ end
263
+
264
+ end
data/test/tc_robj.rb ADDED
@@ -0,0 +1,84 @@
1
+ require 'test/unit'
2
+ require 'rsruby'
3
+
4
+ class TestRObj < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @r = RSRuby.instance
8
+ RSRuby.set_default_mode(RSRuby::NO_DEFAULT)
9
+ @r.class_table.clear
10
+ @r.proc_table.clear
11
+ end
12
+
13
+ def test_type
14
+ assert_equal(@r.array.class, RObj)
15
+ end
16
+
17
+ def test_call
18
+ #TODO
19
+ end
20
+
21
+ def test_keyword_parameters
22
+ #TODO - In RPy keyword parameters are converted like the method calls
23
+ #In ruby this isn't such a problem because you can use quoted strings
24
+ #but we will implement it anyway.
25
+ @r.list.autoconvert(RSRuby::BASIC_CONVERSION)
26
+ d = @r.list(:foo => 'foo', :bar_foo => 'bar.foo', :print_ => 'print', :as_data_frame => 'as.data.frame')
27
+ d.each do |k,v|
28
+ assert_equal(k, d[k])
29
+ end
30
+ end
31
+
32
+ def test_bad_keyword_parameters
33
+ #TODO?
34
+ #assert_raises(ArgumentError){@r.list(:none => 1)}
35
+ end
36
+
37
+ def test_name_conversions
38
+ assert_equal(@r.array, @r['array'])
39
+ assert_equal(@r.print_,@r['print'])
40
+ assert_equal(@r.as_data_frame, @r['as.data.frame'])
41
+ assert_equal(@r.attr__, @r['attr<-'])
42
+ end
43
+
44
+ def test_not_found
45
+ assert_raises(RException){@r.foo}
46
+ end
47
+
48
+ def test_name_length_one
49
+ assert_nothing_raised{@r.T}
50
+ end
51
+
52
+ def test_autoconvert
53
+ @r.seq.autoconvert(RSRuby::BASIC_CONVERSION)
54
+ assert_equal(@r.seq(10), (1..10).to_a)
55
+ @r.seq.autoconvert(RSRuby::NO_CONVERSION)
56
+ assert_equal(@r.seq(10).class, RObj)
57
+ end
58
+
59
+ def test_bad_autoconvert
60
+ assert_raises(ArgumentError){@r.seq.autoconvert(RSRuby::TOP_CONVERSION+1)}
61
+ end
62
+
63
+ def test_get_autoconvert
64
+ @r.seq.autoconvert(RSRuby::BASIC_CONVERSION)
65
+ mode = @r.seq.autoconvert
66
+ assert_equal(mode, RSRuby::BASIC_CONVERSION)
67
+ end
68
+
69
+ def test_r_gc
70
+ #TODO - How can this work?
71
+ @r.seq.autoconvert(RSRuby::NO_CONVERSION)
72
+ arr = @r.seq(100000)
73
+ @r.gc
74
+ assert(@r['['].call(arr,10))
75
+ end
76
+
77
+ def test_lcall
78
+ RSRuby.set_default_mode(RSRuby::NO_CONVERSION)
79
+ arr = @r.c.lcall([['',0],['a',1],['b',2],['c',3]])
80
+ RSRuby.set_default_mode(RSRuby::BASIC_CONVERSION)
81
+ assert_equal(@r.names(arr), ['','a','b','c'])
82
+ end
83
+
84
+ end