luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

test_runstring.lua (1902B)


      1 -- Test for RunString and CompileString functions
      2 
      3 print("=== Testing RunString ===")
      4 
      5 -- Test 1: Simple code execution
      6 print("Test 1: Simple code execution")
      7 local result = RunString("print('Hello from RunString!')")
      8 print("Result:", result)
      9 
     10 -- Test 2: Code with syntax error
     11 print("\nTest 2: Code with syntax error")
     12 local result2 = RunString("print('Missing closing quote)")
     13 print("Result:", result2)
     14 
     15 -- Test 3: Code with runtime error
     16 print("\nTest 3: Code with runtime error")
     17 local result3 = RunString("error('Intentional error')")
     18 print("Result:", result3)
     19 
     20 print("\n=== Testing CompileString ===")
     21 
     22 -- Test 4: Compile valid code
     23 print("Test 4: Compile valid code")
     24 local func, err = CompileString("return 2 + 2")
     25 if func then
     26     print("Compiled successfully, type:", type(func))
     27     local success, result = pcall(func)
     28     if success then
     29         print("Execution result:", result)
     30     else
     31         print("Execution failed:", result)
     32     end
     33 else
     34     print("Compilation failed:", err)
     35 end
     36 
     37 -- Test 5: Compile invalid code
     38 print("\nTest 5: Compile invalid code")
     39 local func2, err2 = CompileString("return 2 +")
     40 if func2 then
     41     print("Compiled successfully (unexpected)")
     42 else
     43     print("Compilation failed (expected):", err2)
     44 end
     45 
     46 -- Test 6: Compile and execute multiple times
     47 print("\nTest 6: Compile and execute multiple times")
     48 local counter_func, err3 = CompileString([[
     49     local count = 0
     50     return function()
     51         count = count + 1
     52         return count
     53     end
     54 ]])
     55 
     56 if counter_func then
     57     print("Compiled successfully")
     58     local success, counter = pcall(counter_func)
     59     if success and type(counter) == "function" then
     60         print("Counter 1:", counter())
     61         print("Counter 2:", counter())
     62         print("Counter 3:", counter())
     63     else
     64         print("Execution failed")
     65     end
     66 else
     67     print("Compilation failed:", err3)
     68 end
     69 
     70 print("\n=== All tests completed ===")