##// END OF EJS Templates
%cpaste enhancement (accept '> > > as prefix), patch + test by Stefan, close #216
Ville M. Vainio -
Show More
@@ -0,0 +1,61 b''
1 """Test cpaste magic"""
2
3 tests = {'pass': ["> > > run()",
4 ">>> > run()",
5 "+++ run()",
6 "++ run()"],
7
8 'fail': ["+ + run()",
9 " ++ run()"]}
10
11 from StringIO import StringIO
12 import sys
13
14 stdin_save = sys.stdin
15
16 # NOTE: no blank lines allowed in function definition
17 def testcase(code,should_fail=False):
18 """Execute code via 'cpaste' and ensure it was executed, unless
19 should_fail is set.
20
21 """
22 _ip.user_ns['code_ran'] = False
23 #
24 src = StringIO()
25 src.write('\n')
26 src.write(code)
27 src.write('\n--\n')
28 src.seek(0)
29 #
30 sys.stdin = src
31
32 try:
33 cpaste
34 except:
35 if not should_fail:
36 raise AssertionError("Failure not expected : '%s'" %
37 code)
38 else:
39 assert code_ran
40 if should_fail:
41 raise AssertionError("Failure expected : '%s'" % code)
42 #
43 finally:
44 sys.stdin = stdin_save
45 #
46
47 def run():
48 """Marker function: sets a flag when executed.
49
50 """
51 _ip.user_ns['code_ran'] = True
52 return 'run' # return string so '+ run()' doesn't result in success
53
54
55 ### Actual testing happens here
56
57 for code in tests['pass']:
58 testcase(code)
59
60 for code in tests['fail']:
61 testcase(code,should_fail=True)
@@ -3144,6 +3144,8 b' Defaulting color scheme to \'NoColor\'"""'
3144 3144 par = args.strip()
3145 3145 sentinel = opts.get('s','--')
3146 3146
3147 strip_from_start = [re.compile(e) for e in
3148 ['^(.?>)+','^In \[\d+\]:','^\++']]
3147 3149 from IPython import iplib
3148 3150 lines = []
3149 3151 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
@@ -3151,7 +3153,11 b' Defaulting color scheme to \'NoColor\'"""'
3151 3153 l = iplib.raw_input_original(':')
3152 3154 if l ==sentinel:
3153 3155 break
3154 lines.append(l.lstrip('>').lstrip('+'))
3156
3157 for pat in strip_from_start:
3158 l = pat.sub('',l)
3159 lines.append(l)
3160
3155 3161 block = "\n".join(lines) + '\n'
3156 3162 #print "block:\n",block
3157 3163 if not par:
General Comments 0
You need to be logged in to leave comments. Login now