示例#1
0
def testB():
    """Test procedure for Part B"""
    result = a1.get_lhs(
        '{"success":true, "lhs":"2 United States Dollars", "rhs":"1.825936 Euros", "error":""}'
    )
    cornell.assert_equals('2 United States Dollars', result)
    result = a1.get_lhs(
        '{"success":false, "lhs":"", "rhs":"", "error":"Source currency code is invalid."}'
    )
    cornell.assert_equals('', result)

    result = a1.get_rhs(
        '{"success":true, "lhs":"2 United States Dollars", "rhs":"1.825936 Euros", "error":""}'
    )
    cornell.assert_equals('1.825936 Euros', result)
    result = a1.get_rhs(
        '{"success":false, "lhs":"", "rhs":"", "error":"Source currency code is invalid."}'
    )
    cornell.assert_equals('', result)

    result = a1.has_error(
        '{"success":false, "lhs":"", "rhs":"", "error":"Source currency code is invalid."}'
    )
    cornell.assert_equals(True, result)
    result = a1.has_error(
        '{"success":true, "lhs":"2 United States Dollars", "rhs":"1.825936 Euros", "error":""}'
    )
    cornell.assert_equals(False, result)
def testB(s):
    """Test procedure for Part B

     sb1='{ "ok":true, "lhs":"2.5 United States Dollars", "rhs":"64.375 Cuban Pesos", "err":"" }'
     sb2='{ "ok":true, "lhs":"2.5UnitedStatesDollars", "rhs":"64.375CubanPesos", "err":"" }'
     sb3='{ "ok":false, "lhs":"", "rhs":"", "err":"Source currency code is invalid." }'
     sb4='{ "ok":false, "lhs":"", "rhs":"", "err":"Currency amount is invalid." }' """
    a1.get_lhs(s)
    a1.get_rhs(s)
    a1.has_error(s)
示例#3
0
def testB():
    """
    Test functions get_src(), get_dst()
    """

    #testing one occurrence of double quotes
    btest1 = a1.first_inside_quotes('A "BD" D')
    introcs.assert_equals("BD", btest1)

    #testing multiple occurrence of double quotes
    btest2 = a1.first_inside_quotes('A b "cde" fg "hi" jk ')
    introcs.assert_equals("cde", btest2)

    #testing nothing in the double quote
    btest3 = a1.first_inside_quotes('""')
    introcs.assert_equals("", btest3)

    #testing a string with only double quotes
    btest4 = a1.first_inside_quotes('"ab"')
    introcs.assert_equals("ab", btest4)

    #testing a valid input
    btest5 = a1.get_lhs('{ "ok":true, "lhs":"1 Bitcoin", "rhs":"9916.0137 ' +
                        'Euros","err":"" }')
    introcs.assert_equals('1 Bitcoin', btest5)

    #testing an invalid input
    btest7 = a1.get_lhs('{ "ok":false, "lhs":"", "rhs":"", "err":"Source ' +
                        'currency code is invalid." }')
    introcs.assert_equals('', btest7)

    #testing an invalid input
    btest8 = a1.get_rhs('{ "ok":false, "lhs":"", "rhs":"", "err":"Source ' +
                        'currency code is invalid." }')
    introcs.assert_equals('', btest8)

    #testing a valid input
    btest9 = a1.get_rhs('{ "ok":true, "lhs":"1 Bitcoin", "rhs":"9916.0137 ' +
                        'Euros","err":"" }')
    introcs.assert_equals('9916.0137 Euros', btest9)

    #testing an invalid query
    btest10 = a1.has_error('{ "ok":false, "lhs":"", "rhs":"", "err":"" }')
    introcs.assert_equals(True, btest10)

    #testing a valid query
    btest11 = a1.has_error(
        '{ "ok":true, "lhs":"1 Bitcoin", "rhs":"9916.0137 ' +
        'Euros", "err":"" }')
    introcs.assert_equals(False, btest11)

    #testing an invalid currency
    btest11 = a1.has_error('{ "ok":false, "lhs":"", "rhs":"", ' +
                           '"err":"Exchange currency code is invalid." }')
    introcs.assert_equals(True, btest11)
