コード例 #1
0
def test_check_function_def_basic(stu, passes):
    s = setup_state(stu, 'def test(x): print(x)')
    with helper.verify_sct(passes):
        s.check_function_def('test').multi(
            check_args(0).has_equal_part('name', msg='wrong'),
            check_body().set_context(1).check_function('print').check_args(
                0).has_equal_value())
コード例 #2
0
def test_two_for_loops(stu, passes):
    s = setup_state(stu,
                    'for i in range(1):\n  pass\nfor j in range(4): print(j)')
    with helper.verify_sct(passes):
        s.check_for_loop(index=1).multi(
            check_iter().has_equal_value(),
            check_body().set_context(2).has_equal_output())
コード例 #3
0
def test_check_function_basic(stu, passes, a_arg, b_arg):
    s = setup_state(stu, 'my_fun(1, 2)', pec='def my_fun(a, b): pass')
    with helper.verify_sct(passes):
        s.check_function('my_fun').multi(
            check_args(a_arg).has_equal_value(),
            check_args(b_arg).has_equal_value()
        )
コード例 #4
0
def test_check_class_def_pass(stu, passes):
    sol = 'class A(str):\n  def __init__(self): pass'
    s = setup_state(stu, sol)
    with helper.verify_sct(passes):
        s.check_class_def('A').multi(
            check_bases(0).has_equal_ast(),
            check_body().check_function_def('__init__').check_body().has_equal_ast()  
        )
コード例 #5
0
def test_check_function_def_args(stu, passes):
    s = setup_state(stu, "def f(a, b = 3): pass")
    with helper.verify_sct(passes):
        s.check_function_def('f').multi(
            check_args(0).has_equal_part('name', msg='wrong').has_equal_part(
                'is_default', msg='wrong'),
            check_args(1).has_equal_part('name', msg='wrong').has_equal_part(
                'is_default', msg='wrong').has_equal_value())
コード例 #6
0
def test_check_call(stu, passes):
    s = setup_state(stu, 'def test(a, b): print(a + b); return a + b')
    with helper.verify_sct(passes):
        s.check_function_def('test').multi(
            check_call("f(1,2)").has_equal_value(),
            check_call("f(1,2)").has_equal_output(),
            check_call("f(3,1)").has_equal_value(),
            check_call("f(1, '2')").has_equal_error())
コード例 #7
0
def test_for_loop_nested(stu, passes):
    s = setup_state(
        stu, 'for i in range(3):\n  for j in range(4):\n    print(i + j)')
    with helper.verify_sct(passes):
        s.check_for_loop().multi(
            check_iter().has_equal_value(),
            check_body().set_context(2).check_for_loop().multi(
                check_iter(),
                check_body().set_context(3).has_equal_output()))
コード例 #8
0
def test_check_lambda_full_ast_based(stu, passes):
    s = setup_state(stu, 'lambda x, y=2: print(x + y)')
    with helper.verify_sct(passes):
        s.check_lambda_function(0).multi(
            has_equal_part_len('args', unequal_msg='wrong'),
            check_args(0).has_equal_part('name', msg='wrong').has_equal_part(
                'is_default', msg='wrong'),
            check_args(1).has_equal_part('name', msg='wrong').has_equal_part(
                'is_default', msg='wrong').has_equal_value(),
            check_body().set_context(1, 2).has_equal_output())
コード例 #9
0
def test_check_list_comp_basic(stu, passes):
    pec = "x = {'a': 2, 'b':3, 'c':4, 'd':'test'}"
    sol = "[key + str(val) for key,val in x.items() if isinstance(key, str) if isinstance(val, int)]"
    s = setup_state(stu, sol, pec)
    with helper.verify_sct(passes):
        s.check_list_comp().multi(
            check_iter().has_equal_value(),
            check_ifs(0).check_function('isinstance').check_args(
                'obj').has_equal_ast(),
            check_ifs(1).check_function('isinstance').check_args(
                'obj').has_equal_ast(),
            check_body().has_context(exact_names=True).set_context(
                'a', 2).has_equal_value())
コード例 #10
0
ファイル: test_has_expr.py プロジェクト: fossabot/pythonwhat
def test_has_equal_value_name(stu, passes):
    s = setup_state(stu, 'a = 0\nfor i in range(0): a = 1')
    with helper.verify_sct(passes):
        s.check_for_loop().check_body().has_equal_value(name='a')
コード例 #11
0
def test_has_context(stu, exact, passes):
    s = setup_state(stu, 'for i in range(2): pass')
    with helper.verify_sct(passes):
        s.check_for_loop().check_body().has_context(exact_names=exact)
コード例 #12
0
ファイル: test_has_expr.py プロジェクト: fossabot/pythonwhat
def test_has_equal_output_for(stu, passes, context_vals):
    s = setup_state(stu, "for i in range(10):\n    print(i)")
    with helper.verify_sct(passes):
        s.check_for_loop().check_body().has_equal_output(
            context_vals=context_vals)
