From 58cd71f5c1ff3962827e166e8e0a5aca5a928818 Mon Sep 17 00:00:00 2001 From: Santhosh Edukulla Date: Fri, 1 Nov 2013 16:55:07 +0530 Subject: [PATCH] Added a verifyElementInList utility function The purpose is to verify a given element in list at a given position with few error checks like list type,empty list and position Returns appropriate codes based upon the inputs. Can be used under tests instead of multiple asserts Signed-off-by: Santhosh Edukulla --- tools/marvin/marvin/codes.py | 1 + tools/marvin/marvin/integration/lib/utils.py | 40 ++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/tools/marvin/marvin/codes.py b/tools/marvin/marvin/codes.py index bd01ad3e7da..b6580d0d199 100644 --- a/tools/marvin/marvin/codes.py +++ b/tools/marvin/marvin/codes.py @@ -39,3 +39,4 @@ INVALID_INPUT = "INVALID INPUT" EMPTY_LIST = "EMPTY_LIST" FAIL = 0 PASS = 1 +MATCH_NOT_FOUND = "ELEMENT NOT FOUND IN THE INPUT" diff --git a/tools/marvin/marvin/integration/lib/utils.py b/tools/marvin/marvin/integration/lib/utils.py index b6e38ec9e6f..d53c1ae301d 100644 --- a/tools/marvin/marvin/integration/lib/utils.py +++ b/tools/marvin/marvin/integration/lib/utils.py @@ -353,3 +353,43 @@ def validateList(inp): ret[2] = EMPTY_LIST return ret return [PASS, inp[0], None] + +def verifyElementInList(inp, toverify, pos = 0): + ''' + @name: verifyElementInList + @Description: + 1. A utility function to validate + whether the input passed is a list. + The list is empty or not. + If it is list and not empty, verify + whether a given element is there in that list or not + at a given pos + @Input: + I : Input to be verified whether its a list or not + II : Element to verify whether it exists in the list + III : Position in the list at which the input element to verify + default to 0 + @output: List, containing [ Result,Reason ] + Ist Argument('Result') : FAIL : If it is not a list + If it is list but empty + PASS : If it is list and not empty + and matching element was found + IIrd Argument( 'Reason' ): Reason for failure ( FAIL ), + default to None. + INVALID_INPUT + EMPTY_LIST + MATCH_NOT_FOUND + ''' + if toverify is None or toverify == '' \ + or pos is None or pos < -1 or pos == '': + return [FAIL, INVALID_INPUT] + out = validateList(inp) + if out[0] == FAIL: + return [FAIL, out[2]] + if out[0] == PASS: + if len(inp) > pos and inp[pos] == toverify: + return [PASS, None] + else: + return [FAIL, MATCH_NOT_FOUND] + +