FuncInputArgumentType.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // Copyright (c) 2013 Li-Cheng (Andy) Tai
  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 Vector2
  28. {
  29. public:
  30. float value;
  31. //some Vector Math overloads and members here
  32. operator float() const
  33. {
  34. return 1.0f; /* test only */
  35. }
  36. Vector2 operator +(const Vector2& v)
  37. {
  38. value += v.value;
  39. return v; /* test only */
  40. }
  41. inline float add(const Vector2& v)
  42. {
  43. return (*this) + v; //it crashes right here as it references to an nonexistent obj
  44. }
  45. bool boolFunc()
  46. {
  47. return true;
  48. }
  49. bool boolFunc2()
  50. {
  51. return false;
  52. }
  53. };
  54. static const SQChar *sq_code = _SC("\
  55. local v = Vector2();\
  56. local v2 = Vector2();\
  57. \
  58. local b = v2.boolFunc(); \
  59. \
  60. \
  61. local b = v2.boolFunc(); \
  62. \
  63. gTest.EXPECT_TRUE(b); \
  64. gTest.EXPECT_INT_EQ(b, 1); \
  65. gTest.EXPECT_FLOAT_EQ(b, 1.0); \
  66. b = v2.boolFunc2(); \
  67. \
  68. gTest.EXPECT_FALSE(b); \
  69. gTest.EXPECT_INT_EQ(b, 0); \
  70. gTest.EXPECT_FLOAT_EQ(b, 0.0); \
  71. \
  72. print (b) ; \
  73. b = v2.boolFunc2(); \
  74. \
  75. print (b) ; \
  76. \
  77. v.add(v2); /*good*/ \
  78. print(\"1\\n\");\
  79. local raised = false;\
  80. try { \
  81. v.add(10); \
  82. gTest.EXPECT_INT_EQ(0, 1); \
  83. } catch (ex) {\
  84. raised = true;\
  85. print(ex + \"\\n\"); \
  86. }\
  87. gTest.EXPECT_TRUE(raised); \
  88. print(\"2\\n\");\
  89. raised = false;\
  90. try { \
  91. v.add(); \
  92. gTest.EXPECT_INT_EQ(0, 1); \
  93. } catch (ex) {\
  94. raised = true;\
  95. print(ex + \"\\n\"); \
  96. }\
  97. gTest.EXPECT_TRUE(raised); \
  98. print(\"3\\n\");\
  99. raised = false;\
  100. try {\
  101. v.add(\"text\"); \
  102. gTest.EXPECT_INT_EQ(0, 1); \
  103. } catch (ex) {\
  104. raised = true;\
  105. print(ex + \"\\n\"); \
  106. }\
  107. gTest.EXPECT_TRUE(raised); \
  108. print(\"4\\n\");\
  109. ");
  110. TEST_F(SqratTest, NumericArgumentTypeConversionAndCheck) {
  111. DefaultVM::Set(vm);
  112. Sqrat::Class<Vector2> classVector2(vm, _SC("Vector2"));
  113. classVector2.Func(_SC("add"), &Vector2::add);
  114. classVector2.Func(_SC("boolFunc"), &Vector2::boolFunc);
  115. classVector2.Func(_SC("boolFunc2"), &Vector2::boolFunc2);
  116. Sqrat::RootTable(vm).Bind(_SC("Vector2"), classVector2);
  117. Script script;
  118. script.CompileString(sq_code);
  119. if (Sqrat::Error::Occurred(vm)) {
  120. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  121. }
  122. script.Run();
  123. if (Sqrat::Error::Occurred(vm)) {
  124. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  125. }
  126. }
  127. class F
  128. {
  129. public:
  130. int func(int i, char j)
  131. {
  132. return 1;
  133. }
  134. const char * func(char c, const char *s)
  135. {
  136. return s;
  137. }
  138. };
  139. static const char *sq_code2 = _SC("\
  140. f <- F();\
  141. gTest.EXPECT_INT_EQ(1, f.f1(2. 'v'));\
  142. gTest.EXPECT_STR_EQ(\"test\", f.f2('t', \"test\")); \
  143. ");
  144. TEST_F(SqratTest, FunctionOfSameNumberOfArgumentsButDifferentTypesBinding) {
  145. DefaultVM::Set(vm);
  146. Sqrat::Class<F> Fclass(vm, _SC("F"));
  147. Fclass.Func<int (F::*)(int, char)>(_SC("f1"), &F::func);
  148. Fclass.Func<const char * (F::*)(char, const char*)>(_SC("f2"), &F::func);
  149. Sqrat::RootTable(vm).Bind(_SC("F"), Fclass);
  150. Script script;
  151. script.CompileString(sq_code2);
  152. if (Sqrat::Error::Occurred(vm)) {
  153. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  154. }
  155. script.Run();
  156. if (Sqrat::Error::Occurred(vm)) {
  157. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  158. }
  159. }