SquirrelFunctions.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. TEST_F(SqratTest, CallSquirrelFunction) {
  28. DefaultVM::Set(vm);
  29. Script script;
  30. script.CompileString(_SC(" \
  31. function AddTwo(a, b) { \
  32. return a + b; \
  33. } \
  34. function MultiplyTwo(a, b) { \
  35. return a * b; \
  36. } \
  37. function returnTrue() { \
  38. return true; \
  39. }\
  40. function returnFalse() { \
  41. return false; \
  42. }\
  43. "));
  44. if (Sqrat::Error::Occurred(vm)) {
  45. FAIL() << _SC("Script Compile Failed: ") << Sqrat::Error::Message(vm);
  46. }
  47. script.Run(); // Must run the script before the function will be available
  48. if (Sqrat::Error::Occurred(vm)) {
  49. FAIL() << _SC("Script Run Failed: ") << Sqrat::Error::Message(vm);
  50. }
  51. // Method one for function retrieval: via the constructor
  52. Function addTwo(RootTable(), _SC("AddTwo"));
  53. ASSERT_FALSE(addTwo.IsNull());
  54. EXPECT_EQ(*addTwo.Evaluate<int>(1, 2), 3);
  55. // Method two for function retrieval: from the class or table
  56. Function multiplyTwo = RootTable().GetFunction(_SC("MultiplyTwo"));
  57. ASSERT_FALSE(multiplyTwo.IsNull());
  58. EXPECT_EQ(*multiplyTwo.Evaluate<int>(2, 3), 6);
  59. Function returnTrue = RootTable().GetFunction(_SC("returnTrue"));
  60. ASSERT_FALSE(returnTrue.IsNull());
  61. ASSERT_TRUE(*returnTrue.Evaluate<bool>());
  62. ASSERT_TRUE(*returnTrue.Evaluate<SQBool>());
  63. Function returnFalse = RootTable().GetFunction(_SC("returnFalse"));
  64. ASSERT_FALSE(returnFalse.IsNull());
  65. ASSERT_FALSE(*returnFalse.Evaluate<bool>());
  66. ASSERT_FALSE(*returnFalse.Evaluate<SQBool>());
  67. }
  68. int NativeOp(int a, int b, Function opFunc) {
  69. if(opFunc.IsNull()) {
  70. return -1;
  71. }
  72. return *opFunc.Evaluate<int>(a, b);
  73. }
  74. TEST_F(SqratTest, FunctionAsArgument) {
  75. DefaultVM::Set(vm);
  76. RootTable().Func(_SC("NativeOp"), &NativeOp);
  77. Script script;
  78. script.CompileString(_SC(" \
  79. function SubTwo(a, b) { \
  80. return a - b; \
  81. } \
  82. gTest.EXPECT_INT_EQ(::NativeOp(5, 1, SubTwo), 4); \
  83. "));
  84. if (Sqrat::Error::Occurred(vm)) {
  85. FAIL() << _SC("Script Compile Failed: ") << Sqrat::Error::Message(vm);
  86. }
  87. script.Run();
  88. if (Sqrat::Error::Occurred(vm)) {
  89. FAIL() << _SC("Script Run Failed: ") << Sqrat::Error::Message(vm);
  90. }
  91. }