Created
April 14, 2016 16:05
-
-
Save Mitko-Kerezov/e55e0231fd376a20b48ebb2e621526dc to your computer and use it in GitHub Desktop.
Unit tests for FMI's Python course's fourth challenge. Original tests from @alexnad can be found at https://gist.github.com/alexnad/b71515e82d175b39b325
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original from @alexnad here https://gist.github.com/alexnad/b71515e82d175b39b325 | |
import unittest | |
from os import path, makedirs | |
import solution | |
import shutil | |
CLASSES_FILE = """ | |
class A: | |
def pleasent_func(self, a, b): | |
return a + b | |
def a_unpleasant_func(self, a, b): | |
for x in range(a): | |
for y in range(b): | |
if x + y > b-a: | |
return x + y | |
class B: | |
def pleasant_func(self, a, b): | |
if a + b > 0: | |
return a + b | |
def b_unpleasant_func(self, a, b): | |
try: | |
result = set() | |
for x in range(a): | |
for y in range(b): | |
if x + y == 0: | |
result.append(x/y) | |
except ZeroDivisionError as e: | |
print(e) | |
class C: | |
pass | |
class D: | |
pass | |
""" | |
FUNCTION_FILE = """ | |
def unpleasant_func(a, b): | |
result = [] | |
for x in range(a): | |
for y in range(b): | |
if x+y > 0: | |
result.append(x+y) | |
def a_very_unpleasant_func(a, b, path_to_file): | |
try: | |
for x in range(a): | |
for y in range(b): | |
if x/y == 10: | |
with open(path_to_file, 'w') as f: | |
f.write(a+b) | |
except ZeroDivisionError as e: | |
print(e) | |
def pleasant_func(a, b, path_to_file): | |
try: | |
d = a/b | |
with open(path_to_file) as f: | |
buf = f.readlines() | |
while d > 0: | |
d -= 1 | |
except ZeroDivisionError as e: | |
print(e) | |
def sum(a, b): | |
while a + b > 0: | |
for x in range(a): | |
for y in range(b): | |
a -= 1 | |
return a + b | |
""" | |
class TestAst(unittest.TestCase): | |
def setUp(self): | |
self.path = "testcode" | |
self.function_dir = path.join(self.path, "functions") | |
self.classes_dir = path.join(self.path, "classes") | |
self.functions_file = path.join(self.function_dir, "functions.py") | |
self.classes_file = path.join(self.classes_dir, "classes.py") | |
if not path.exists(self.path): | |
makedirs(self.path) | |
if not path.exists(self.function_dir): | |
makedirs(self.function_dir) | |
if not path.exists(self.classes_dir): | |
makedirs(self.classes_dir) | |
with open(self.functions_file, 'w') as functions: | |
functions.write(FUNCTION_FILE) | |
with open(self.classes_file, 'w') as functions: | |
functions.write(CLASSES_FILE) | |
def tearDown(self): | |
shutil.rmtree(self.path) | |
def testStats(self): | |
unpleasant = [self.classes_file + '#a_unpleasant_func', | |
self.classes_file + '#b_unpleasant_func', | |
self.functions_file + '#unpleasant_func', | |
self.functions_file + '#a_very_unpleasant_func', | |
self.functions_file + '#sum' | |
] | |
excpected = {'classes': 4, | |
'functions': 8, | |
'unpleasant_functions': unpleasant} | |
result = solution.stats(self.path) | |
self.assertEqual(result['classes'], excpected['classes']) | |
self.assertEqual(result['functions'], excpected['functions']) | |
for function in excpected['unpleasant_functions']: | |
self.assertIn(function, result['unpleasant_functions']) | |
for function in result['unpleasant_functions']: | |
self.assertIn(function, excpected['unpleasant_functions']) | |
with self.assertRaises(NotADirectoryError): | |
solution.stats('wrongPathToDirectory') | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment