Ejemplo n.º 1
0
def test_syntax_errors():
    count = 0
    for filename in causes:
        cause = causes[filename]
        try:
            try:
                exec("from . import %s" % filename)  # for pytest
            except ImportError:
                exec("import %s" % filename)
        except Exception:
            friendly_traceback.explain(redirect="capture")
        result = friendly_traceback.get_output()

        if "syntax" in filename:
            assert "SyntaxError" in result, (
                "SyntaxError identified incorrectly; %s" % filename)
        elif "indentation" in filename:
            assert "IndentationError" in result, (
                "IndentationError identified incorrectly; %s" % filename)
        else:
            assert "TabError" in result, (
                "TabError identified incorrectly; %s" % filename)
        assert cause in result, "\nExpected: %s\nGot: %s" % (cause, result)
        count += 1
    return count
Ejemplo n.º 2
0
def test_type_error15a():
    try:
        _ = [1, 2](3, 4)
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: 'list' object is not callable" in result
    if friendly_traceback.get_lang() == "en":
        assert "I suspect that you had an object of this type, a `list`," in result
    return result
Ejemplo n.º 3
0
def test_type_error16():
    try:
        raise "exception"
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: exceptions must derive from BaseException" in result
    if friendly_traceback.get_lang() == "en":
        assert "In Python 3, exceptions must be derived from BaseException." in result
    return result
def test_module_not_found_error():
    try:
        import does_not_exist
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "ModuleNotFoundError: No module named 'does_not_exist'" in result
    if friendly_traceback.get_lang() == 'en':
        assert "module that cannot be found is 'does_not_exist'." in result
    return result
Ejemplo n.º 5
0
def test_overflow_error():
    try:
        2. ** 1600
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "OverflowError: (34, 'Result too large')" in result
    if friendly_traceback.get_lang() == 'en':
        assert "OverflowError is raised when the result" in result
    return result
def test_type_error15():
    try:
        _ = (1, 2)(3, 4)
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: 'tuple' object is not callable" in result
    if friendly_traceback.get_lang() == 'en':
        assert "I suspect that you had an object of this type, <a tuple>," in result
    return result
def test_zero_division_error():
    try:
        1 / 0
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "ZeroDivisionError: division by zero" in result
    if friendly_traceback.get_lang() == "en":
        assert "A ZeroDivisionError occurs when" in result
    return result
def test_unknown_error():
    try:
        raise MyException("Some informative message about an unknown exception.")
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "Some informative message" in result
    if friendly_traceback.get_lang() == "en":
        assert "Please report this example" in result
    return result
def test_import_error():
    try:
        from math import Pi
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "ImportError: cannot import name 'Pi'" in result
    if friendly_traceback.get_lang() == 'en':
        assert "The object that could not be imported is 'Pi'." in result
    return result
Ejemplo n.º 10
0
def test_name_error():
    try:
        b = c
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "NameError: name 'c' is not defined" in result
    if friendly_traceback.get_lang() == 'en':
        assert "In your program, the unknown name is 'c'." in result
    return result
def test_type_error11a():
    try:
        a = - [1, 2, 3]
        print(a)
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: bad operand type for unary -: 'list'" in result
    if friendly_traceback.get_lang() == 'en':
        assert "You tried to use the unary operator '-'" in result
    return result
def test_zero_division_error2():
    zero = 0
    try:
        1 % zero
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "ZeroDivisionError: integer division or modulo by zero" in result
    if friendly_traceback.get_lang() == "en":
        assert "A ZeroDivisionError occurs when" in result
    return result
def test_overflow_error():
    try:
        2.0**1600
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert ("OverflowError: (34, 'Result too large')" in result or
            "OverflowError: (34, 'Numerical result out of range')" in result)
    if friendly_traceback.get_lang() == "en":
        assert "OverflowError is raised when the result" in result
    return result
Ejemplo n.º 14
0
def test_type_error11b():
    try:
        a = ~(1, 2, 3)
        print(a)
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: bad operand type for unary ~: 'tuple'" in result
    if friendly_traceback.get_lang() == "en":
        assert "You tried to use the unary operator '~'" in result
    return result
def test_key_error():
    d = {'a': 1, 'b': 2}
    try:
        d['c']
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "KeyError: 'c'" in result
    if friendly_traceback.get_lang() == 'en':
        assert "that cannot be found is 'c'." in result
    return result
Ejemplo n.º 16
0
def test_type_error12():
    a = (1, 2, 3)
    try:
        a[0] = 0
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: 'tuple' object does not support item assignment" in result
    if friendly_traceback.get_lang() == "en":
        assert "In Python, some objects are known as immutable:" in result
    return result
Ejemplo n.º 17
0
def test_type_error8a():
    try:
        a = "a"
        b = 42
        a >>= b
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for >>=:" in result
    if friendly_traceback.get_lang() == "en":
        assert "You tried to perform the bit shifting operation >>" in result
    return result
Ejemplo n.º 18
0
def test_type_error4a():
    try:
        a = 1j
        b = {2, 3}
        b *= a
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for *=:" in result
    if friendly_traceback.get_lang() == "en":
        assert "a `set` and a complex number" in result
    return result
Ejemplo n.º 19
0
def test_type_error3a():
    try:
        a = (1, 2)
        b = [3, 4]
        b -= a
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for -=:" in result
    if friendly_traceback.get_lang() == "en":
        assert "a `list` and a `tuple`" in result
    return result
Ejemplo n.º 20
0
def test_type_error2a():
    try:
        one = 1
        two = "two"
        one += two
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for +=:" in result
    if friendly_traceback.get_lang() == "en":
        assert "an integer (`int`) and a string (`str`)" in result
    return result
Ejemplo n.º 21
0
def test_type_error2():
    try:
        one = 1
        none = None
        result = one + none
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for +:" in result
    if friendly_traceback.get_lang() == "en":
        assert "an integer (`int`) and a variable equal to None (`NoneType`)" in result
    return result
Ejemplo n.º 22
0
def test_type_error5():
    try:
        a = {1: 1, 2: 2}
        b = 3.1416
        result = a / b
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for /:" in result
    if friendly_traceback.get_lang() == "en":
        assert "a dictionary (`dict`) and a number (`float`)" in result
    return result
Ejemplo n.º 23
0
def test_type_error5c():
    try:
        a = {1: 1, 2: 2}
        b = 3.1416
        b //= a
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for //=:" in result
    if friendly_traceback.get_lang() == "en":
        assert "an integer (`int`) and a dictionary (`dict`)"
    return result
Ejemplo n.º 24
0
def test_type_error1b():
    try:
        a_tuple = (1, 2, 3)
        a_list = [1, 2, 3]
        result = a_tuple + a_list
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: can only concatenate" in result
    if friendly_traceback.get_lang() == "en":
        assert "a `tuple` and a `list`" in result
    return result
Ejemplo n.º 25
0
def test_type_error10():
    try:
        a = "a"
        b = 42
        b < a
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: '<' not supported between instances of 'int' and 'str'" in result
    if friendly_traceback.get_lang() == "en":
        assert "You tried to do an order comparison (<)" in result
    return result
Ejemplo n.º 26
0
def test_type_error6():
    try:
        a = "a"
        b = 2
        result = a & b
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for &:" in result
    if friendly_traceback.get_lang() == "en":
        assert "a string (`str`) and an integer (`int`)" in result
    return result
def test_index_error2():
    a = list(range(40))
    b = tuple(range(50))
    try:
        print(a[50], b[0])
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "IndexError: list index out of range" in result
    if friendly_traceback.get_lang() == 'en':
        assert "In this case, the sequence is a list." in result
    return result
Ejemplo n.º 28
0
def test_type_error7a():
    try:
        a = {1: 1, 2: 2}
        b = 3.1416
        a **= b
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for ** or pow():" in result
    if friendly_traceback.get_lang() == "en":
        assert "You tried to exponentiate (raise to a power)" in result
    return result
def test_index_error1():
    a = (1, 2, 3)
    b = [1, 2, 3]
    try:
        print(a[3], b[2])
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "IndexError: tuple index out of range" in result
    if friendly_traceback.get_lang() == 'en':
        assert "In this case, the sequence is a tuple." in result
    return result
Ejemplo n.º 30
0
def test_type_error9a():
    try:
        a = "a"
        b = 2
        a @= b
    except Exception:
        friendly_traceback.explain(redirect="capture")
    result = friendly_traceback.get_output()
    assert "TypeError: unsupported operand type(s) for @=:" in result
    if friendly_traceback.get_lang() == "en":
        assert "You tried to use the operator @" in result
    return result