Beispiel #1
0
    def test_F(self):
        self.data["DC_SCT"]  =  '''
list_comp = F().check_list_comp(0).check_body().set_context(ii=2).has_equal_value('unequal')
Ex().check_list_comp(0).check_body().set_context(aa=2).multi(list_comp)
'''
        sct_payload = helper.run(self.data)
        self.assertTrue(sct_payload['correct'])
Beispiel #2
0
    def test_multi_splits_node_and_check(self):
        self.data["DC_SCT"]  =  '''
test_body = F().check_list_comp(0).check_body().set_context(aa=2).has_equal_value('wrong')
Ex().check_list_comp(0).multi(F().check_body().set_context(aa=2).has_equal_value('wrong'))
'''
        sct_payload = helper.run(self.data)
        self.assertTrue(sct_payload['correct'])
Beispiel #3
0
    def test_multi_generator(self):
        self.data["DC_SCT"] = """
Ex().check_list_comp(0).check_body()\
        .multi(set_context(aa=i).has_equal_value('wrong') for i in range(2))
"""
        sct_payload = helper.run(self.data)
        self.assertTrue(sct_payload['correct'])
Beispiel #4
0
    def test_check_to_F_nested(self):
        self.data["DC_SCT"] = '''
# funky, but we're testing nested check functions!
multi_test = multi(check_list_comp(0).check_body().set_context(aa=2).has_equal_value('badbody'))
Ex().multi(multi_test)
'''
        sct_payload = helper.run(self.data)
        self.assertTrue(sct_payload['correct'])
Beispiel #5
0
    def test_spec_run_order(self):
        self.data["DC_CODE"] = '''[aa for aa in range(2)]'''
        self.data["DC_SCT"]  =  '''
Ex().test_list_comp(body=test_expression_result(extra_env={'aa': 2}, incorrect_msg = 'spec1'))
Ex().check_list_comp(0).check_body().set_context(aa=2).has_equal_value('spec2')
'''
        sct_payload = helper.run(self.data)
        self.assertFalse(sct_payload['correct'])
        self.assertIn('spec1', sct_payload['message'])
Beispiel #6
0
    def test_F_extends(self):
        self.data["DC_SCT"]  =  '''
list_comp = F().check_list_comp(0)
body_check = list_comp.check_body().set_context(aa=2).has_equal_value('unequal')

Ex().extend(body_check)\
        .check_list_comp(0).check_body().set_context(ii=2).has_equal_value('unequal')
'''
        sct_payload = helper.run(self.data)
        self.assertTrue(sct_payload['correct'])
Beispiel #7
0
    def test_spec1_in_multi_pass(self):
        self.data["DC_SCT"]  =  '''
te = test_expression_result(extra_env={'aa':2}, incorrect_msg='unequal')
Ex().multi(test_list_comp(body=te))                                                        # spec 1 inside multi
Ex().check_list_comp(0).check_body().multi(te)                                             # half of each spec
Ex().check_list_comp(0).check_body().set_context(aa=2).has_equal_value('unequal')          # full spec 2
test_list_comp(body=te)                                                                    # full spec 1
'''
        sct_payload = helper.run(self.data)
        self.assertTrue(sct_payload['correct'])
Beispiel #8
0
    def test_nested_test_or(self):
        self.data["DC_SCT"] = """
Ex().test_correct(
    check_object("x").has_equal_value(),
    test_or(
        check_function("round").check_args('number').has_equal_value(),
        test_or(
            has_code('2'),
            has_code('3')
        )
    )
)
        """
        sct_payload = helper.run(self.data)
        self.assertFalse(sct_payload['correct'])
Beispiel #9
0
def check_numpy_array(name, state=None):
    """Test a numpy array object. Return that object.
    
    Args:
        name: name of the object to be tested.

    """
    # is defined
    obj = Ex(state).check_object(name)

    # is a numpy array
    obj.is_instance(np.ndarray)

    # same shape
    obj.has_equal_value(expr_code='{:s}.shape'.format(name))

    # equal contents
    obj.has_equal_value()

    # return object state for chaining
    return obj
Beispiel #10
0
def setup_state(stu_code = "", sol_code = "", pec = "", pid = None):

    stu_output = io.StringIO()
    with redirect_stdout(stu_output):
        stu_process = StubProcess("%s\n%s" % (pec, stu_code), pid)

    sol_output = io.StringIO()
    with redirect_stdout(sol_output):
        sol_process = StubProcess("%s\n%s" % (pec, sol_code), pid)

    rep = Reporter()
    Reporter.active_reporter = rep

    state = State(
        student_code = stu_code,
        solution_code = sol_code,
        pre_exercise_code = pec,
        student_process = stu_process,
        solution_process = sol_process,
        raw_student_output = stu_output.getvalue())

    State.root_state = state
    return(Ex(state))
Beispiel #11
0
import helper
import pytest
from pythonwhat.Feedback import InstructorError


@pytest.mark.parametrize('sct', [
    '''
list_comp = F().check_list_comp(0).check_body().set_context(ii=2).has_equal_value('unequal')
Ex().check_list_comp(0).check_body().set_context(aa=2).multi(list_comp)
''', '''
list_comp = check_list_comp(0).check_body().set_context(ii=2).has_equal_value('unequal')
Ex().check_list_comp(0).check_body().set_context(aa=2).multi(list_comp)
''', '''
# funky, but we're testing nested check functions!
multi_test = multi(check_list_comp(0).check_body().set_context(aa=2).has_equal_value('badbody'))
Ex().multi(multi_test)
''', '''
list_comp = F().check_list_comp(0).check_body().set_context(ii=2).has_equal_value('unequal')
Ex().check_list_comp(0).check_body().set_context(aa=2).multi(list_comp)
Ex().check_list_comp(1).check_body().set_context(bb=4).multi(list_comp)
''', '''
eq_test = F().check_list_comp(0).check_body().set_context(1).has_equal_value
Ex().multi(eq_test('unequal'))
'''
])
def test_f_chain(sct):
    code = '[[ii+1 for ii in range(aa)] for aa in range(2)]\n[[ii*2 for ii in range(bb)] for bb in range(1,3)]'
    res = helper.run({
        'DC_SOLUTION': code,
        'DC_CODE': code,
        'DC_SCT': sct,
def test_ex_rshift_check_numpy_array_fail():
    state = create_x_state(np.array([1,2,3]), np.array([1,2]))
    with pytest.raises(TF): Ex(state) >> check_numpy_array('x')
Beispiel #13
0
 def test_F_assign_getattr(self):
     self.data["DC_SCT"]  =  '''
Beispiel #14
0
def ex():
    return Ex('state')._sct_copy(lambda x, state=None: state + x)('x')
Beispiel #15
0
def test_ex_sct_copy_pos(addx):
    assert Ex('state')._sct_copy(addx)('x')._state == 'statex'
Beispiel #16
0
def test_ex_sct_copy_kw(addx):
    assert Ex('state')._sct_copy(addx)(x = 'x')._state == 'statex'
Beispiel #17
0
 def test_check_to_F(self):
     self.data["DC_SCT"] = '''
def test_ex_check_numpy_array():
    state = create_x_state(np.array([1,2,3]), np.array([1,2, 3]))
    Ex(state).multi(check_numpy_array('x'))