Audio plugin host https://kx.studio/carla
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.

juce_CachedValue.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #if JUCE_UNIT_TESTS
  18. class CachedValueTests : public UnitTest
  19. {
  20. public:
  21. CachedValueTests() : UnitTest ("CachedValues") {}
  22. void runTest() override
  23. {
  24. beginTest ("default constructor");
  25. {
  26. CachedValue<String> cv;
  27. expect (cv.isUsingDefault());
  28. expect (cv.get() == String());
  29. }
  30. beginTest ("without default value");
  31. {
  32. ValueTree t ("root");
  33. t.setProperty ("testkey", "testvalue", nullptr);
  34. CachedValue<String> cv (t, "testkey", nullptr);
  35. expect (! cv.isUsingDefault());
  36. expect (cv.get() == "testvalue");
  37. cv.resetToDefault();
  38. expect (cv.isUsingDefault());
  39. expect (cv.get() == String());
  40. }
  41. beginTest ("with default value");
  42. {
  43. ValueTree t ("root");
  44. t.setProperty ("testkey", "testvalue", nullptr);
  45. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  46. expect (! cv.isUsingDefault());
  47. expect (cv.get() == "testvalue");
  48. cv.resetToDefault();
  49. expect (cv.isUsingDefault());
  50. expect (cv.get() == "defaultvalue");
  51. }
  52. beginTest ("with default value (int)");
  53. {
  54. ValueTree t ("root");
  55. t.setProperty ("testkey", 23, nullptr);
  56. CachedValue<int> cv (t, "testkey", nullptr, 34);
  57. expect (! cv.isUsingDefault());
  58. expect (cv == 23);
  59. expectEquals (cv.get(), 23);
  60. cv.resetToDefault();
  61. expect (cv.isUsingDefault());
  62. expect (cv == 34);
  63. }
  64. beginTest ("with void value");
  65. {
  66. ValueTree t ("root");
  67. t.setProperty ("testkey", var(), nullptr);
  68. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  69. expect (! cv.isUsingDefault());
  70. expect (cv == "");
  71. expectEquals (cv.get(), String());
  72. }
  73. beginTest ("with non-existent value");
  74. {
  75. ValueTree t ("root");
  76. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  77. expect (cv.isUsingDefault());
  78. expect (cv == "defaultvalue");
  79. expect (cv.get() == "defaultvalue");
  80. }
  81. beginTest ("with value changing");
  82. {
  83. ValueTree t ("root");
  84. t.setProperty ("testkey", "oldvalue", nullptr);
  85. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  86. expect (cv == "oldvalue");
  87. t.setProperty ("testkey", "newvalue", nullptr);
  88. expect (cv != "oldvalue");
  89. expect (cv == "newvalue");
  90. }
  91. beginTest ("set value");
  92. {
  93. ValueTree t ("root");
  94. t.setProperty ("testkey", 23, nullptr);
  95. CachedValue<int> cv (t, "testkey", nullptr, 45);
  96. cv = 34;
  97. expectEquals ((int) t["testkey"], 34);
  98. cv.resetToDefault();
  99. expect (cv == 45);
  100. expectEquals (cv.get(), 45);
  101. expect (t["testkey"] == var());
  102. }
  103. beginTest ("reset value");
  104. {
  105. }
  106. }
  107. };
  108. static CachedValueTests cachedValueTests;
  109. #endif