Fixture.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #if !defined(SQRAT_TEST_FIXTURE_H)
  2. #define SQRAT_TEST_FIXTURE_H
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdarg.h>
  8. #include <squirrel.h>
  9. #include <sqstdio.h>
  10. #include <sqrat.h>
  11. #include <gtest/gtest.h>
  12. namespace Sqrat {
  13. class SqratTest : public ::testing::Test {
  14. protected:
  15. HSQUIRRELVM vm;
  16. virtual void SetUp() {
  17. vm = sq_open(1024);
  18. #if SQUIRREL_VERSION_NUMBER >= 300
  19. sq_setprintfunc(vm, printfunc, printfunc);
  20. #else
  21. sq_setprintfunc(vm, printfunc);
  22. #endif
  23. sq_newclosure(vm, errorhandler,0);
  24. sq_seterrorhandler(vm);
  25. BindTestFunctions();
  26. }
  27. virtual void TearDown() {
  28. sq_close(vm);
  29. }
  30. #ifdef SQUNICODE
  31. #define scvprintf vwprintf
  32. #else
  33. #define scvprintf vprintf
  34. #endif
  35. static void printfunc(HSQUIRRELVM v,const SQChar *s,...) {
  36. va_list vl;
  37. va_start(vl, s);
  38. scvprintf(s, vl);
  39. va_end(vl);
  40. }
  41. static SQInteger errorhandler(HSQUIRRELVM v) {
  42. const SQChar *sErr = 0;
  43. if(sq_gettop(v)>=1) {
  44. if(SQ_SUCCEEDED(sq_getstring(v,2,&sErr))) {
  45. ADD_FAILURE() << _SC("A Script Error Occured: ") << sErr;
  46. }
  47. else {
  48. ADD_FAILURE() << _SC("An Unknown Script Error Occured.") << sErr;
  49. }
  50. }
  51. return 0;
  52. }
  53. // Test Macros
  54. // Assert
  55. static void SQ_ASSERT_TRUE(bool a) {
  56. ASSERT_TRUE(a);
  57. }
  58. static void SQ_ASSERT_FALSE(bool a) {
  59. ASSERT_FALSE(a);
  60. }
  61. static void SQ_ASSERT_INT_EQ(int a, int b) {
  62. ASSERT_EQ(a, b);
  63. }
  64. static void SQ_ASSERT_INT_NE(int a, int b) {
  65. ASSERT_NE(a, b);
  66. }
  67. static void SQ_ASSERT_FLOAT_EQ(float a, float b) {
  68. ASSERT_FLOAT_EQ(a, b);
  69. }
  70. static void SQ_ASSERT_FLOAT_NE(float a, float b) {
  71. ASSERT_NE(a, b);
  72. }
  73. static void SQ_ASSERT_STR_EQ(string a, string b) {
  74. ASSERT_STREQ(a.c_str(), b.c_str());
  75. }
  76. static void SQ_ASSERT_STR_NE(string a, string b) {
  77. ASSERT_STRNE(a.c_str(), b.c_str());
  78. }
  79. // Expect
  80. static void SQ_EXPECT_TRUE(bool a) {
  81. EXPECT_TRUE(a);
  82. }
  83. static void SQ_EXPECT_FALSE(bool a) {
  84. EXPECT_FALSE(a);
  85. }
  86. static void SQ_EXPECT_INT_EQ(int a, int b) {
  87. EXPECT_EQ(a, b);
  88. }
  89. static void SQ_EXPECT_INT_NE(int a, int b) {
  90. EXPECT_NE(a, b);
  91. }
  92. static void SQ_EXPECT_FLOAT_EQ(float a, float b) {
  93. EXPECT_FLOAT_EQ(a, b);
  94. }
  95. static void SQ_EXPECT_FLOAT_NE(float a, float b) {
  96. EXPECT_NE(a, b);
  97. }
  98. static void SQ_EXPECT_STR_EQ(string a, string b) {
  99. EXPECT_STREQ(a.c_str(), b.c_str());
  100. }
  101. static void SQ_EXPECT_STR_NE(string a, string b) {
  102. EXPECT_STRNE(a.c_str(), b.c_str());
  103. }
  104. void BindTestFunctions() {
  105. RootTable(vm).Bind(_SC("gTest"), Table(vm)
  106. .Func(_SC("ASSERT_TRUE"), &SQ_ASSERT_TRUE)
  107. .Func(_SC("ASSERT_FALSE"), &SQ_ASSERT_FALSE)
  108. .Func(_SC("ASSERT_INT_EQ"), &SQ_ASSERT_INT_EQ)
  109. .Func(_SC("ASSERT_INT_NE"), &SQ_ASSERT_INT_NE)
  110. .Func(_SC("ASSERT_FLOAT_EQ"), &SQ_ASSERT_FLOAT_EQ)
  111. .Func(_SC("ASSERT_FLOAT_NE"), &SQ_ASSERT_FLOAT_NE)
  112. .Func(_SC("ASSERT_STR_EQ"), &SQ_ASSERT_STR_EQ)
  113. .Func(_SC("ASSERT_STR_NE"), &SQ_ASSERT_STR_NE)
  114. .Func(_SC("EXPECT_TRUE"), &SQ_EXPECT_TRUE)
  115. .Func(_SC("EXPECT_FALSE"), &SQ_EXPECT_FALSE)
  116. .Func(_SC("EXPECT_INT_EQ"), &SQ_EXPECT_INT_EQ)
  117. .Func(_SC("EXPECT_INT_NE"), &SQ_EXPECT_INT_NE)
  118. .Func(_SC("EXPECT_FLOAT_EQ"), &SQ_EXPECT_FLOAT_EQ)
  119. .Func(_SC("EXPECT_FLOAT_NE"), &SQ_EXPECT_FLOAT_NE)
  120. .Func(_SC("EXPECT_STR_EQ"), &SQ_EXPECT_STR_EQ)
  121. .Func(_SC("EXPECT_STR_NE"), &SQ_EXPECT_STR_NE)
  122. );
  123. }
  124. };
  125. }
  126. #endif