示例#4
0
def testB():
    "test procedure for part B"

    #Test for first_inside_quotes
    cornell.assert_equals('C', a1.first_inside_quotes('A   B   "C"    D'))
    cornell.assert_equals('D   E   F',
                          a1.first_inside_quotes('A    B    C    "D   E   F"'))
    cornell.assert_equals('   ', a1.first_inside_quotes('ABC "   " DEF'))

    #Test for get_lhs
    cornell.assert_equals(
        '2 United States Dollars',
        a1.get_lhs(
            '{"success":true, "lhs":"2 United States Dollars", "rhs":"1.825936 Euros", "error":""}'
        ))
    cornell.assert_equals(
        '2.5 United States Dollars',
        a1.get_lhs(
            '{ "success" : true, "lhs" : "2.5 United States Dollars", "rhs" : "2.0952375 Euros", "error" : "" }'
        ))
    cornell.assert_equals(
        '30000 United States Dollars',
        a1.get_lhs(
            '{ "success" : true, "lhs" : "30000 United States Dollars", "rhs" : "6.5237409 Bitcoins", "error" : "" }'
        ))

    #Test for get_rhs
    cornell.assert_equals(
        '112518 Saudi Riyals',
        a1.get_rhs(
            '{ "success" : true, "lhs" : "30000 United States Dollars", "rhs" : "112518 Saudi Riyals", "error" : "" }'
        ))
    cornell.assert_equals(
        '2384.8473112096 Saudi Riyals',
        a1.get_rhs(
            '{ "success" : true, "lhs" : "30000 Dominican Pesos", "rhs" : "2384.8473112096 Saudi Riyals", "error" : "" }'
        ))
    cornell.assert_equals(
        '4975.8459973372 Hong Kong Dollars',
        a1.get_rhs(
            '{ "success" : true, "lhs" : "30000 Dominican Pesos", "rhs" : "4975.8459973372 Hong Kong Dollars", "error" : "" }'
        ))

    #Test for has_error
    cornell.assert_equals(
        True,
        a1.has_error(
            '{"success":false, "lhs":"", "rhs":"", "error":"Source currency code is invalid."}'
        ))
    cornell.assert_equals(
        False,
        a1.has_error(
            '{"success":true, "lhs":"2 United States Dollars", "rhs":"1.825936 Euros", "error":""}'
        ))
示例#5
0
def testB():
    """Test procedure for functions first_inside_quotes, get_lhs(json), get_rhs(json), and has_error(json)"""
    #Test case for general string with only one set of double quotes
    result9 = a1.first_inside_quotes('A "B C" D')
    cornell.assert_equals = ('B C', result9)
    
    #Test case for nothing inside the double quotes
    result10 = a1.first_inside_quotes('A "" C')
    cornell.assert_equals = ('', result10)
    
    #Test case for two sets of double quotes in the string
    result11 = a1.first_inside_quotes('A "B" "C" D')
    cornell.assert_equals = ('B', result11)
    
    #Test case for whole string inside double quotes
    result34 = a1.first_inside_quotes('"B"')
    cornell.assert_equals = ('B', result34)
    
    #Test case for given a general lhs currency query
    result12 = a1.get_lhs('{"success" : true, "lhs" : "2 United States Dollars", "rhs" : "1.825936 Euros", "error":""}')
    cornell.assert_equals = ('2 United States Dollars', result12)
    
    #Test Case for an invalid lhs currency query
    result13 = a1.get_lhs('{"success" : false, "lhs" : "2 AAA", "rhs" : "3 BBB", "error" : "Currency amount is invalid." }')
    cornell.assert_equals = ('', result13)
    
    #Test case for given a general rhs currency query
    result14 = a1.get_rhs('{"success" : true, "lhs" : "2 United States Dollars", "rhs" : "1.825936 Euros", "error":""}')
    cornell.assert_equals = ('1.825936 Euros', result14)
    
    #Test Case for an invalid rhs currency query
    result15 = a1.get_rhs('{"success" : false, "lhs" : "2 AAA", "rhs" : "3 BBB", "error" : "Currency amount is invalid." }')
    cornell.assert_equals = ('', result15)
    
    #Test case for a valid currency query
    result16 = a1.has_error('{"success" : true, "lhs" : "2 United States Dollars", "rhs" : "1.825936 Euros", "error":""}')
    cornell.assert_equals = (False, result16)
    
    #Test case for an invalid currency query
    result17 = a1.has_error('{"success" : false, "lhs" : "", "rhs" : "", "error":"Source currency code is invalid."}')
    cornell.assert_equals = (True, result17)
