Luhn算法机器人框架

Luhn algorithm Robot Framework

我是机器人框架的新手,正在尝试实施 Luhn 算法。 我在python中找到了代码,想把它改成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 )

我从 robotscript 开始,一直卡在语句 if not (( count & 1 ) ^ oddeven ) 有人可以帮我把上面的代码转换成机器人脚本 是否有自动将 python 代码更改为机器人的代码?

cardLuhnChecksumIsValid
[Arguments]    ${ICCID1}
${num_digits}    Get length    ${ICCID1}
${odd_even}    ${num_digits}
...    AND    
FOR    ${Count}    IN RANGE    0    ${num_digits}
${digit}    Convert To Integer    ${ICCID[${Count}]}

查看 BuiltIn 库中的 Evaluate 关键字。你可以用它来重写:

if not (( count & 1 ) ^ oddeven ):
    digit = digit * 2

至:

${condition}=    Evaluate    not (( ${count} & 1 ) ^ ${oddeven} )
${digit}=    Run Keyword If    ${condition}    Evaluate    ${digit} * 2

您可以使用相同的方法重写算法的其余部分。从 Robot Framework 3.2 版本开始,您还可以使用 Inline Python evaluation.

    *** 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    (  & ${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}