From ddc4aaadc4ab779854461b256918a36cc41dd889 Mon Sep 17 00:00:00 2001 From: Christian Van Wambeke Date: Sun, 8 Apr 2018 22:35:19 +0200 Subject: [PATCH] add test/test_022_returnCode.py --- src/coloringSat.py | 2 +- src/returnCode.py | 102 +++++++++++++++++++++----------- src/utilsSat.py | 4 +- test/test_022_returnCode.py | 115 ++++++++++++++++++++++++++++++++++++ 4 files changed, 184 insertions(+), 39 deletions(-) create mode 100755 test/test_022_returnCode.py diff --git a/src/coloringSat.py b/src/coloringSat.py index c301455..840d232 100755 --- a/src/coloringSat.py +++ b/src/coloringSat.py @@ -163,7 +163,7 @@ def toColor(msg): else: return replace(msg, _tags) -def cleanColor(msg): +def cleanColors(msg): """clean the message of color tags ' ... """ return replace(msg, _tagsNone) diff --git a/src/returnCode.py b/src/returnCode.py index 516db9e..9b910d3 100644 --- a/src/returnCode.py +++ b/src/returnCode.py @@ -23,27 +23,34 @@ usage: >> import returnCode as RCO """ -import os -import sys - -# OKSYS and KOSYS seems equal on linux or windows -OKSYS = 0 # OK -KOSYS = 1 # KO -OK_STATUS = "OK" -KO_STATUS = "KO" +import pprint as PP ##################################################### class ReturnCode(object): - """ + """\ assume simple return code for methods, with explanation as 'why' - obviously why it is not OK, but also why it is OK (if you want) + obviously why is why it is not OK, + but also why is why it is OK (if you want). + and optionnaly contains a return value as self.getValue() + usage: - >> import returnCode as RCO - >> return RCO.ReturnCode("KO", "there is no problem here") - >> return RCO.ReturnCode("KO", "there is a problem here because etc") - >> return RCO.ReturnCode("TIMEOUT_STATUS", "too long here because etc") - >> return RCO.ReturnCode("NA", "not applicable here because etc") + >> import returnCode as RCO + + >> aValue = doSomethingToReturn() + >> return RCO.ReturnCode("KO", "there is no problem here", aValue) + >> return RCO.ReturnCode("KO", "there is a problem here because etc", None) + >> return RCO.ReturnCode("TIMEOUT_STATUS", "too long here because etc") + >> return RCO.ReturnCode("NA", "not applicable here because etc") + + >> rc = doSomething() + >> print("short returnCode string", str(rc)) + >> print("long returnCode string with value", repr(rc)) + + >> rc1 = RCO.ReturnCode("OK", ...) + >> rc2 = RCO.ReturnCode("KO", ...) + >> rcFinal = rc1 + rc2 + >> print("long returnCode string with value", repr(rcFinal)) # KO! """ OK_STATUS = "OK" @@ -71,32 +78,49 @@ class ReturnCode(object): TIMEOUT_STATUS: TOSYS, } _DEFAULT_WHY = "No given explanation" + _DEFAULT_VALUE = "Not set" - def __init__(self, status=None, why=None): + def __init__(self, status=None, why=None, value=None): + self._why = self._DEFAULT_WHY + self._value = self._DEFAULT_VALUE if status is None: - aStatus = self.UNKNOWN_STATUS - self._why = self._DEFAULT_WHY + self._status = self.UNKNOWN_STATUS else: - self.setStatus(status, why) + self.setStatus(status, why, value) def __repr__(self): - res = "%s: '%s'" % (self._status, self.indent(self._why)) + """complete with value, 'ok, why, value' message""" + res = "%s: '%s' for value: %s" % (self._status, self._why, PP.pformat(self._value)) + return res + + def __str__(self): + """without value, only simple 'ok, why' message""" + res = "%s: '%s'" % (self._status, self._why) return res def indent(self, text, amount=5, ch=' '): - """indent multi lines message""" - padding = amount * ch - res = ''.join(padding + line for line in text.splitlines(True)) - return res[amount:] + """indent multi lines message""" + padding = amount * ch + res = ''.join(padding + line for line in text.splitlines(True)) + return res[amount:] - def __add__(self, value): + def __add__(self, rc2): """allows expression 'returnCode1 + returnCode2 + ...' """ - isOk = self.isOk() and value.isOk() + isOk = self.isOk() and rc2.isOk() + newWhy = self._toList(self.getWhy()) + self._toList(rc2.getWhy()) + newValue = self._toList(self.getValue()) + self._toList(rc2.getValue()) if isOk: - return ReturnCode("OK", "%s\n%s" % (self.getWhy(), value.getWhy()) ) + return ReturnCode("OK", newWhy, newValue ) else: - return ReturnCode("KO", "%s\n%s" % (self.getWhy(), value.getWhy()) ) - + return ReturnCode("KO", newWhy, newValue ) + + def _toList(self, strOrList): + """internal use""" + if type(strOrList) is not list: + return [strOrList] + else: + return strOrList + def toSys(self): try: return self._TOSYS[self._status] @@ -106,26 +130,32 @@ class ReturnCode(object): def getWhy(self): return self._why - def getWhy(self): - return self._why - def setWhy(self, why): self._why = why - def setStatus(self, status, why=None): + def getValue(self): + return self._value + + def setValue(self, value): + self._value = value + + def setStatus(self, status, why=None, value=None): if why is None: aWhy = self._DEFAULT_WHY else: aWhy = why + if status in self._TOSYS.keys(): self._status = status self._why = aWhy else: self._status = self.NA_STATUS self._why = "Error status '%s' for '%s'" % (status, aWhy) + + if value is not None: + self._value = value + else: + self._value = self._DEFAULT_VALUE def isOk(self): return (self._status == self.OK_STATUS) - - - diff --git a/src/utilsSat.py b/src/utilsSat.py index 3301cd3..e81d11a 100644 --- a/src/utilsSat.py +++ b/src/utilsSat.py @@ -32,7 +32,7 @@ import shutil import errno import stat -from srs.coloringSat import cleanColors # as shortcut +from src.coloringSat import cleanColors # as shortcut ############################################################################## # file system utilities @@ -50,7 +50,7 @@ def replace_in_file(filein, strin, strout): with open(filein, "r") as f: contents = f.read() shutil.move(filein, filein + "_old") - with open(filein, "r") as f: + with open(filein, "w") as f: f.write(contents.replace(strin, strout)) ############################################################################## diff --git a/test/test_022_returnCode.py b/test/test_022_returnCode.py new file mode 100755 index 0000000..d16ddd9 --- /dev/null +++ b/test/test_022_returnCode.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- + +# Copyright (C) 2010-2018 CEA/DEN +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +import os +import sys +import unittest +import pprint as PP + +import src.debug as DBG +from src.returnCode import ReturnCode as RC + +verbose = False # True + +class TestCase(unittest.TestCase): + "Test the debug.py""" + + def test_000(self): + # one shot setUp() for this TestCase + if verbose: + DBG.push_debug(True) + DBG.write("assert unittest", [a for a in dir(self) if "assert" in a]) + pass + + def test_010(self): + rc = RC() + rrc = str(rc) + DBG.write("test_010 str", rrc) + self.assertIn("ND:", rrc) + self.assertIn("No given explanation", rrc) + self.assertNotIn("for value", rrc) + rrc = repr(rc) + DBG.write("test_010 repr", rrc) + self.assertIn("ND:", rrc) + self.assertIn("No given explanation", rrc) + self.assertIn("for value", rrc) + + def test_015(self): + rc = RC("OK", "all is good") + rrc = str(rc) + DBG.write("test_015 str", rrc) + self.assertIn("OK:", rrc) + self.assertIn("all is good", rrc) + self.assertNotIn("for value", rrc) + rrc = repr(rc) + DBG.write("test_015 repr", rrc) + self.assertIn("OK:", rrc) + self.assertIn("all is good", rrc) + self.assertIn("Not set", rrc) + aVal = "I am a value result" + rc.setValue(aVal) + self.assertEqual(rc.getValue(), aVal) + rrc = repr(rc) + DBG.write("repr", rrc) + + def test_020(self): + aVal = "I am a value result" + rc1 = RC("OK", "all is good1", aVal + "1") + self.assertTrue(rc1.isOk()) + rc2 = RC("OK", "all is good2", aVal + "2") + self.assertTrue(rc2.isOk()) + rc3 = rc1 + rc2 + self.assertTrue(rc3.isOk()) + rrc = repr(rc3) + DBG.write("test_020 repr", rrc) + self.assertIn("OK:", rrc) + self.assertIn("good1", rrc) + self.assertIn("good2", rrc) + self.assertIn("result1", rrc) + self.assertIn("result2", rrc) + rc4 = rc3 + rc1 + rrc = repr(rc4) + DBG.write("test_020 repr", rrc) + self.assertEqual(len(rc4.getWhy()), 3) + self.assertEqual(len(rc4.getValue()), 3) + + def test_025(self): + rc0 = RC("KO") + aVal = "I am a value result" + rc1 = RC("OK", "all is good1", aVal + "1") + rc1.setStatus("KO") # change status raz why and value + self.assertEqual(repr(rc0), repr(rc1)) + + rc1 = RC("OK", "all is good1", aVal + "1") + rc2 = rc0 + rc1 + rc1 + rc0 + rc1 + DBG.write("test_020 repr", rc2, True) + rrc = repr(rc2) + self.assertIn("KO:", rrc) + + def test_999(self): + # one shot tearDown() for this TestCase + if verbose: + DBG.pop_debug() + return + +if __name__ == '__main__': + unittest.main(exit=False) + pass + -- 2.39.2