def test_if_elif_else_blocks(self): counter = [0] def function(): counter[0]+=1 return 'abc' scope = ({'print': function}) self.assertEquals('abc', dojo_compile('if 2+2==5: (print()) elif 2+2==4: (print(); print()) else: ()')(scope)) self.assertEquals(2, counter[0]) self.assertEquals(3, dojo_compile('if 2+2==5: (print()) elif 2+2==6: (print(); print()) else: 3')(scope)) self.assertEquals(2, counter[0])
def test_and_operator(self): counter = [0] def function(): counter[0]+=1 return True scope = ({'print': function}) self.assertEquals(False, dojo_compile('2+2==5 and print()')(scope)) self.assertEquals(0, counter[0]) self.assertEquals(True, dojo_compile('2+2==4 and print()')(scope)) self.assertEquals(1, counter[0])
def test_or_operator(self): counter = [0] def function(): counter[0]+=1 return False scope = ({'print': function}) self.assertEquals(False, dojo_compile('2+2==5 or print()')(scope)) self.assertEquals(1, counter[0]) self.assertEquals(True, dojo_compile('2+2==4 or print()')(scope)) self.assertEquals(1, counter[0]) self.assertEquals(True, dojo_compile('2+2==4 or 2+3==5')(scope)) self.assertEquals(1, counter[0])
def test_define_multiline_with_return_functions(self): self.assertEquals(1024, dojo_compile(""" test = /x,y=>( a = y**.5 return x**a ) test(2, 100) """)())
def test_define_multiline_functions_with_parenthesis_below(self): self.assertEquals(1024, dojo_compile(""" test = /x,y=> ( a = y**.5 x**a ) test(2, 100) """)())
def test_simple_binaries(self): self.assertEquals(44, dojo_compile('42+2')()) self.assertEquals(40, dojo_compile('42-2')()) self.assertEquals(84, dojo_compile('42*2')()) self.assertEquals(21.5, dojo_compile('43/2')()) self.assertEquals(21, dojo_compile('43//2')()) self.assertEquals(2, dojo_compile('42%4')()) self.assertEquals(1024, dojo_compile('2**10')())
def test_if_else_blocks(self): counter = [0] def function(): counter[0]+=1 return 'abc' scope = ({'print': function}) self.assertEquals(None, dojo_compile('if 2+2==5: (print();print())')(scope)) self.assertEquals(0, counter[0]) self.assertEquals('abc', dojo_compile('if 2+2==4: (print();print())')(scope)) self.assertEquals(2, counter[0]) self.assertEquals('bbb', dojo_compile('if 2+2==5: (print();print()) else: ("qwe";"bbb")')(scope)) self.assertEquals(2, counter[0]) self.assertEquals('abc', dojo_compile('if 2+2==4: (print();print()) else: ("qwe";"bbb")')(scope)) self.assertEquals(4, counter[0])
def test_if_else(self): counter = [0] def function(): counter[0]+=1 return 'abc' scope = ({'print': function}) self.assertEquals(None, dojo_compile('if 2+2==5: print()')(scope)) self.assertEquals(0, counter[0]) self.assertEquals('abc', dojo_compile('if 2+2==4: print()')(scope)) self.assertEquals(1, counter[0]) self.assertEquals('qwe', dojo_compile('if 2+2==5: print() else: "qwe"')(scope)) self.assertEquals(1, counter[0]) self.assertEquals('abc', dojo_compile('if 2+2==4: print() else: "qwe"')(scope)) self.assertEquals(2, counter[0])
def test_in_not_int(self): self.assertEquals(True, dojo_compile('2 in [1,2,3]')()) self.assertEquals(False, dojo_compile('4 in [1,2,3]')()) self.assertEquals(False, dojo_compile('2 not in [1,2,3]')()) self.assertEquals(True, dojo_compile('4 not in [1,2,3]')()) self.assertEquals(False, dojo_compile('2 not \t\n in [1,2,3]')()) self.assertEquals(True, dojo_compile('4 not \t\n in [1,2,3]')())
def test_comparation_operators(self): self.assertEquals(False, dojo_compile('a=2; b=3; a>b')()) self.assertEquals(True, dojo_compile('a=2; b=3; a<b')()) self.assertEquals(False, dojo_compile('a=2; b=3; a>=b')()) self.assertEquals(True, dojo_compile('a=2; b=3; a<=b')()) self.assertEquals(True, dojo_compile('a=2; b=2; a>=b')()) self.assertEquals(True, dojo_compile('a=2; b=2; a<=b')())
def test_unaries_ambiguity_not(self): program = dojo_compile('4;-2') self.assertEquals(-2, program())
def test_unaries(self): program = dojo_compile('-2') self.assertEquals(-2, program())
def test_double_callable(self): scope = {'add':lambda x:lambda y:x+y} self.assertEquals(14, dojo_compile('2*add(5)(2)')(scope))
def test_with_varargs(self): scope = {'add':lambda *x:x} self.assertEquals((5,2), dojo_compile('add(5, 2)')(scope))
def test_constant(self): self.assertEquals(42, dojo_compile('42')())
def test_set_variable_is_also_expression(self): self.assertEquals(20, dojo_compile('a=(2+3)*4;c=b=a')())
def test_get_variable_from_global(self): self.assertEquals(5, dojo_compile('a+b')({'a':2,'b':3}))
def test_precedence(self): self.assertEquals(14, dojo_compile('2+3*4')())
def test_explicit_precedence(self): self.assertEquals(20, dojo_compile('(2+3)*4')())
def test_set_variable_and_get_after(self): self.assertEquals(5, dojo_compile('a=2; a+3')())
def test_local_variable_overrides_global(self): self.assertEquals(7, dojo_compile('b=5;a+b')({'a':2,'b':3}))
def test_reverse_dict_to_list(self): self.assertEquals(['a', 'b', 'c'], dojo_compile('{"a":0,"b":1,"c":2}|>dict.items|>sorted{@key=x=>x[1]}|>map{x=>x[0]}|>list')())
def test_callable_with_named_args(self): scope = {'div':lambda x=10, y=2:x/y} compiled = dojo_compile('div(@y=5)') self.assertEquals(2, compiled(scope))
def test_exception_contains_line_number_on_different_line(self): with self.assertRaises(UnexpectedToken) as context: dojo_compile('2+2\n2+3\n )') self.assertIn('line 3 column 3', context.exception.args[0])
def test_callable_named_params(self): scope = {'add':lambda x, y:x+y} self.assertEquals(14, dojo_compile('2*add(5, 2)')(scope))
def test_exception_contains_line_number_on_same_line(self): with self.assertRaises(UnexpectedToken) as context: dojo_compile('2+2; 2+3 )') self.assertIn('line 1 column 11', context.exception.args[0])
def test_null_callable_inside_block(self): scope = {'add':lambda:2, 'abc':3} self.assertEquals(2, dojo_compile('abc\n(add();add())')(scope))
def test_when_unknown_char(self): with self.assertRaises(InvalidSyntax) as context: dojo_compile('$') self.assertIn('line 1 column 1', context.exception.args[0])
def test_callable_ambiguity(self): scope = {'add':lambda x:x+2} self.assertEquals(5, dojo_compile('add\n(5)')(scope))
def test_power_one_variable_to_another(self): self.assertEquals(1024, dojo_compile('a=2; b=10; a**b')())