File: test_io.py

package info (click to toggle)
rpy 1.0.3-15
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 692 kB
  • ctags: 671
  • sloc: ansic: 2,144; python: 1,592; makefile: 129; sh: 69
file content (75 lines) | stat: -rw-r--r-- 1,893 bytes parent folder | download | duplicates (4)
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
from __future__ import nested_scopes

import unittest
import sys
sys.path.insert(1, "..")
from rpy import *

class DummyOut:
    def write(self, *args, **kwds):
        pass

class IOTestCase(unittest.TestCase):

    def setUp(self):
        sys.stdout = sys.stderr = DummyOut()

    def tearDown(self):
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__
        # reset i/o defaults
        set_rpy_output(rpy_io.rpy_output)
        set_rpy_input(rpy_io.rpy_input)

            
    def testIOstdin(self):
        def dummy(prompt, n):
            return prompt+'\n'
        
        set_rpy_input(dummy)
        self.failUnless(r.readline('foo') == 'foo')
        
    def testIOstdout(self):
        out = []
        def dummy(s):
            out.append(s)

        set_rpy_output(dummy)
        r.print_(5)
        self.failUnless(out == ['[1]', ' 5', '\n'])

    def testIOshowfiles(self):
        if sys.platform != 'win32':
            out = []
            def dummy(files, headers, title, delete):
                out.append('foo')
                print "Names curently defined"
                dir()            
                set_rpy_showfiles(dummy)
                r.help()
                self.failUnless(out == ['foo'])

    def testIOstdoutException(self):
        def stdout(s):
            raise Exception

        set_rpy_output(stdout)
        self.assert_(r.print_(5))

    def testIOstdinException(self):
        def stdin(prompt, n):
            raise Exception

        set_rpy_input(stdin)
        self.assert_(r.readline() == '')
        
    def testIOshowfilesException(self):
        if sys.platform != 'win32':
            def showfiles(files, headers, title, delete):
                raise Exception

            set_rpy_showfiles(showfiles)
            self.assert_(r.help() == None)
        
if __name__ == '__main__':
    unittest.main()