Reputation: 307
I am new to robot framework and trying to implement Luhn Algorithm. I found the code in python and want to change it to Robot framework.
def cardLuhnChecksumIsValid(card_number):
""" checks to make sure that the card passes a luhn mod-10 checksum """
sum = 0
num_digits = len(card_number)
oddeven = num_digits & 1
for count in range(0, num_digits):
digit = int(card_number[count])
if not (( count & 1 ) ^ oddeven ):
digit = digit * 2
if digit > 9:
digit = digit - 9
sum = sum + digit
return ( (sum % 10) == 0 )
I started with robotscript and was stuck at statement if not (( count & 1 ) ^ oddeven ) can someone please help me convert the above code to robot script is there a automated to change python code to robot ?
cardLuhnChecksumIsValid
[Arguments] ${ICCID1}
${num_digits} Get length ${ICCID1}
${odd_even} ${num_digits}
... AND ${1}
FOR ${Count} IN RANGE 0 ${num_digits}
${digit} Convert To Integer ${ICCID[${Count}]}
Upvotes: 0
Views: 343
Reputation: 307
*** Test Cases ***
Check - ICCID
[Setup] setup
${ICCID} Card.get ICCID #get ICCID
${Result_Chksum} cardLuhnChecksumIsValid ${ICCID}
[Teardown] tearDown
*** Keywords ***
setup
No Operation
tearDown
No Operation
cardLuhnChecksumIsValid
[Arguments] ${ICCID_val}
${num_digits} Get length ${ICCID_val}
${oddeven}= Evaluate ( ${1} & ${num_digits} )
FOR ${Count} IN RANGE 0 ${num_digits}
${digit} Convert To Integer ${ICCID_val[${Count}]}
${condition}= Evaluate not (( ${Count} & 1 ) ^ ${oddeven} )
${digit}= Run Keyword If ${condition} Evaluate (${digit}*2)
... ELSE Set Variable ${digit}
${digit}= Run Keyword If (${digit} > 9) Evaluate (${digit} - 9)
... ELSE Set Variable ${digit}
${Sum}= Evaluate (${Sum}+${digit})
END
${result}= Evaluate (${Sum}%10)
[Return] ${result}
Upvotes: 0
Reputation: 7291
Have a look at on the Evaluate keyword in the BuiltIn library. You can use that to rewrite:
if not (( count & 1 ) ^ oddeven ):
digit = digit * 2
to:
${condition}= Evaluate not (( ${count} & 1 ) ^ ${oddeven} )
${digit}= Run Keyword If ${condition} Evaluate ${digit} * 2
You can rewrite the rest of the algorithm using the same approach. From Robot Framework 3.2 release you can also use Inline Python evaluation.
Upvotes: 0