コード例 #13
0
ファイル: test_has_expr.py プロジェクト: fossabot/pythonwhat
def test_has_equal_output_basic(stu, passes):
    s = setup_state(stu, 'x = {"a":1, "b":2, "c": 3}')
    with helper.verify_sct(passes):
        s.test_expression_output(expr_code='print(x["a"])')
コード例 #14
0
def test_test_function_v2_print(stu, passes):
    s = setup_state(stu, "print(1)")
    with helper.verify_sct(passes):
        s.test_function_v2('print', params=['value'])
コード例 #15
0
ファイル: test_has_expr.py プロジェクト: fossabot/pythonwhat
def test_copy_functionality(copy, passes):
    s = setup_state('a = [1]', 'a = [2]')
    with helper.verify_sct(passes):
        s.has_equal_value(expr_code='a[0] = 3', name='a',
                          copy=copy).has_equal_value(expr_code='a', name='a')
コード例 #16
0
def test_test_function_v2_params(stu, passes):
  s = setup_state(stu, 'my_fun(1, b = 2)', pec='def my_fun(a, b): pass')
  with helper.verify_sct(passes):
        s.test_function_v2('my_fun', params = ['a'])
コード例 #17
0
def test_test_function_do_eval(stu, do_eval, passes):
    s = setup_state(stu, 'round(a)', pec='a,b = 1,2')
    with helper.verify_sct(passes):
        s.test_function('round', do_eval=do_eval)
コード例 #18
0
ファイル: test_has_expr.py プロジェクト: fossabot/pythonwhat
def test_has_equal_value_basic(stu, passes):
    s = setup_state(stu, 'a = 2')
    with helper.verify_sct(passes):
        s.has_equal_value(expr_code='a')
コード例 #19
0
def test_sig_from_params(stu, passes):
    s = setup_state(stu, "max([1, 2, 3, 4])")
    with helper.verify_sct(passes):
        sig = sig_from_params(param('iterable', param.POSITIONAL_ONLY))
        s.check_function('max', signature = sig).check_args(0).has_equal_value()
コード例 #20
0
ファイル: test_has_expr.py プロジェクト: fossabot/pythonwhat
def test_test_custom_equality_func(tol, passes):
    s = setup_state("a = [1.011]", "a = [1.01]")
    import numpy as np
    with helper.verify_sct(passes):
        s.check_object('a').has_equal_value(
            func=lambda x, y: np.allclose(x, y, atol=tol))
コード例 #21
0
def test_test_function_basic(stu, passes):
    s = setup_state(stu, 'my_fun(a = 1, b = 2)', pec='def my_fun(a, b): pass')
    with helper.verify_sct(passes):
        s.test_function('my_fun')
コード例 #22
0
def test_basic(stu, correct):
    s = setup_state(stu_code = stu, sol_code = 'import pandas as pd')
    with helper.verify_sct(correct):
        s.has_import('pandas')
コード例 #23
0
def test_test_function_keywords(stu, passes):
  s = setup_state(stu, 'my_fun(1, b = 2)', pec='def my_fun(a, b): pass')
  with helper.verify_sct(passes):
        s.test_function('my_fun', args = [], keywords = ['b'])
コード例 #24
0
def test_same_as(stu, same_as, correct):
    s = setup_state(stu_code = stu, sol_code = 'import pandas as pd')
    with helper.verify_sct(correct):
        s.has_import('pandas', same_as = same_as)
コード例 #25
0
def test_test_function_print(stu, passes):
    s = setup_state(stu, "print(1)")
    with helper.verify_sct(passes):
        s.test_function('print')
コード例 #26
0
def test_chaining(stu, correct):
    s = setup_state(stu_code = stu, sol_code = 'import numpy.random as rand')
    with helper.verify_sct(correct):
        s.has_import('numpy.random')
コード例 #27
0
def test_test_function_v2_do_eval(stu, do_eval, passes):
    s = setup_state(stu, 'round(a)', pec='a,b = 1,2')
    with helper.verify_sct(passes):
        s.test_function_v2('round', params=['number'], do_eval=[do_eval])
コード例 #28
0
def test_set_env_fail(state):
    with helper.verify_sct(False):
        state.multi(set_env(x=4), set_env(y=5).has_equal_value(name='x'))
コード例 #29
0
def test_basic_has_printout(stu, correct):
    sol = 'print(1, 2, 3)'
    s = setup_state(stu_code=stu, sol_code=sol)
    with helper.verify_sct(correct):
        s.has_printout(0)
コード例 #30
0
def test_set_env_full_example(stu, passes):
    s = setup_state(stu, "print(a_list[1])", pec="a_list = [0, 1, 2]")
    with helper.verify_sct(passes):
        s.set_env(a_list=list(range(10))).has_equal_output()