示例#6
0
def testB():
    """
    Test procedure for Part B
    """
    #test procedures for a1.first_inside_quotes: the function a1.first_inside_quotes
    #returns the first substring of s between two double quote characters.
    #For example, if s is 'A "B C" D', the function first_inside_quotes returns 'B C'.
    #If s is '"A" "B" C "D"', the function first_inside_quotes returns 'A'.
    #If s is " ", the function first_inside_quotes returns ' '.
    introcs.assert_equals('B C', a1.first_inside_quotes('A "B C" D'))

    #Test multiple ""
    introcs.assert_equals('A', a1.first_inside_quotes('"A" "B" C "D"'))

    #Test empty space
    introcs.assert_equals(' ', a1.first_inside_quotes('" "'))

    #test procedure for a1.get_lhs: a1.get_src returns the string inside double quotes immediately following the keyword "lhs". If JSON is '{ "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }', the function a1.get_lhs returns '2 United States Dollars' (not '"2 United States Dollars"').
    introcs.assert_equals(
        '2 United States Dollars',
        a1.get_lhs(
            '{ "ok": true, "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros", "err" : "" }'
        ))

    #test procedure for a1.get_rhs: a1.get_rhs returns the string inside double quotes immediately following the keyword "rhs". If JSON is '{ "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }', the function returns '2 United States Dollars' (not '"2 United States Dollars"').
    introcs.assert_equals(
        '1.727138 Euros',
        a1.get_rhs(
            '{ "ok": true, "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }'
        ))

    #test procedure for a1.has_error: a1.get_src returns True if the query has an error. Since the query is not valid, the function returns True (it does NOT return "Source currency code is invalid").
    introcs.assert_equals(
        True,
        a1.has_error(
            '{ "ok": true, "lhs" : "", "rhs" : "", "err" : "Source currency code is invalid." }'
        ))

    #test procedure for a1.get_lhs: a1.get_src returns the string inside double quotes immediately following the keyword "lhs". If JSON is '{ "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }', the function returns "2 United States Dollars".
    introcs.assert_equals(
        "2 United States Dollars",
        a1.get_lhs(
            '{ "ok": true, "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros", "err" : "" }'
        ))

    #test procedure for a1.get_lhs: a1.get_src returns the string inside double quotes immediately following the keyword "lhs". If JSON is '{ "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }', the function a1.get_lhs returns "2 United States Dollars".
    introcs.assert_equals(
        "1.727138 Euros",
        a1.get_rhs(
            '{ "ok": true, "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }'
        ))

    #test procedure for a1.has_error: a1.has_error returns False since the query does not have an error and is thus "valid".
    introcs.assert_equals(
        False,
        a1.has_error(
            '{"ok": true, "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros", "err" : "" }'
        ))

    #test procedure for a1.has_error: a1.has_error returns True since the query has an error. The function does NOT return "Source currency code is invalid".
    introcs.assert_equals(
        True,
        a1.has_error(
            '{"ok": true, "lhs" : "", "rhs" : "", "err" : "Source currency code is invalid." }'
        ))

    #test procedure for a1.get_lhs: a1.get_lhs returns the string inside double quotes immediately following the keyword "lhs". If JSON is '{ "ok":true, "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros", "err" : "" }', the function a1.get_lhs returns '2 United States Dollars'.
    introcs.assert_equals(
        '2 United States Dollars',
        a1.get_lhs(
            '{ "ok":true, "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros", "err" : "" }'
        ))

    #test procedure for a1.get_rhs: a1.get_rhs returns the string inside double quotes immediately following the keyword "rhs". If JSON is '{ "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }', the function a1.get_rhs returns '1.727138 Euros'.
    introcs.assert_equals(
        '1.727138 Euros',
        a1.get_rhs(
            '{"ok": true, "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }'
        ))

    #test procedure for a1.has_error: a1.has_error returns True if the query has an error. If JSON is '{ "lhs" : "", "rhs" : "", , "err" : "Source currency code is invalid." }' the function a1.has_error returns True, since the query has an error (it does NOT return "Source currency code is invalid").
    introcs.assert_equals(
        True,
        a1.has_error(
            '{"ok": true, "lhs" : "", "rhs" : "", "err" : "Source currency code is invalid." }'
        ))

    #test procedure for a1.get_lhs: a1.get_lhs returns the string inside double quotes immediately following "lhs". If JSON is '{ "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }' the function a1.get_lhs returns "2 United States Dollars".
    introcs.assert_equals(
        "2 United States Dollars",
        a1.get_lhs(
            '{"ok": true, "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }'
        ))

    #test procedure for a1.get_rhs: a1.get_rhs returns the string inside double quotes immediately following "rhs". If JSON is '{ "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }', the function a1.get_rhs returns "1.727138 Euros".
    introcs.assert_equals(
        "1.727138 Euros",
        a1.get_rhs(
            '{"ok": true, "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros",  "err" : "" }'
        ))

    #test procedure for a1.has_error: a1.has_error returns False if the query is "valid". It does NOT return "valid".
    introcs.assert_equals(
        False,
        a1.has_error(
            '{"ok": true, "lhs" : "2 United States Dollars", "rhs" : "1.727138 Euros", "err" : "" }'
        ))

    #test procedure for a1.has_error: a1.has_error returns True if the query has an error. It does NOT return "Source currency code is invalid".
    introcs.assert_equals(
        True,
        a1.has_error(
            '{ "ok":false, "lhs":"", "rhs":"", "err":"Exchange currency code is invalid." }'
        ))
