-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathparsertest.py
68 lines (58 loc) · 2.19 KB
/
mathparsertest.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
#!/usr/bin/env python
import pytest
import mathparser
import sys
def test_ItShouldReadAFile():
"""Given a file as input it should parse it"""
f = mathparser.readcommands("Test/validTest.file")
assert f is not None
def test_ItShouldParseIncorrectFileAndLogErrors():
"""Given an incorrect file it should parse the file and errors should be logged to stderr"""
from cStringIO import StringIO
placeHolder = sys.stderr
try:
sys.stderr = StringIO()
f = mathparser.readcommands("Test/invalidTest.file")
errorlogs = sys.stderr.getvalue()
assert f is not None
assert errorlogs is not None
finally:
sys.stderr.close()
sys.stderr = placeHolder
def test_ItShouldParseAndReturnMathCommand():
"""Given a correct file with 5 commands a list of 5 MathCommand should be returned"""
f = mathparser.readcommands("Test/validTest.file")
assert len(list(f)) == 5
def test_ItShouldPrintCorrectValue():
""" Given a file as input, the correct value should be output to stdout"""
from cStringIO import StringIO
placeHolder = sys.stderr
try:
sys.stdout = StringIO()
mathparser.main(['mathparser.py','Test/validTest.file'])
result = sys.stdout.getvalue()
finally:
sys.stdout.close()
sys.stdout = placeHolder
assert result == "4\n"
def test_ItShouldNotFailOnEmptyFile():
"""Given an empty file an empty list should be returned"""
f = mathparser.readcommands('Test/emptyTest.file')
assert len(list(f)) == 0
def test_ItShouldNotParseDivisionByZero():
"""Given an division by zero it should print an error"""
from cStringIO import StringIO
placeHolder = sys.stderr
try:
sys.stderr = StringIO()
f = mathparser.readcommands("Test/divisionByZero.file")
errorlogs = sys.stderr.getvalue()
assert f is not None
assert errorlogs is not None
finally:
sys.stderr.close()
sys.stderr = placeHolder
def test_NegativeSquareNumber():
"""Test not implemented yet and not covered in code,
current version returns the original number back for negative squares"""
assert 1 == 0