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.

156 lines
4.3KB

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