Example #1
0
 def test_10_find_func(self):
     """Testing function finding."""
     ahk.start()
     ahk.ready()
     # First test find function
     self.assertEqual(ahk.find_func('nonexist'),
                      0,
                      msg="Non-null pointer to non-existent function?")
     # Add function
     ahk.add_lines("""
                   AddTwo(n) {
                       return n + 2
                   }
                   """)
     # Try to get a reference
     res = ahk.find_func('AddTwo')
     self.assertNotEqual(res, 0, msg="Null pointer to existent function?")
     return  # The following code fails
     # Try calling the function
     proto = ctypes.CFUNCTYPE(ctypes.c_char_p, ctypes.c_char_p)
     func = proto(res)
     res = int(func('5'))
     self.assertEqual(res,
                      5 + 2,
                      msg="Got bad result {0} when calling function?")
Example #2
0
 def test_11_find_label(self):
     """Testing finding a labeled line."""
     ahk.start()
     ahk.ready()
     # Non-existent label should return Null pointer
     self.assertEqual(ahk.find_label('nonexist'),
                      0,
                      msg="Got pointer to non-existent label!")
     # Add lines by string
     ahk.add_lines("test = 0\nlbl:\ntest := test+5")
     res = ahk.get("test")
     self.assertEqual(res,
                      "5",
                      msg="Value={0}, script not evaluated?".format(res))
     # Get pointer to the labeled line
     res = ahk.find_label('lbl')
     self.assertNotEqual(res,
                         0,
                         msg="Got null pointer to known good label?")
     return  # The remaining code fails or hangs the process
     #NOTE depends on exec_line
     #ahk.exec_line(ahk.exec_line(res, 0))
     ahk.exec_line(res, 0)
     res = ahk.get("test")
     print(res)
     self.assertEqual(res,
                      "10",
                      msg="Value={0}, script not evaluated?".format(res))
Example #3
0
    def test_06_jump(self):
        """Testing jumping to a labeled code block."""
        ahk.start()
        ahk.ready()
        # Add lines by string
        self.assertEqual(ahk.get("test"), "", msg="Residual data found?")
        ahk.add_lines("test = 0\nlbl:\ntest := test+5")
        value = ahk.get("test")
        self.assertEqual(value,
                         "5",
                         msg="Value={0}, script not evaluated?".format(value))

        # Test jumping once
        self.assertTrue(ahk.jump("lbl"), msg="Label not found in script!")
        value = ahk.get("test")
        self.assertEqual(value,
                         "10",
                         msg="Value={0}, script not evaluated?".format(value))

        # Test repeated jump
        for i in range(8):
            self.assertTrue(ahk.jump("lbl"), msg="Label not found in script!")
            expected = str(15 + (i * 5))
            value = ahk.get("test")
            self.assertEqual(value,
                             expected,
                             msg="Expected {0}, found {1}!".format(
                                 expected, value))
Example #4
0
 def test_07_call(self):
     """Testing calling ahk functions."""
     ahk.start()
     ahk.ready()
     # Add lines by string containing a function definition
     ahk.add_lines("""
                   Add(x, y) {
                       return (x + y)
                   }
                   """)
     # Now call the new function and check the result
     result = ahk.call("Add", 5, 5)
     self.assertEqual(int(result),
                      10,
                      msg="Unexpected result {0}!".format(result))
Example #5
0
 def test_09_reload(self):
     """Testing reloading."""
     ahk.start()
     ahk.ready()
     # Set a new variable to a known value
     value = '5'
     ahk.add_lines("test = {0}\n".format(value))  # Set test variable
     res = ahk.get('test')
     self.assertEqual(
         res,
         value,
         msg="Value={0}, ahk.add_lines not evaluated?".format(res))
     # Reloading should clear all variable values
     ahk.reload()
     res = ahk.get('test')
     self.assertNotEqual(
         res,
         value,
         msg="Value={0}, variable retained value after reload?".format(res))
Example #6
0
 def test_08_post(self):
     """Testing posting function call."""
     ahk.start()
     ahk.ready()
     # Posting to a non-existent function should return false
     self.assertFalse(
         ahk.post('nonexistent'),
         msg="Success reported posting to non-existent function!")
     # Define a new function that changes the environment
     ahk.add_lines("""
                   changer() {
                       ErrorLevel = 1
                   }
                   """)
     # Set ErrorLevel then check value after posting function
     ahk.set("ErrorLevel", 0)
     # The following raises WindowsError access violation
     self.assertTrue(ahk.post('changer'),
                     msg="Failed to find an existing function?")
     self.assertNotEqual(0,
                         ahk.get("ErrorLevel"),
                         msg="Internal variable unchanged?")
Example #7
0
    def test_05_add_lines(self):
        """Test adding code to running script."""
        ahk.start()
        ahk.ready()
        # Add lines by string
        self.assertEqual(ahk.get("test"), "", msg="Residual data found?")
        ahk.add_lines("test = 5\n")
        value = ahk.get("test")
        self.assertEqual(value,
                         "5",
                         msg="Value={0}, script not evaluated?".format(value))

        # Add lines by file path
        with open(self.tempfilename, 'w') as tmp:
            tmp.write("test := test+5")

        addr = ahk.add_lines(filename=self.tempfilename)
        #NOTE depends on ahk.exec_line
        ahk.exec_line(addr, wait=True)
        value = ahk.get("test")
        self.assertEqual(value,
                         "10",
                         msg="Value={0}, script not evaluated?".format(value))
Example #8
0
 def test_12_exec_line(self):
     """Testing line-wise execution."""
     ahk.start()
     ahk.ready()
     # Test retrieving the current line
     self.assertNotEqual(ahk.exec_line(mode=0),
                         0,
                         msg="Got null pointer to first line?")
     # Add lines by string
     ahk.set('test', 0)
     addr = ahk.add_lines("test := test+5\n")
     res = ahk.get("test")
     self.assertEqual(res,
                      "5",
                      msg="Value={0}, script not evaluated?".format(res))
     # Executing the saved line should increase test by 5
     ahk.exec_line(addr, wait=True)
     res = ahk.get("test")
     self.assertEqual(res,
                      "10",
                      msg="Value={0}, line not executed?".format(res))