示例#1
0
    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])
示例#2
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])
示例#3
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])
示例#4
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)
     """)())
示例#5
0
 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)
     """)())
示例#6
0
 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')())
示例#7
0
    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])
示例#8
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])
示例#9
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]')())
示例#10
0
    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')())
示例#11
0
 def test_unaries_ambiguity_not(self):
     program = dojo_compile('4;-2')
     self.assertEquals(-2, program())
示例#12
0
 def test_unaries(self):
     program = dojo_compile('-2')
     self.assertEquals(-2, program())
示例#13
0
 def test_double_callable(self):
     scope = {'add':lambda x:lambda y:x+y}
     self.assertEquals(14, dojo_compile('2*add(5)(2)')(scope))
示例#14
0
 def test_with_varargs(self):
     scope = {'add':lambda *x:x}
     self.assertEquals((5,2), dojo_compile('add(5, 2)')(scope))
示例#15
0
 def test_constant(self):
     self.assertEquals(42, dojo_compile('42')())
示例#16
0
 def test_set_variable_is_also_expression(self):
     self.assertEquals(20, dojo_compile('a=(2+3)*4;c=b=a')())
示例#17
0
 def test_get_variable_from_global(self):
     self.assertEquals(5, dojo_compile('a+b')({'a':2,'b':3}))
示例#18
0
 def test_precedence(self):
     self.assertEquals(14, dojo_compile('2+3*4')())
示例#19
0
 def test_explicit_precedence(self):
     self.assertEquals(20, dojo_compile('(2+3)*4')())
示例#20
0
 def test_set_variable_and_get_after(self):
     self.assertEquals(5, dojo_compile('a=2; a+3')())
示例#21
0
 def test_local_variable_overrides_global(self):
     self.assertEquals(7, dojo_compile('b=5;a+b')({'a':2,'b':3}))
示例#22
0
 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')())
示例#23
0
 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))
示例#24
0
    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])
示例#25
0
 def test_callable_named_params(self):
     scope = {'add':lambda x, y:x+y}
     self.assertEquals(14, dojo_compile('2*add(5, 2)')(scope))
示例#26
0
    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])
示例#27
0
 def test_null_callable_inside_block(self):
     scope = {'add':lambda:2, 'abc':3}
     self.assertEquals(2, dojo_compile('abc\n(add();add())')(scope))
示例#28
0
    def test_when_unknown_char(self):
        with self.assertRaises(InvalidSyntax) as context:
            dojo_compile('$')

        self.assertIn('line 1 column 1', context.exception.args[0])
示例#29
0
 def test_callable_ambiguity(self):
     scope = {'add':lambda x:x+2}
     self.assertEquals(5, dojo_compile('add\n(5)')(scope))
示例#30
0
 def test_power_one_variable_to_another(self):
     self.assertEquals(1024, dojo_compile('a=2; b=10; a**b')())