UniqueObject.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <gtest/gtest.h>
  2. #include <sqrat.h>
  3. #include "Fixture.h"
  4. struct CxxObject {
  5. CxxObject& cxx_ptr() {
  6. return *this;
  7. }
  8. };
  9. char const script_code[] = "\
  10. v <- CxxObject();\
  11. gTest.EXPECT_STR_EQ(v.tostring, v.cxx_ptr().tostring);\
  12. \
  13. target <- null;\
  14. function set_target(v) {\
  15. ::target = v;\
  16. }\
  17. function test_target(v) {\
  18. gTest.EXPECT_STR_EQ(::target.tostring, v.tostring);\
  19. }\
  20. ";
  21. using namespace Sqrat;
  22. TEST_F(SqratTest, UniqueObject) {
  23. DefaultVM::Set(vm);
  24. Class<CxxObject> cls(vm, _SC("CxxObject"));
  25. cls.Func<CxxObject& (CxxObject::*)()>(_SC("cxx_ptr"), &CxxObject::cxx_ptr);
  26. RootTable(vm).Bind(_SC("CxxObject"), cls);
  27. Script script;
  28. std::string err;
  29. if (!script.CompileString(script_code, err)) {
  30. FAIL() << _SC("Compile failed: ") << err;
  31. }
  32. if (!script.Run(err)) {
  33. FAIL() << _SC("Script failed: ") << err;
  34. }
  35. Function set_target(RootTable(vm), "set_target"), test_target(RootTable(vm), "test_target");
  36. CxxObject* obj = new CxxObject;
  37. set_target(obj);
  38. test_target(obj);
  39. delete obj;
  40. }