-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
58 lines (45 loc) · 1.66 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
import unittest
import os
import io
import sys
import contextlib
from classes.parser import DirectoryScan
@contextlib.contextmanager
def captured_output():
new_out, new_err = io.StringIO(), io.StringIO()
old_out, old_err = sys.stdout, sys.stderr
try:
sys.stdout, sys.stderr = new_out, new_err
yield sys.stdout, sys.stderr
finally:
sys.stdout, sys.stderr = old_out, old_err
def validate_args(args, parser):
with captured_output() as (out, err):
try:
parser.parse_args(args)
return True
except SystemExit as _:
return False
class Tests(unittest.TestCase):
def setUp(self) -> None:
self.dirscan = DirectoryScan()
def test_initializes_without_args(self):
args = self.dirscan.parser.parse_args()
self.assertEqual(args.directory, os.getcwd())
self.assertFalse(args.all)
self.assertEqual(args.size, "block")
def test_initializes_with_args(self):
args = self.dirscan.parser.parse_args(["~/", "-a"])
self.assertEqual(args.directory, "~/")
self.assertTrue(args.all)
def test_initializes_with_size_scale(self):
args = self.dirscan.parser.parse_args(["-s", "K"])
self.assertEqual(args.size, "K")
def test_fails_with_bad_args(self):
self.assertFalse(validate_args(["-z"], self.dirscan.parser))
self.assertTrue(validate_args(["-a"], self.dirscan.parser))
def test_size_scale_failure(self):
self.assertFalse(validate_args(["-s", "B"], self.dirscan.parser))
self.assertTrue(validate_args(["-s", "K"], self.dirscan.parser))
if __name__ == "__main__":
unittest.main()