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.

160 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. #if JUCE_UNIT_TESTS
  22. class CachedValueTests : public UnitTest
  23. {
  24. public:
  25. CachedValueTests() : UnitTest ("CachedValues", "Values") {}
  26. void runTest() override
  27. {
  28. beginTest ("default constructor");
  29. {
  30. CachedValue<String> cv;
  31. expect (cv.isUsingDefault());
  32. expect (cv.get() == String());
  33. }
  34. beginTest ("without default value");
  35. {
  36. ValueTree t ("root");
  37. t.setProperty ("testkey", "testvalue", nullptr);
  38. CachedValue<String> cv (t, "testkey", nullptr);
  39. expect (! cv.isUsingDefault());
  40. expect (cv.get() == "testvalue");
  41. cv.resetToDefault();
  42. expect (cv.isUsingDefault());
  43. expect (cv.get() == String());
  44. }
  45. beginTest ("with default value");
  46. {
  47. ValueTree t ("root");
  48. t.setProperty ("testkey", "testvalue", nullptr);
  49. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  50. expect (! cv.isUsingDefault());
  51. expect (cv.get() == "testvalue");
  52. cv.resetToDefault();
  53. expect (cv.isUsingDefault());
  54. expect (cv.get() == "defaultvalue");
  55. }
  56. beginTest ("with default value (int)");
  57. {
  58. ValueTree t ("root");
  59. t.setProperty ("testkey", 23, nullptr);
  60. CachedValue<int> cv (t, "testkey", nullptr, 34);
  61. expect (! cv.isUsingDefault());
  62. expect (cv == 23);
  63. expectEquals (cv.get(), 23);
  64. cv.resetToDefault();
  65. expect (cv.isUsingDefault());
  66. expect (cv == 34);
  67. }
  68. beginTest ("with void value");
  69. {
  70. ValueTree t ("root");
  71. t.setProperty ("testkey", var(), nullptr);
  72. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  73. expect (! cv.isUsingDefault());
  74. expect (cv == "");
  75. expectEquals (cv.get(), String());
  76. }
  77. beginTest ("with non-existent value");
  78. {
  79. ValueTree t ("root");
  80. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  81. expect (cv.isUsingDefault());
  82. expect (cv == "defaultvalue");
  83. expect (cv.get() == "defaultvalue");
  84. }
  85. beginTest ("with value changing");
  86. {
  87. ValueTree t ("root");
  88. t.setProperty ("testkey", "oldvalue", nullptr);
  89. CachedValue<String> cv (t, "testkey", nullptr, "defaultvalue");
  90. expect (cv == "oldvalue");
  91. t.setProperty ("testkey", "newvalue", nullptr);
  92. expect (cv != "oldvalue");
  93. expect (cv == "newvalue");
  94. }
  95. beginTest ("set value");
  96. {
  97. ValueTree t ("root");
  98. t.setProperty ("testkey", 23, nullptr);
  99. CachedValue<int> cv (t, "testkey", nullptr, 45);
  100. cv = 34;
  101. expectEquals ((int) t["testkey"], 34);
  102. cv.resetToDefault();
  103. expect (cv == 45);
  104. expectEquals (cv.get(), 45);
  105. expect (t["testkey"] == var());
  106. }
  107. beginTest ("reset value");
  108. {
  109. }
  110. }
  111. };
  112. static CachedValueTests cachedValueTests;
  113. #endif
  114. } // namespace juce