@@ -299,12 +299,73 @@ def gen_exercise(*args, **kwargs):
299299 setattr (TestOverride , test_name , pf )
300300
301301
302- #class TestSetContext(unittest.TestCase):
303- # def setUp(self):
304- # self.data = {
305- # "DC_PEC": "",
306- # "DC_SOLUTION": "[(i,j) for i,j in enumerate(range(10))]"
307- # }
302+ # Test SCT Ex syntax (copied from sqlwhat) -----------------------------------
303+
304+ import pytest
305+ from pythonwhat .check_syntax import Ex , F , state_dec
306+
307+ @pytest .fixture
308+ def addx ():
309+ return lambda x , state = None : state + x
310+
311+ @pytest .fixture
312+ def f ():
313+ return F ._from_func (lambda state = None : state + 'b' )
314+
315+ @pytest .fixture
316+ def f2 ():
317+ return F ._from_func (lambda state = None : state + 'c' )
318+
319+ def test_f_from_func (f ):
320+ assert f ('a' ) == 'ab'
321+
322+ def test_f_sct_copy_kw (addx ):
323+ assert F ()._sct_copy (addx )(x = 'x' )('state' ) == 'statex'
324+
325+ def test_f_sct_copy_pos (addx ):
326+ assert F ()._sct_copy (addx )('x' )('state' ) == 'statex'
327+
328+ def test_ex_sct_copy_kw (addx ):
329+ assert Ex ('state' )._sct_copy (addx )(x = 'x' )._state == 'statex'
330+
331+ def test_ex_sct_copy_pos (addx ):
332+ assert Ex ('state' )._sct_copy (addx )('x' )._state == 'statex'
333+
334+ def test_f_2_funcs (f , addx ):
335+ g = f ._sct_copy (addx )
336+
337+ assert g (x = 'x' )('a' ) == 'abx'
338+
339+ def test_f_add_unary_func (f ):
340+ g = f >> (lambda state = None : state + 'c' )
341+ assert g ('a' ) == 'abc'
342+
343+ def test_f_add_f (f , f2 ):
344+ g = f >> f2
345+ assert g ('a' ) == 'abc'
346+
347+ def test_f_from_state_dec (addx ):
348+ dec_addx = state_dec (addx )
349+ f = dec_addx (x = 'x' )
350+ isinstance (f , F )
351+ assert f ('state' ) == 'statex'
352+
353+ @pytest .fixture
354+ def ex ():
355+ return Ex ('state' )._sct_copy (lambda x , state = None : state + x )('x' )
356+
357+ def test_ex_add_f (ex , f ):
358+ (ex >> f )._state = 'statexb'
359+
360+ def test_ex_add_unary (ex ):
361+ (ex >> (lambda state = None : state + 'b' ))._state == 'statexb'
362+
363+ def test_ex_add_ex_err (ex ):
364+ with pytest .raises (BaseException ): ex >> ex
365+
366+ def test_f_add_ex_err (f , ex ):
367+ with pytest .raises (BaseException ): f >> ex
368+
308369
309370if __name__ == "__main__" :
310371 unittest .main ()
0 commit comments