From c2e2a560482f231947e8e4cc819129c504c5faed Mon Sep 17 00:00:00 2001 From: Anthex Date: Thu, 25 Apr 2019 11:27:25 +0200 Subject: [PATCH] add unit tests for printing functions --- .coverage | 2 +- .gitignore | 1 + test_structure.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/.coverage b/.coverage index c428a74..d0c3cfb 100644 --- a/.coverage +++ b/.coverage @@ -1 +1 @@ -!coverage.py: This is a private format, don't read it directly!{"lines":{"C:\\Users\\achil\\Desktop\\TD5\\test_structure.py":[1,3,4,5,7,8,9,13,19,26,36,44,49,57,65,71,14,15,16,17,21,22,23,24,27,28,29,30,31,32,33,34,37,38,39,40,41,42,45,46,47,50,51,52,53,54,55,58,59,60,61,62,63,66,67,68,69,72,73,74,75,76,77,78],"C:\\Users\\achil\\Desktop\\TD5\\structure.py":[1,2,4,5,6,18,22,23,28,34,41,44,51,54,61,64,74,75,90,91,99,100,108,109,122,132,139,149,171,191,199,215,224,237,260,235,13,14,15,16,24,25,26,96,97,244,245,246,250,251,252,253,254,255,256,62,267,271,42,35,36,37,38,39,45,46,47,48,49,29,30,52,113,114,115,116,117,118,119,105,106,120,127,128,129,145,146,147,130,137,69,70,55,56,57,58,59,71,72,19,20,220,221,81,82,83,84,85,86,87,88,197,207,208,209,210,211,212,213],"C:\\Users\\achil\\Desktop\\TD5\\main.py":[]}} \ No newline at end of file +!coverage.py: This is a private format, don't read it directly!{"lines":{"C:\\Users\\achil\\Desktop\\TD5\\test_structure.py":[1,2,3,5,6,7,9,10,11,15,21,28,38,46,51,59,67,73,82,91,101,103,107,112,115,119,16,17,18,19,23,24,25,26,29,30,31,32,33,34,35,36,39,40,41,42,43,44,47,48,49,52,53,54,55,56,57,60,61,62,63,64,65,68,69,70,71,74,75,76,77,78,79,80,83,84,86,104,105,108,109,110,87,113,88,117,89,92,93,95,96,97,98],"C:\\Users\\achil\\Desktop\\TD5\\structure.py":[1,2,4,5,6,18,22,23,28,34,41,44,51,54,61,64,74,75,90,91,99,100,108,109,122,132,139,149,171,191,199,215,224,237,260,235,13,14,15,16,24,25,26,96,97,244,245,246,250,251,252,253,254,255,256,62,267,271,42,35,36,37,38,39,45,46,47,48,49,29,30,52,113,114,115,116,117,118,119,105,106,120,127,128,129,145,146,147,130,137,69,70,55,56,57,58,59,71,72,19,20,220,221,81,82,83,84,85,86,87,88,197,207,208,209,210,211,212,213,153,154,156,157,158,161,162,163,164,167,168,159,166,169,175,176,178,179,180,181,183,184,187,188,182,186,189],"C:\\Users\\achil\\Desktop\\TD5\\main.py":[]}} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 82c0eb6..fef6a83 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ __pycache__/ .vscode/ .pytest_cache/ coverage.xml +.coverage diff --git a/test_structure.py b/test_structure.py index bf57f3a..fdf4ce4 100644 --- a/test_structure.py +++ b/test_structure.py @@ -1,4 +1,6 @@ from structure import * +from io import StringIO +import sys Tf = [[newCell(-38,-27,-54,-13,2,2),newCell(-74,-62,-48,-33,2,6),newCell(-13,-28,-12,-40,2,10) ],\ [newCell(-34,-27,-38,-41,6,2), newCell(-64,-48,-72,-35,6,6), newCell(-45,-37,-20,-15,6,10)], \ @@ -75,4 +77,45 @@ def test_getModeLikely(): test_MM.path([5,6,7,6,7,6,7,5,6]) assert test_MM.getMostLikely() == 7 test_MM.path([4,4,4,4,4,4,4]) - assert test_MM.getMostLikely() == 4 \ No newline at end of file + assert test_MM.getMostLikely() == 4 + +def test_printValues(): + test_MM = MarkovModel(Tf) + test_MM.path([1,2,3,2,3,4,3,4]) + + with OutputBuffer() as output: + test_MM.printValues() + assert len(output.out) > 2500 + print(len(output.out)) + +def test_printPercentage(): + test_MM = MarkovModel(Tf) + test_MM.path([1,2,3,2,3,4,3,4]) + + with OutputBuffer() as output: + test_MM.printPercentages() + assert len(output.out) > 2000 + print(len(output.out)) + + +class OutputBuffer(object): + + def __init__(self): + self.stdout = StringIO() + self.stderr = StringIO() + + def __enter__(self): + self.original_stdout, self.original_stderr = sys.stdout, sys.stderr + sys.stdout, sys.stderr = self.stdout, self.stderr + return self + + def __exit__(self, exception_type, exception, traceback): + sys.stdout, sys.stderr = self.original_stdout, self.original_stderr + + @property + def out(self): + return self.stdout.getvalue() + + @property + def err(self): + return self.stderr.getvalue() \ No newline at end of file -- libgit2 0.21.4