/usr/lib/python2.7/dist-packages/rpy2/robjects/tests/testRObject.py is in python-rpy2 2.4.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | import unittest
import rpy2.robjects as robjects
rinterface = robjects.rinterface
import array
import tempfile
class RObjectTestCase(unittest.TestCase):
def testNew(self):
identical = rinterface.baseenv["identical"]
py_a = array.array('i', [1,2,3])
self.assertRaises(ValueError, robjects.RObject, py_a)
ri_v = rinterface.SexpVector(py_a, rinterface.INTSXP)
ro_v = robjects.RObject(ri_v)
self.assertTrue(identical(ro_v, ri_v)[0])
del(ri_v)
self.assertEqual(rinterface.INTSXP, ro_v.typeof)
def testR_repr(self):
obj = robjects.baseenv["pi"]
s = obj.r_repr()
self.assertTrue(s.startswith('3.14'))
def testStr(self):
prt = robjects.baseenv["pi"]
s = prt.__str__()
self.assertTrue(s.startswith('[1] 3.14'))
def testRclass(self):
self.assertEqual("character",
robjects.baseenv["letters"].rclass[0])
self.assertEqual("numeric",
robjects.baseenv["pi"].rclass[0])
self.assertEqual("function",
robjects.globalenv.get("help").rclass[0])
def testRclass_set(self):
x = robjects.r("1:3")
old_class = x.rclass
x.rclass = robjects.StrVector(("Foo", )) + x.rclass
self.assertEqual("Foo",
x.rclass[0])
self.assertEqual(old_class[0], x.rclass[1])
def testDo_slot(self):
self.assertEqual("A1.4, p. 270",
robjects.globalenv.get("BOD").do_slot("reference")[0])
import pickle
class RObjectPicklingTestCase(unittest.TestCase):
def testPickle(self):
tmp_file = tempfile.NamedTemporaryFile()
robj = robjects.baseenv["pi"]
pickle.dump(robj, tmp_file)
tmp_file.flush()
tmp_file.seek(0)
robj_again = pickle.load(tmp_file)
self.assertTrue(robjects.baseenv["identical"](robj,
robj_again)[0])
tmp_file.close()
import rpy2.robjects.methods
class RS4TestCase(unittest.TestCase):
def setUp(self):
robjects.r('setClass("A", representation(a="numeric", b="character"))')
def tearDown(self):
robjects.r('setClass("A")')
def testSlotNames(self):
ainstance = robjects.r('new("A", a=1, b="c")')
self.assertEqual(('a', 'b'), tuple(ainstance.slotnames()))
def testIsClass(self):
ainstance = robjects.r('new("A", a=1, b="c")')
self.assertFalse(ainstance.isclass("B"))
self.assertTrue(ainstance.isclass("A"))
def testValidObject(self):
ainstance = robjects.r('new("A", a=1, b="c")')
self.assertTrue(ainstance.validobject())
#FIXME: test invalid objects ?
def suite():
suite = unittest.TestLoader().loadTestsFromTestCase(RObjectTestCase)
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(RObjectPicklingTestCase))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(RS4TestCase))
return suite
if __name__ == '__main__':
unittest.main()
|