-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
101 lines (69 loc) · 2.6 KB
/
tests.py
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
100
101
#!/usr/local/bin/env python
"""Unit tests for scriptable.
Requires:
- Nose: 'python-nose' in Ubuntu, or 'nose' in PyPI.
- Coverage: 'python-coverage' in Ubuntu, or 'coverage' in PyPI.
Run like this:
nosetests --with-coverage --cover-package=scriptable tests.py
"""
import sys
import StringIO
from unittest import TestCase, main
import scriptable
import test_support
class TestAll(TestCase):
"""Test the scriptable method"""
def setUp(self):
self.real_stdin = sys.stdin
self.real_stdout = sys.stdout
sys.stdin = self.stdin = StringIO.StringIO()
sys.stdout = self.stdout = StringIO.StringIO()
def tearDown(self):
sys.stdin = self.real_stdin
sys.stdout = self.real_stdout
def test_str_arr(self):
"""Tests calling with string array"""
test_str = 'one\ntwo\n'
self.stdin.write(test_str)
self.stdin.seek(0)
scriptable.scriptable('test_support.echo')
self.stdout.seek(0)
self.assertEquals(self.stdout.read(), test_str)
def test_run_via_main(self):
"""Tests running scriptable via 'main' method"""
test_str = 'one\ntwo\n'
self.stdin.write(test_str)
self.stdin.seek(0)
scriptable.main(['-test-', 'test_support.echo'])
self.stdout.seek(0)
self.assertEquals(self.stdout.read(), test_str)
def test_no_args(self):
"""Test a function that takes no arguments"""
scriptable.scriptable('test_support.no_args')
self.stdout.seek(0)
expected_output = '\n'.join(test_support.NO_ARGS_OUTPUT) + '\n'
self.assertEquals(self.stdout.read(), expected_output)
def test_returns_string(self):
"""Test a function that returns a string"""
test_str = 'one\ntwo\n'
self.stdin.write(test_str)
self.stdin.seek(0)
scriptable.scriptable('test_support.first_of_array')
self.stdout.seek(0)
self.assertEquals(self.stdout.read(), test_str.split('\n')[0] + '\n')
def test_returns_single_value(self):
"""Test a function that doesn't return an iterable."""
self.stdin.write('any\nthing\n')
self.stdin.seek(0)
scriptable.scriptable('test_support.return_int')
self.stdout.seek(0)
self.assertEquals(self.stdout.read(), u'42\n')
def test_usage(self):
"""Test the 'main' method output usage information"""
argv = ['-test']
ret = scriptable.main(argv)
self.assertEquals(ret, 1)
self.stdout.seek(0)
self.assertIn('Usage', self.stdout.read())
if __name__ == '__main__':
main()