FunctionOverload.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // Copyright (c) 2009 Brandon Jones
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. //
  8. // Permission is granted to anyone to use this software for any purpose,
  9. // including commercial applications, and to alter it and redistribute it
  10. // freely, subject to the following restrictions:
  11. //
  12. // 1. The origin of this software must not be misrepresented; you must not
  13. // claim that you wrote the original software. If you use this software
  14. // in a product, an acknowledgment in the product documentation would be
  15. // appreciated but is not required.
  16. //
  17. // 2. Altered source versions must be plainly marked as such, and must not be
  18. // misrepresented as being the original software.
  19. //
  20. // 3. This notice may not be removed or altered from any source
  21. // distribution.
  22. //
  23. #include <gtest/gtest.h>
  24. #include <sqrat.h>
  25. #include "Fixture.h"
  26. using namespace Sqrat;
  27. class Speaker {
  28. public:
  29. int Echo() {
  30. return 0;
  31. }
  32. int Echo(int val) {
  33. return val;
  34. }
  35. };
  36. int GlobalEcho() {
  37. return 0;
  38. }
  39. int GlobalEcho(int val) {
  40. return val;
  41. }
  42. class StaticTestClass
  43. {
  44. public:
  45. static int i1, i2;
  46. static void set(int a1) { i1 = a1; }
  47. static void set(int a1, int a2) { i1 = a1; i2 = a2; }
  48. static int get_i1() { return i1; }
  49. static int get_i2() { return i2; }
  50. };
  51. int StaticTestClass::i1 = -1;
  52. int StaticTestClass::i2 = -1;
  53. TEST_F(SqratTest, OverloadedMemberFunction) {
  54. DefaultVM::Set(vm);
  55. // Member function overloads
  56. RootTable().Bind(_SC("Speaker"),
  57. Class<Speaker>(vm, _SC("Speaker"))
  58. .Overload<int (Speaker::*)()>(_SC("Echo"), &Speaker::Echo)
  59. .Overload<int (Speaker::*)(int)>(_SC("Echo"), &Speaker::Echo)
  60. );
  61. // static Member function overloads
  62. RootTable().Bind(_SC("StaticTestClass"),
  63. Class<StaticTestClass>(vm, _SC("StaticTestClass"))
  64. .StaticOverload<void (*)(int)>(_SC("set"), &StaticTestClass::set)
  65. .StaticOverload<void (*)(int, int)>(_SC("set"), &StaticTestClass::set)
  66. .StaticFunc(_SC("get_i1"), &StaticTestClass::get_i1)
  67. .StaticFunc(_SC("get_i2"), &StaticTestClass::get_i2)
  68. );
  69. // Global Function overloads
  70. RootTable().Overload<int (*)()>(_SC("GlobalEcho"), &GlobalEcho);
  71. RootTable().Overload<int (*)(int)>(_SC("GlobalEcho"), &GlobalEcho);
  72. Script script;
  73. script.CompileString(_SC(" \
  74. s <- Speaker(); \
  75. \
  76. gTest.EXPECT_INT_EQ(0, s.Echo()); \
  77. gTest.EXPECT_INT_EQ(1, s.Echo(1)); \
  78. gTest.EXPECT_INT_EQ(0, GlobalEcho()); \
  79. gTest.EXPECT_INT_EQ(1, GlobalEcho(1)); \
  80. "));
  81. if (Sqrat::Error::Occurred(vm)) {
  82. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  83. }
  84. script.Run();
  85. if (Sqrat::Error::Occurred(vm)) {
  86. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  87. }
  88. script.CompileString(_SC(" \
  89. s <- StaticTestClass(); \
  90. \
  91. gTest.EXPECT_INT_EQ(-1, s.get_i1()); \
  92. gTest.EXPECT_INT_EQ(-1, s.get_i2()); \
  93. s.set(2); \
  94. gTest.EXPECT_INT_EQ(2, s.get_i1()); \
  95. gTest.EXPECT_INT_EQ(-1, s.get_i2()); \
  96. s.set(4, 6); \
  97. gTest.EXPECT_INT_EQ(4, s.get_i1()); \
  98. gTest.EXPECT_INT_EQ(6, s.get_i2()); \
  99. "));
  100. if (Sqrat::Error::Occurred(vm)) {
  101. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  102. }
  103. script.Run();
  104. if (Sqrat::Error::Occurred(vm)) {
  105. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  106. }
  107. }
  108. //
  109. // Overload test with const functions, based on scenario provided by emeyex
  110. //
  111. class Entity {
  112. public:
  113. unsigned int QueryEnumValue( unsigned int enumKey, unsigned int enumValueDefault ) const {
  114. return enumKey;
  115. }
  116. unsigned int QueryEnumValue( unsigned int enumKey ) const {
  117. return QueryEnumValue( enumKey, 0 );
  118. }
  119. };
  120. TEST_F(SqratTest, ConstOverloadTest) {
  121. DefaultVM::Set(vm);
  122. // Member function overloads
  123. RootTable().Bind(_SC("Entity"),
  124. Class<Entity>(vm, _SC("Entity"))
  125. .Overload<unsigned int (Entity::*)(unsigned int, unsigned int) const>(_SC("QueryEnumValue"), &Entity::QueryEnumValue)
  126. .Overload<unsigned int (Entity::*)(unsigned int) const>(_SC("QueryEnumValue"), &Entity::QueryEnumValue)
  127. );
  128. Script script;
  129. script.CompileString(_SC(" \
  130. e <- Entity(); \
  131. \
  132. gTest.EXPECT_INT_EQ(1, e.QueryEnumValue(1, 0)); \
  133. gTest.EXPECT_INT_EQ(2, e.QueryEnumValue(2)); \
  134. "));
  135. if (Sqrat::Error::Occurred(vm)) {
  136. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  137. }
  138. script.Run();
  139. if (Sqrat::Error::Occurred(vm)) {
  140. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  141. }
  142. }
  143. class B
  144. {
  145. private:
  146. int value;
  147. public:
  148. B(): value(-1) {}
  149. int set(int v)
  150. {
  151. return value = v;
  152. }
  153. int get()
  154. {
  155. //std::cout << "B's address is " << (long) this << std::endl;
  156. return value;
  157. }
  158. static string shared;
  159. static int sharedInt;
  160. };
  161. static B& getB(B &b)
  162. {
  163. return b;
  164. }
  165. static B& getB2(B *b, int, char *)
  166. {
  167. return *b;
  168. }
  169. static B& getB4( B & b, B *, const B, int)
  170. {
  171. return b;
  172. }
  173. static B* getBPtr(B *b)
  174. {
  175. return b;
  176. }
  177. string B::shared ;
  178. int B::sharedInt = -1;
  179. TEST_F(SqratTest, FunctionReturningReferencesToClassesWithStaticMembers) {
  180. DefaultVM::Set(vm);
  181. Class<B> _B(vm, _SC("B"));
  182. _B
  183. .Func(_SC("set"), &B::set)
  184. .Func(_SC("get"), &B::get)
  185. .StaticVar(_SC("shared"), &B::shared)
  186. .StaticVar(_SC("sharedInt"), &B::sharedInt);
  187. RootTable().Bind(_SC("B"), _B);
  188. RootTable().Func(_SC("getB"), &getB);
  189. RootTable().Func(_SC("getB2"), &getB2);
  190. RootTable().Func(_SC("getB4"), &getB4);
  191. RootTable().Func(_SC("getBPtr"), &getBPtr);
  192. Script script;
  193. script.CompileString(_SC(" \
  194. b <- B();\
  195. bb <- B(); \
  196. \
  197. gTest.EXPECT_INT_EQ(b.get(), -1); \
  198. gTest.EXPECT_INT_EQ(bb.sharedInt, -1); \
  199. gTest.EXPECT_INT_EQ(b.sharedInt, -1); \
  200. b.set(12);\
  201. b.shared = \"a long string\"; \
  202. b.sharedInt = 1234; \
  203. gTest.EXPECT_STR_EQ(bb.shared, \"a long string\"); \
  204. gTest.EXPECT_STR_EQ(b.shared, \"a long string\"); \
  205. gTest.EXPECT_INT_EQ(bb.sharedInt, 1234); \
  206. gTest.EXPECT_INT_EQ(b.get(), 12); \
  207. local b1 = getBPtr(b);\
  208. b.set(20);\
  209. gTest.EXPECT_INT_EQ(b1.get(), 20); \
  210. local b2 = getB(b);\
  211. b.set(40);\
  212. gTest.EXPECT_INT_EQ(b1.get(), 40); \
  213. gTest.EXPECT_INT_EQ(b2.get(), 40); \
  214. local b3 = getB2(b, 12, \"test\");\
  215. b.set(60);\
  216. gTest.EXPECT_INT_EQ(b2.get(), 60); \
  217. gTest.EXPECT_INT_EQ(b3.get(), 60); \
  218. local b4 = getB4(b, b, b, 33);\
  219. b.set(80);\
  220. gTest.EXPECT_INT_EQ(b3.get(), 80); \
  221. gTest.EXPECT_INT_EQ(b4.get(), 80); \
  222. \
  223. bb.shared = \"short str\"; \
  224. gTest.EXPECT_STR_EQ(b2.shared, \"short str\"); \
  225. gTest.EXPECT_STR_EQ(b3.shared, \"short str\"); \
  226. gTest.EXPECT_STR_EQ(b.shared, \"short str\"); \
  227. gTest.EXPECT_STR_EQ(b1.shared, \"short str\"); \
  228. gTest.EXPECT_STR_EQ(b4.shared, \"short str\"); \
  229. gTest.EXPECT_INT_EQ(b.sharedInt, 1234); \
  230. gTest.EXPECT_INT_EQ(b1.sharedInt, 1234); \
  231. gTest.EXPECT_INT_EQ(b2.sharedInt, 1234); \
  232. gTest.EXPECT_INT_EQ(b3.sharedInt, 1234); \
  233. gTest.EXPECT_INT_EQ(b4.sharedInt, 1234); \
  234. b4.shared = \"abcde\"; \
  235. b4.sharedInt = 9999; \
  236. gTest.EXPECT_STR_EQ(bb.shared, \"abcde\"); \
  237. gTest.EXPECT_INT_EQ(bb.sharedInt, 9999); \
  238. gTest.EXPECT_INT_EQ(b1.sharedInt, 9999); \
  239. "));
  240. if (Sqrat::Error::Occurred(vm)) {
  241. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  242. }
  243. script.Run();
  244. if (Sqrat::Error::Occurred(vm)) {
  245. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  246. }
  247. }