actions.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* actions.cpp - test actions
  2. * Copyright (C) 2017-2018 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <catch.hpp>
  18. #include <core/node/make.h>
  19. #include <core/node/list.h>
  20. #include <core/node_info/node_info.h>
  21. #include <core/node_info/macros.h>
  22. #include <core/node_tree/traverse.h>
  23. #include <core/action_stack.h>
  24. #include <core/actions/change_value.h>
  25. #include <core/actions/change_link.h>
  26. #include <core/actions/custom_property.h>
  27. #include <core/actions/list.h>
  28. #include "zero_context.h"
  29. #include "new_node.h"
  30. REGISTER_NODE(Add);
  31. using namespace rainynite;
  32. using namespace rainynite::core;
  33. TEST_CASE("Undo/redo value change", "[action,node]") {
  34. ActionStack action_stack;
  35. auto one = make_value<double>(1.0);
  36. CHECK(one->mod() == 1.0);
  37. SECTION("Simple undo/redo") {
  38. action_stack.emplace<actions::ChangeValue>(one, 2.0);
  39. action_stack.close();
  40. CHECK(one->mod() == 2.0);
  41. action_stack.emplace<actions::ChangeValue>(one, 3.0);
  42. action_stack.close();
  43. CHECK(one->mod() == 3.0);
  44. action_stack.undo();
  45. CHECK(one->mod() == 2.0);
  46. action_stack.undo();
  47. CHECK(one->mod() == 1.0);
  48. CHECK(!action_stack.undo());
  49. CHECK(one->mod() == 1.0);
  50. action_stack.redo();
  51. CHECK(one->mod() == 2.0);
  52. action_stack.emplace<actions::ChangeValueAt>(one, 4.0, zero_context());
  53. CHECK(one->mod() == 4.0);
  54. CHECK(!action_stack.redo());
  55. CHECK(one->mod() == 4.0);
  56. }
  57. SECTION("Append value change") {
  58. action_stack.emplace<actions::ChangeValue>(one, 2.0);
  59. CHECK(one->mod() == 2.0);
  60. action_stack.emplace<actions::ChangeValue>(one, 3.0);
  61. CHECK(one->mod() == 3.0);
  62. action_stack.undo();
  63. CHECK(one->mod() == 1.0);
  64. action_stack.redo();
  65. CHECK(one->mod() == 3.0);
  66. }
  67. }
  68. TEST_CASE("Change link", "[action,node]") {
  69. ActionStack action_stack;
  70. auto add = make_node_with_name_as<BaseValue<double>>("Add");
  71. auto add_node = dynamic_pointer_cast<AbstractListLinked>(add);
  72. REQUIRE(add->value(zero_context()) == 0);
  73. auto tree = make_shared<NodeTree>(add);
  74. action_stack.emplace<actions::ChangeLink>(tree, tree->index(tree->get_root_index(), 0), make_value<double>(1));
  75. CHECK(add->value(zero_context()) == 1);
  76. action_stack.emplace<actions::ChangeLink>(tree, tree->index(tree->get_root_index(), 1), make_value<double>(2));
  77. CHECK(add->value(zero_context()) == 3);
  78. action_stack.undo();
  79. CHECK(add->value(zero_context()) == 1);
  80. action_stack.undo();
  81. CHECK(add->value(zero_context()) == 0);
  82. }
  83. TEST_CASE("Custom property", "[action,node]") {
  84. ActionStack action_stack;
  85. auto add = make_node_with_name_as<AbstractNode>("Add");
  86. auto other_node = make_value<double>(1);
  87. CHECK_THROWS_AS(add->get_property("_something"), NodeAccessError);
  88. auto tree = make_shared<NodeTree>(abstract_value_cast(add));
  89. action_stack.emplace<actions::AddCustomProperty>(tree, tree->get_root_index(), "_something", other_node);
  90. CHECK(add->get_property("_something") == other_node);
  91. action_stack.emplace<actions::RemoveCustomProperty>(tree, tree->get_root_index(), "_something");
  92. CHECK_THROWS_AS(add->get_property("_something"), NodeAccessError);
  93. action_stack.undo();
  94. CHECK(add->get_property("_something") == other_node);
  95. action_stack.undo();
  96. CHECK_THROWS_AS(add->get_property("_something"), NodeAccessError);
  97. }
  98. class EmptyTraverser : public TreeTraverser {
  99. public:
  100. EmptyTraverser(NodeTree& tree) :
  101. TreeTraverser(tree)
  102. {}
  103. protected:
  104. bool object_start(NodeTree::Index /*index*/) override {
  105. return true;
  106. }
  107. void object_end(NodeTree::Index /*index*/) override {
  108. }
  109. };
  110. TEST_CASE("List", "[action,node]") {
  111. ActionStack action_stack;
  112. auto list = make_shared<ListValue<double>>();
  113. REQUIRE(list->value(zero_context()) == vector<double>{});
  114. auto tree = make_shared<NodeTree>(list);
  115. auto list_index = tree->get_root_index();
  116. action_stack.emplace<actions::ListPushNew>(tree, list_index);
  117. CHECK(list->value(zero_context()) == vector<double>{0});
  118. EmptyTraverser traverser(*tree);
  119. traverser.traverse_tree();
  120. action_stack.emplace<actions::ListInsertElement>(tree, list_index, 1, make_value<double>(1));
  121. CHECK(list->value(zero_context()) == (vector<double>{0, 1}));
  122. action_stack.emplace<actions::ListRemoveElement>(tree, list_index, 0);
  123. CHECK(list->value(zero_context()) == vector<double>{1});
  124. action_stack.undo();
  125. action_stack.undo();
  126. action_stack.undo();
  127. CHECK(list->value(zero_context()) == vector<double>{});
  128. action_stack.redo();
  129. CHECK(list->value(zero_context()) == vector<double>{0});
  130. action_stack.redo();
  131. CHECK(list->value(zero_context()) == (vector<double>{0, 1}));
  132. action_stack.redo();
  133. CHECK(list->value(zero_context()) == vector<double>{1});
  134. }
  135. TEST_CASE("List clear action", "[action,node]") {
  136. ActionStack action_stack;
  137. auto list = make_shared<ListValue<double>>();
  138. auto tree = make_shared<NodeTree>(list);
  139. auto list_index = tree->get_root_index();
  140. REQUIRE(list->value(zero_context()) == vector<double>{});
  141. action_stack.emplace<actions::ListPushNew>(tree, list_index);
  142. CHECK(list->value(zero_context()) == vector<double>{0});
  143. action_stack.emplace<actions::ListPushNew>(tree, list_index);
  144. CHECK(list->value(zero_context()) == vector<double>{0, 0});
  145. action_stack.emplace<actions::ListClear>(tree, list_index);
  146. CHECK(list->value(zero_context()) == vector<double>{});
  147. action_stack.undo();
  148. CHECK(list->value(zero_context()) == vector<double>{0, 0});
  149. }