0

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}]}

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0
    *** 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}

1 Comment

if the return value is 0 then the ICCID is verified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.