LuaObjectTests.cs 6.3 KB

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