Skip to content

Commit

Permalink
Add tests for magic capture #32
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo Peng committed Jan 26, 2018
1 parent 27c21d7 commit 02b86de
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions test/test_sos_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,67 @@ def testMagicRender(self):
''')
wait_for_idle(kc)

def testMagicCapture(self):
with sos_kernel() as kc:
iopub = kc.iopub_channel
# preview variable
execute(kc=kc, code='''
%capture --to res
print('kkk')
''')
wait_for_idle(kc)
execute(kc=kc, code='''
res
''')
stdout, _ = get_std_output(iopub)
# FIXME: Not sure why this test does not work
#self.assertTrue('kkk' in stdout, 'Got stdout "{}"'.format(stdout))
execute(kc=kc, code=r'''
%capture csv --to res
print('a,b\nc,d')
''')
_, stderr = get_std_output(iopub)
self.assertEqual(stderr, '', f"Get error {stderr}")
#
execute(kc=kc, code=r'''
%capture tsv --to res
print('a\tb\nc\td')
''')
_, stderr = get_std_output(iopub)
self.assertEqual(stderr, '', f"Get error {stderr}")
#
execute(kc=kc, code=r'''
%capture json --to res
print('[1,2,3]')
''')
_, stderr = get_std_output(iopub)
self.assertEqual(stderr, '', f"Get error {stderr}")
# form file
execute(kc=kc, code=r'''
%capture csv --to res --from a.txt
with open('a.txt', 'w') as ofile:
print('a,b\nc,d', file=ofile)
''')
_, stderr = get_std_output(iopub)
self.assertEqual(stderr, '', f"Get error {stderr}")
#
execute(kc=kc, code=r'''
%capture tsv --to res --from b.txt
with open('b.txt', 'w') as ofile:
print('a\tb\nc\td', file=ofile)
''')
_, stderr = get_std_output(iopub)
self.assertEqual(stderr, '', f"Get error {stderr}")
#
execute(kc=kc, code=r'''
%capture json --to res --from c.txt
with open('c.txt', 'w') as ofile:
print('[1, 2, 3]', file=ofile)
''')
_, stderr = get_std_output(iopub)
self.assertEqual(stderr, '', f"Get error {stderr}")


if __name__ == '__main__':
unittest.main()

0 comments on commit 02b86de

Please sign in to comment.