~ipython-dev/ipython/0.10.1

« back to all changes in this revision

Viewing changes to test/test_cpaste.ipy

  • Committer: Fernando Perez
  • Date: 2008-06-02 01:26:30 UTC
  • mfrom: (0.1.130 ipython-local)
  • Revision ID: fernando.perez@berkeley.edu-20080602012630-m14vezrhydzvahf8
Merge in all development done in bzr since February 16 2008.

At that time, a clean bzr branch was started from the SVN tree, but
without SVN history.  That SVN history has now been used as the basis
of this branch, and the development done on the history-less BZR
branch has been added and is the content of this merge.  

This branch will be the new official main line of development in
Launchpad (equivalent to the old SVN trunk).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Test cpaste magic"""
 
2
 
 
3
tests = {'pass': ["> > > run()",
 
4
                  ">>> > run()",
 
5
                  "+++ run()",
 
6
                  "++ run()",
 
7
                  "  >>> run()"],
 
8
 
 
9
         'fail': ["+ + run()",
 
10
                  " ++ run()"]}
 
11
 
 
12
from StringIO import StringIO
 
13
import sys
 
14
 
 
15
stdin_save = sys.stdin
 
16
 
 
17
# NOTE: no blank lines allowed in function definition
 
18
def testcase(code,should_fail=False):
 
19
    """Execute code via 'cpaste' and ensure it was executed, unless
 
20
    should_fail is set.
 
21
 
 
22
    """
 
23
    _ip.user_ns['code_ran'] = False
 
24
    #
 
25
    src = StringIO()
 
26
    src.write('\n')
 
27
    src.write(code)
 
28
    src.write('\n--\n')
 
29
    src.seek(0)
 
30
    #
 
31
    sys.stdin = src
 
32
    
 
33
    try:
 
34
        cpaste
 
35
    except:
 
36
        if not should_fail:
 
37
            raise AssertionError("Failure not expected : '%s'" %
 
38
                                 code)
 
39
    else:
 
40
        assert code_ran
 
41
        if should_fail:
 
42
            raise AssertionError("Failure expected : '%s'" % code)
 
43
    #
 
44
    finally:
 
45
        sys.stdin = stdin_save
 
46
    #
 
47
 
 
48
def run():
 
49
    """Marker function: sets a flag when executed.
 
50
 
 
51
    """
 
52
    _ip.user_ns['code_ran'] = True
 
53
    return 'run' # return string so '+ run()' doesn't result in success
 
54
 
 
55
 
 
56
### Actual testing happens here
 
57
 
 
58
for code in tests['pass']:
 
59
    testcase(code)
 
60
 
 
61
for code in tests['fail']:
 
62
    testcase(code,should_fail=True)