LuaTestSuiteExtract.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using MoonSharp.Interpreter.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using MoonSharp.Interpreter.Execution;
  7. using NUnit.Framework;
  8. namespace MoonSharp.Interpreter.Tests
  9. {
  10. /// <summary>
  11. /// Selected tests extracted from Lua test suite
  12. /// </summary>
  13. [TestFixture]
  14. class LuaTestSuiteExtract
  15. {
  16. void RunTest(string script)
  17. {
  18. HashSet<string> failedTests = new HashSet<string>();
  19. int i = 0;
  20. var globalCtx = new Table();
  21. globalCtx[new RValue("xassert")] = new RValue(new CallbackFunction(
  22. a =>
  23. {
  24. if (!a[1].TestAsBoolean())
  25. failedTests.Add(a[0].String);
  26. return RValue.Nil;
  27. }));
  28. globalCtx[new RValue("assert")] = new RValue(new CallbackFunction(
  29. a =>
  30. {
  31. ++i;
  32. if (!a[0].TestAsBoolean())
  33. failedTests.Add(string.Format("assert #{0}", i));
  34. return RValue.Nil;
  35. }));
  36. globalCtx[new RValue("print")] = new RValue(new CallbackFunction(a =>
  37. {
  38. // Debug.WriteLine(string.Join(" ", a.Select(v => v.AsString()).ToArray()));
  39. return RValue.Nil;
  40. }));
  41. RValue res = MoonSharpInterpreter.LoadFromString(script).Execute(globalCtx);
  42. Assert.IsFalse(failedTests.Any(), string.Format("Failed asserts {0}",
  43. string.Join(", ", failedTests.Select(xi => xi.ToString()).ToArray())));
  44. }
  45. [Test]
  46. public void LuaSuite_Calls_LocalFunctionRecursion()
  47. {
  48. RunTest(@"
  49. -- testing local-function recursion
  50. fact = false
  51. do
  52. local res = 1
  53. local function fact (n)
  54. if n==0 then return res
  55. else return n*fact(n-1)
  56. end
  57. end
  58. xassert('fact(5) == 120', fact(5) == 120)
  59. end
  60. xassert('fact == false', fact == false)
  61. ");
  62. }
  63. [Test]
  64. public void LuaSuite_Calls_Declarations()
  65. {
  66. RunTest(@"
  67. -- testing local-function recursion
  68. -- testing declarations
  69. a = {i = 10}
  70. self = 20
  71. function a:x (x) return x+self.i end
  72. function a.y (x) return x+self end
  73. xassert('a:x(1)+10 == a.y(1)', a:x(1)+10 == a.y(1))
  74. a.t = {i=-100}
  75. a['t'].x = function (self, a,b) return self.i+a+b end
  76. xassert('a.t:x(2,3) == -95', a.t:x(2,3) == -95)
  77. do
  78. local a = {x=0}
  79. function a:add (x) self.x, a.y = self.x+x, 20; return self end
  80. xassert('a:add(10):add(20):add(30).x == 60 and a.y == 20', a:add(10):add(20):add(30).x == 60 and a.y == 20)
  81. end
  82. local a = {b={c={}}}
  83. function a.b.c.f1 (x) return x+1 end
  84. function a.b.c:f2 (x,y) self[x] = y end
  85. xassert('a.b.c.f1(4) == 5', a.b.c.f1(4) == 5)
  86. a.b.c:f2('k', 12); xassert('a.b.c.k == 12', a.b.c.k == 12)
  87. print('+')
  88. t = nil -- 'declare' t
  89. function f(a,b,c) local d = 'a'; t={a,b,c,d} end
  90. f( -- this line change must be valid
  91. 1,2)
  92. xassert('missingparam', t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a')
  93. f(1,2, -- this one too
  94. 3,4)
  95. xassert('extraparam', t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
  96. ");
  97. }
  98. [Test]
  99. public void LuaSuite_Calls_Closures()
  100. {
  101. RunTest(@"
  102. -- fixed-point operator
  103. Z = function (le)
  104. local function a (f)
  105. return le(function (x) return f(f)(x) end)
  106. end
  107. return a(a)
  108. end
  109. -- non-recursive factorial
  110. F = function (f)
  111. return function (n)
  112. if n == 0 then return 1
  113. else return n*f(n-1) end
  114. end
  115. end
  116. fat = Z(F)
  117. xassert('fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4)', fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
  118. local function g (z)
  119. local function f (a,b,c,d)
  120. return function (x,y) return a+b+c+d+a+x+y+z end
  121. end
  122. return f(z,z+1,z+2,z+3)
  123. end
  124. f = g(10)
  125. xassert('f(9, 16) == 10+11+12+13+10+9+16+10', f(9, 16) == 10+11+12+13+10+9+16+10)
  126. Z, F, f = nil
  127. --print('+')
  128. ");
  129. }
  130. }
  131. }