ClassProperties.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. struct Item {
  28. Item() {}
  29. Item(const Item& i) : name(i.name) {}
  30. string name;
  31. };
  32. class Player {
  33. public:
  34. Player() : health(10) {};
  35. int Health() const {
  36. return health;
  37. }
  38. void SetHealth(const int& h) {
  39. health = h;
  40. if(health < 0) {
  41. health = 0;
  42. }
  43. }
  44. bool Dead() const {
  45. return (health == 0);
  46. }
  47. Item LeftHand() const {
  48. return leftHand;
  49. }
  50. void SetLeftHand(const Item& i) {
  51. leftHand = i;
  52. }
  53. Item RightHand() const {
  54. return rightHand;
  55. }
  56. void SetRightHand(const Item& i) {
  57. rightHand = i;
  58. }
  59. private:
  60. int health;
  61. Item leftHand;
  62. Item rightHand;
  63. };
  64. TEST_F(SqratTest, ClassProperties) {
  65. DefaultVM::Set(vm);
  66. RootTable().Bind(_SC("Item"),
  67. Class<Item>(vm, _SC("Item"))
  68. .Var(_SC("name"), &Item::name)
  69. );
  70. RootTable().Bind(_SC("Player"),
  71. Class<Player>(vm, _SC("Player"))
  72. // Properties
  73. .Prop(_SC("health"), &Player::Health, &Player::SetHealth)
  74. .Prop(_SC("dead"), &Player::Dead) // Read Only Property
  75. .Prop(_SC("leftHand"), &Player::LeftHand, &Player::SetLeftHand)
  76. .Prop(_SC("rightHand"), &Player::RightHand, &Player::SetRightHand)
  77. );
  78. Script script;
  79. script.CompileString(_SC(" \
  80. p <- Player(); \
  81. gTest.EXPECT_INT_EQ(p.health, 10); \
  82. p.health = 5; \
  83. gTest.EXPECT_INT_EQ(p.health, 5); \
  84. p.health -= 3; \
  85. gTest.EXPECT_INT_EQ(p.health, 2); \
  86. p.health -= 3; \
  87. gTest.EXPECT_INT_EQ(p.health, 0); \
  88. gTest.EXPECT_TRUE(p.dead); \
  89. \
  90. item1 <- Item(); \
  91. item1.name = \"Sword\"; \
  92. p.rightHand = item1; \
  93. item2 <- Item(); \
  94. item2.name = \"Shield\"; \
  95. p.leftHand = item2; \
  96. gTest.EXPECT_STR_EQ(p.rightHand.name, \"Sword\"); \
  97. gTest.EXPECT_STR_EQ(p.leftHand.name, \"Shield\"); \
  98. "));
  99. if (Sqrat::Error::Occurred(vm)) {
  100. FAIL() << _SC("Compile Failed: ") << Sqrat::Error::Message(vm);
  101. }
  102. script.Run();
  103. if (Sqrat::Error::Occurred(vm)) {
  104. FAIL() << _SC("Run Failed: ") << Sqrat::Error::Message(vm);
  105. }
  106. }