LuaObjectTests.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using Lua.Standard;
  2. namespace Lua.Tests;
  3. [LuaObject]
  4. public partial class LuaTestObj
  5. {
  6. int x;
  7. int y;
  8. [LuaMember("x")]
  9. public int X
  10. {
  11. get => x;
  12. set => x = value;
  13. }
  14. [LuaMember("y")]
  15. public int Y
  16. {
  17. get => y;
  18. set => y = value;
  19. }
  20. [LuaMember("create")]
  21. public static LuaTestObj Create(int x, int y)
  22. {
  23. return new LuaTestObj() { x = x, y = y };
  24. }
  25. [LuaMetamethod(LuaObjectMetamethod.Add)]
  26. public static LuaTestObj Add(LuaTestObj a, LuaTestObj b)
  27. {
  28. return new LuaTestObj() { x = a.x + b.x, y = a.y + b.y };
  29. }
  30. [LuaMetamethod(LuaObjectMetamethod.Sub)]
  31. public static async Task<LuaTestObj> Sub(LuaTestObj a, LuaTestObj b)
  32. {
  33. await Task.Delay(1);
  34. return new LuaTestObj() { x = a.x - b.x, y = a.y - b.y };
  35. }
  36. }
  37. [LuaObject]
  38. public partial class TestUserData
  39. {
  40. [LuaMember] public int Property { get; init; }
  41. [LuaMember] public int ReadOnlyProperty { get; }
  42. [LuaMember] public int SetOnlyProperty { set { } }
  43. [LuaMember] public LuaValue LuaValueProperty { get; set; }
  44. [LuaMember("p2")] public string PropertyWithName { get; set; } = "";
  45. [LuaMember]
  46. public static void MethodVoid()
  47. {
  48. Console.WriteLine("HEY!");
  49. }
  50. [LuaMember]
  51. public static async Task MethodAsync()
  52. {
  53. await Task.CompletedTask;
  54. }
  55. [LuaMember]
  56. public static double StaticMethodWithReturnValue(double a, double b)
  57. {
  58. Console.WriteLine($"HEY! {a} {b}");
  59. return a + b;
  60. }
  61. [LuaMember]
  62. public double InstanceMethodWithReturnValue()
  63. {
  64. return Property;
  65. }
  66. [LuaMember]
  67. public async ValueTask<LuaValue> InstanceMethodWithReturnValueAsync(LuaValue value, CancellationToken ct)
  68. {
  69. await Task.Delay(1, ct);
  70. return value;
  71. }
  72. [LuaMetamethod(LuaObjectMetamethod.Call)]
  73. public string Call()
  74. {
  75. return "Called!";
  76. }
  77. }
  78. public class LuaObjectTests
  79. {
  80. [Test]
  81. public async Task Test_Property()
  82. {
  83. var userData = new TestUserData { Property = 1 };
  84. var state = LuaState.Create();
  85. state.Environment["test"] = userData;
  86. var results = await state.DoStringAsync("return test.Property");
  87. Assert.That(results, Has.Length.EqualTo(1));
  88. Assert.That(results[0], Is.EqualTo(new LuaValue(1)));
  89. }
  90. [Test]
  91. public async Task Test_PropertyWithName()
  92. {
  93. var userData = new TestUserData { PropertyWithName = "foo" };
  94. var state = LuaState.Create();
  95. state.Environment["test"] = userData;
  96. var results = await state.DoStringAsync("return test.p2");
  97. Assert.That(results, Has.Length.EqualTo(1));
  98. Assert.That(results[0], Is.EqualTo(new LuaValue("foo")));
  99. }
  100. [Test]
  101. public async Task Test_MethodVoid()
  102. {
  103. var userData = new TestUserData();
  104. var state = LuaState.Create();
  105. state.Environment["test"] = userData;
  106. var results = await state.DoStringAsync("return test.MethodVoid()");
  107. Assert.That(results, Has.Length.EqualTo(0));
  108. }
  109. [Test]
  110. public async Task Test_MethodAsync()
  111. {
  112. var userData = new TestUserData();
  113. var state = LuaState.Create();
  114. state.Environment["test"] = userData;
  115. var results = await state.DoStringAsync("return test.MethodAsync()");
  116. Assert.That(results, Has.Length.EqualTo(0));
  117. }
  118. [Test]
  119. public async Task Test_StaticMethodWithReturnValue()
  120. {
  121. var userData = new TestUserData();
  122. var state = LuaState.Create();
  123. state.Environment["test"] = userData;
  124. var results = await state.DoStringAsync("return test.StaticMethodWithReturnValue(1, 2)");
  125. Assert.That(results, Has.Length.EqualTo(1));
  126. Assert.That(results[0], Is.EqualTo(new LuaValue(3)));
  127. }
  128. [Test]
  129. public async Task Test_InstanceMethodWithReturnValue()
  130. {
  131. var userData = new TestUserData { Property = 1 };
  132. var state = LuaState.Create();
  133. state.Environment["test"] = userData;
  134. var results = await state.DoStringAsync("return test:InstanceMethodWithReturnValue()");
  135. Assert.That(results, Has.Length.EqualTo(1));
  136. Assert.That(results[0], Is.EqualTo(new LuaValue(1)));
  137. }
  138. [Test]
  139. public async Task Test_InstanceMethodWithReturnValueAsync()
  140. {
  141. var userData = new TestUserData { Property = 1 };
  142. var state = LuaState.Create();
  143. state.Environment["test"] = userData;
  144. var results = await state.DoStringAsync("return test:InstanceMethodWithReturnValueAsync(2)");
  145. Assert.That(results, Has.Length.EqualTo(1));
  146. Assert.That(results[0], Is.EqualTo(new LuaValue(2)));
  147. }
  148. [Test]
  149. public async Task Test_CallMetamethod()
  150. {
  151. var userData = new TestUserData();
  152. var state = LuaState.Create();
  153. state.OpenBasicLibrary();
  154. state.Environment["test"] = userData;
  155. var results = await state.DoStringAsync("""
  156. assert(test() == 'Called!')
  157. return test()
  158. """);
  159. Assert.That(results, Has.Length.EqualTo(1));
  160. Assert.That(results[0], Is.EqualTo(new LuaValue("Called!")));
  161. }
  162. [Test]
  163. public async Task Test_ArithMetamethod()
  164. {
  165. var userData = new LuaTestObj();
  166. var state = LuaState.Create();
  167. state.OpenBasicLibrary();
  168. state.Environment["TestObj"] = userData;
  169. var results = await state.DoStringAsync("""
  170. local a = TestObj.create(1, 2)
  171. local b = TestObj.create(3, 4)
  172. return a + b, a - b
  173. """);
  174. Assert.That(results, Has.Length.EqualTo(2));
  175. Assert.That(results[0].Read<object>(), Is.TypeOf<LuaTestObj>());
  176. var objAdd = results[0].Read<LuaTestObj>();
  177. Assert.That(objAdd.X, Is.EqualTo(4));
  178. Assert.That(objAdd.Y, Is.EqualTo(6));
  179. Assert.That(results[1].Read<object>(), Is.TypeOf<LuaTestObj>());
  180. var objSub = results[1].Read<LuaTestObj>();
  181. Assert.That(objSub.X, Is.EqualTo(-2));
  182. Assert.That(objSub.Y, Is.EqualTo(-2));
  183. }
  184. }