Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ctx_test2.cpp 3.4KB

6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <string>
  2. #include "any.h"
  3. #include "ctx.h"
  4. #include "memory_Context.h"
  5. std::string s(const char *str) {
  6. return std::string(str);
  7. }
  8. std::string test_ctx_Controller_executeFunctions_registerFunction_set() {
  9. auto c = memory_createContext();
  10. ctx_Controller<memory_Context> ctrl(c);
  11. // Disable automatic invocation of executeFunctions.
  12. ctrl.isProcessingQueue = true;
  13. ctrl.set("input", s("123"));
  14. auto processInput = [](memory_Context c) -> memory_Context {
  15. if (c.recentField == "input") {
  16. c.outputHelp = "Checked";
  17. c.recentField = "outputHelp";
  18. return c;
  19. }
  20. c.recentField = "none";
  21. return c;
  22. };
  23. ctrl.registerFunction(processInput);
  24. // Apply 'input'.
  25. ctrl.executeFunctions();
  26. // Apply 'outputHelp'.
  27. ctrl.executeFunctions();
  28. if (
  29. ctrl.context.input == "123" &&
  30. ctrl.context.outputHelp == "Checked"
  31. ) {
  32. return "OK: ctx_Controller_executeFunctions_set";
  33. }
  34. return "ERR: ctx_Controller_executeFunctions_set";
  35. }
  36. std::string test_ctx_Controller_processQueue() {
  37. auto c = memory_createContext();
  38. ctx_Controller<memory_Context> ctrl(c);
  39. auto processInput = [](memory_Context c) -> memory_Context {
  40. if (c.recentField == "input") {
  41. c.outputHelp = "Checked";
  42. c.recentField = "outputHelp";
  43. return c;
  44. }
  45. c.recentField = "none";
  46. return c;
  47. };
  48. ctrl.registerFunction(processInput);
  49. ctrl.set("input", s("abc"));
  50. if (
  51. ctrl.context.input == "abc" &&
  52. ctrl.context.outputHelp == "Checked"
  53. ) {
  54. return "OK: ctx_Controller_processQueue";
  55. }
  56. return "ERR: ctx_Controller_processQueue";
  57. }
  58. std::string test_ctx_Controller_registerFieldCallback_match() {
  59. auto c = memory_createContext();
  60. ctx_Controller<memory_Context> ctrl(c);
  61. c.input = "123";
  62. c.recentField = "input";
  63. std::string callbackInput = "";
  64. auto setCallbackInput = [&callbackInput](memory_Context c) {
  65. if (
  66. c.recentField == "input"
  67. ) {
  68. callbackInput = c.input;
  69. }
  70. };
  71. ctrl.registerFieldCallback("input", setCallbackInput);
  72. ctrl.reportContext();
  73. if (
  74. ctrl.context.input == callbackInput
  75. ) {
  76. return "OK: ctx_Controller_registerFieldCallback_match";
  77. }
  78. return "ERR: ctx_Controller_registerFieldCallback_match";
  79. }
  80. std::string test_ctx_Controller_registerFieldCallback_mismatch() {
  81. auto c = memory_createContext();
  82. ctx_Controller<memory_Context> ctrl(c);
  83. c.input = "123";
  84. c.outputHelp = "you";
  85. // A field other than 'input' is marked recent.
  86. c.recentField = "outputHelp";
  87. std::string callbackInput = "";
  88. auto setCallbackInput = [&callbackInput](memory_Context c) {
  89. if (
  90. c.recentField == "input"
  91. ) {
  92. callbackInput = c.input;
  93. }
  94. };
  95. ctrl.registerFieldCallback("input", setCallbackInput);
  96. ctrl.reportContext();
  97. if (
  98. callbackInput == ""
  99. ) {
  100. return "OK: ctx_Controller_registerFieldCallback_mismatch";
  101. }
  102. return "ERR: ctx_Controller_registerFieldCallback_mismatch";
  103. }
  104. std::string test_memory_Context_field() {
  105. auto c = memory_createContext();
  106. c.input = "abc";
  107. if (
  108. libany::any_cast<std::string>(c.field("input")) == "abc"
  109. ) {
  110. return "OK: memory_Context_field";
  111. }
  112. return "ERR: memory_Context_field";
  113. }
  114. std::string test_memory_Context_setField() {
  115. auto c = memory_createContext();
  116. c.input = "abc";
  117. c.setField("input", s("123"));
  118. if (
  119. c.input == "123"
  120. ) {
  121. return "OK: memory_Context_setField";
  122. }
  123. return "ERR: memory_Context_setField";
  124. }