actions.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* actions.cpp - test actions
  2. * Copyright (C) 2017 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/node.h>
  20. #include <core/node_info.h>
  21. #include <core/action_stack.h>
  22. #include <core/actions/change_value.h>
  23. #include <core/actions/change_link.h>
  24. #include <core/actions/custom_property.h>
  25. #include <core/actions/list.h>
  26. #include "zero_context.h"
  27. using namespace rainynite;
  28. using namespace rainynite::core;
  29. TEST_CASE("Undo/redo value change", "[action,node]") {
  30. ActionStack action_stack;
  31. auto one = make_value<double>(1.0);
  32. CHECK(one->mod() == 1.0);
  33. SECTION("Simple undo/redo") {
  34. action_stack.emplace<actions::ChangeValue>(one, 2.0);
  35. action_stack.close();
  36. CHECK(one->mod() == 2.0);
  37. action_stack.emplace<actions::ChangeValue>(one, 3.0);
  38. action_stack.close();
  39. CHECK(one->mod() == 3.0);
  40. action_stack.undo();
  41. CHECK(one->mod() == 2.0);
  42. action_stack.undo();
  43. CHECK(one->mod() == 1.0);
  44. CHECK(!action_stack.undo());
  45. CHECK(one->mod() == 1.0);
  46. action_stack.redo();
  47. CHECK(one->mod() == 2.0);
  48. action_stack.emplace<actions::ChangeValue>(one, 4.0);
  49. CHECK(one->mod() == 4.0);
  50. CHECK(!action_stack.redo());
  51. CHECK(one->mod() == 4.0);
  52. }
  53. SECTION("Append value change") {
  54. action_stack.emplace<actions::ChangeValue>(one, 2.0);
  55. CHECK(one->mod() == 2.0);
  56. action_stack.emplace<actions::ChangeValue>(one, 3.0);
  57. CHECK(one->mod() == 3.0);
  58. action_stack.undo();
  59. CHECK(one->mod() == 1.0);
  60. action_stack.redo();
  61. CHECK(one->mod() == 3.0);
  62. }
  63. }
  64. TEST_CASE("Change link", "[action,node]") {
  65. ActionStack action_stack;
  66. auto add = make_node_with_name<BaseValue<double>>("Add/Real");
  67. auto add_node = dynamic_pointer_cast<AbstractListLinked>(add);
  68. REQUIRE(add->value(zero_context()) == 0);
  69. action_stack.emplace<actions::ChangeLink>(add_node, 0, make_value<double>(1));
  70. CHECK(add->value(zero_context()) == 1);
  71. action_stack.emplace<actions::ChangeLink>(add_node, 1, make_value<double>(2));
  72. CHECK(add->value(zero_context()) == 3);
  73. action_stack.undo();
  74. CHECK(add->value(zero_context()) == 1);
  75. action_stack.undo();
  76. CHECK(add->value(zero_context()) == 0);
  77. }
  78. TEST_CASE("Custom property", "[action,node]") {
  79. ActionStack action_stack;
  80. auto add = make_node_with_name<AbstractNode>("Add/Real");
  81. auto other_node = make_value<double>(1);
  82. CHECK_THROWS_AS(add->get_property("_something"), NodeAccessError);
  83. action_stack.emplace<actions::AddCustomProperty>(add, "_something", other_node);
  84. CHECK(add->get_property("_something") == other_node);
  85. action_stack.emplace<actions::RemoveCustomProperty>(add, "_something");
  86. CHECK_THROWS_AS(add->get_property("_something"), NodeAccessError);
  87. action_stack.undo();
  88. CHECK(add->get_property("_something") == other_node);
  89. action_stack.undo();
  90. CHECK_THROWS_AS(add->get_property("_something"), NodeAccessError);
  91. }
  92. TEST_CASE("List", "[action,node]") {
  93. ActionStack action_stack;
  94. auto list = make_shared<ListValue<double>>();
  95. REQUIRE(list->value(zero_context()) == vector<double>{});
  96. action_stack.emplace<actions::ListPushNew>(list);
  97. CHECK(list->value(zero_context()) == vector<double>{0});
  98. action_stack.emplace<actions::ListInsertElement>(list, 1, make_value<double>(1));
  99. CHECK(list->value(zero_context()) == (vector<double>{0, 1}));
  100. action_stack.emplace<actions::ListRemoveElement>(list, 0);
  101. CHECK(list->value(zero_context()) == vector<double>{1});
  102. action_stack.undo();
  103. action_stack.undo();
  104. action_stack.undo();
  105. CHECK(list->value(zero_context()) == vector<double>{});
  106. action_stack.redo();
  107. CHECK(list->value(zero_context()) == vector<double>{0});
  108. action_stack.redo();
  109. CHECK(list->value(zero_context()) == (vector<double>{0, 1}));
  110. action_stack.redo();
  111. CHECK(list->value(zero_context()) == vector<double>{1});
  112. }