示例#7
0
def testB():
    """
    Test procedure for Part B
    """
    #testing first_inside_quotes for json
    result = a1.first_inside_quotes(
        '{ "ok":true, "lhs":"2.5 United States Dollars", '
        '"rhs":"64.375 Cuban Pesos", "err":"" }')
    introcs.assert_equals('ok', result)
    #testing first_inside_quotes for no quotes
    result = a1.first_inside_quotes('Quotes "" quotes')
    introcs.assert_equals('', result)
    #testing first_inside_quotes for two quotes
    result = a1.first_inside_quotes('two "of" these "quotes"')
    introcs.assert_equals('of', result)
    #testing first_inside_quotes for one quote in a string
    result = a1.first_inside_quotes('""')
    introcs.assert_equals('', result)
    #testing first_inside_quotes for two quotes
    result = a1.first_inside_quotes(' "" "" ')
    introcs.assert_equals('', result)
    #testing first_inside_quotes for a single quote in a double quote
    result = a1.first_inside_quotes(' " ' ' "')
    introcs.assert_equals('  ', result)
    #testing first_inside_quotes whole quote in quotes
    result = a1.first_inside_quotes('"this entire string"')
    introcs.assert_equals('this entire string', result)
    #testing get get_lhs
    result = a1.get_lhs('{ "ok":true, "lhs":"2.5 United States Dollars", '
                        '"rhs":"64.375 Cuban Pesos", "err":"" }')
    introcs.assert_equals('2.5 United States Dollars', result)
    #testing get_lhs on json with error
    result = a1.get_lhs('{ "ok":false, "lhs":"", "rhs":"", '
                        '"err":"Currency amount is invalid." }')
    introcs.assert_equals('', result)
    #testing get_lhs with longer json
    result = a1.get_lhs(
        '{ "ok":true, "lhs":"2.5 Trinidad and Tobago Dollars", '
        '"rhs":"77.304437834842 Guyanaese Dollars", "err":"" }')
    introcs.assert_equals('2.5 Trinidad and Tobago Dollars', result)
    #testing get_lhs
    result = a1.get_lhs('{ "ok":true, "lhs":"1 Bitcoin", '
                        '"rhs":"9916.0137 Euros", "err":"" }')
    introcs.assert_equals('1 Bitcoin', result)
    #testing get_rhs with regular json
    result = a1.get_rhs('{ "ok":true, "lhs":"1 Bitcoin", '
                        '"rhs":"9916.0137 Euros", "err":"" }')
    introcs.assert_equals('9916.0137 Euros', result)
    #testing get_rhs with json with an error
    result = a1.get_rhs('{ "ok":false, "lhs":"", "rhs":"", '
                        '"err":"Currency amount is invalid." }')
    introcs.assert_equals('', result)
    #testing get_rhs with longer json
    result = a1.get_rhs(
        '{ "ok":true, "lhs":"2.5 Trinidad and Tobago Dollars", '
        '"rhs":"77.304437834842 Guyanaese Dollars", "err":"" }')
    introcs.assert_equals('77.304437834842 Guyanaese Dollars', result)
    #testing has_error with an error
    result = a1.has_error('{ "ok":false, "lhs":"", "rhs":"", '
                          '"err":"Currency amount is invalid." }')
    introcs.assert_equals(True, result)
    #testing has_error without an error
    result = a1.has_error('{ "ok":true, "lhs":"1 Bitcoin", '
                          '"rhs":"9916.0137 Euros", "err":"" }')
    introcs.assert_equals(False, result)